Skip to content

Commit b9ca460

Browse files
authored
add scss parsing to the extension (#12)
* add scss parsing to the extension * add scss parsing to the extension * fix typo'd variable name * another typo
1 parent 830876e commit b9ca460

File tree

6 files changed

+61
-35
lines changed

6 files changed

+61
-35
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"require": {
3-
"mck89/peast": "^1.16"
3+
"mck89/peast": "^1.16",
4+
"scssphp/scssphp": "^1.13.0"
45
},
56
"require-dev": {
67
"liquipedia/sqllint": "*",

extension.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ResourceLoaderArticles",
3-
"version": "2.3.0",
3+
"version": "2.4.0",
44
"author": [
55
"[https://fo-nttax.de Alex Winkler]"
66
],
@@ -57,4 +57,4 @@
5757
}
5858
},
5959
"manifest_version": 2
60-
}
60+
}

i18n/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
"resourceloaderarticles-success-delete": "Resource successfully deleted",
3333

3434
"resourceloaderarticles-help-page": "Page name of resource (excl. `MediaWiki:Common.[js|css]/`). Valid JS ends with `.js` and valid styling ends with `.css` or `.less`.",
35-
"resourceloaderarticles-help-priority": "Priority for loading order of the resource (higher first), falls back to alphabetic order within a single priority class."
35+
"resourceloaderarticles-help-priority": "Priority for loading order of the resource (higher first), falls back to alphabetic order within a single priority class. Priority is done in two groups: Less/CSS and SCSS. The first group will always be before the second."
3636
}

src/Hooks/MainHookHandler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function onBeforePageDisplay( $out, $skin ): void {
8383
}
8484

8585
/**
86-
* Set the CONTENT_MODEL_CSS content handler for less files
86+
* Set the CONTENT_MODEL_CSS content handler for less and scss files
8787
*
8888
* @param Title $title
8989
* @param string &$model
@@ -94,6 +94,9 @@ public function onContentHandlerDefaultModelFor( $title, &$model ) {
9494
if ( str_ends_with( $title->getText(), '.less' ) ) {
9595
$model = CONTENT_MODEL_CSS;
9696
return true;
97+
} elseif ( str_ends_with( $title->getText(), '.scss' ) ) {
98+
$model = CONTENT_MODEL_CSS;
99+
return true;
97100
}
98101
}
99102
}

src/ResourceLoader/ResourceLoaderArticlesModule.php

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ResourceLoader;
1717
use ResourceLoaderContext;
1818
use ResourceLoaderWikiModule;
19+
use ScssPhp\ScssPhp\Compiler as SCSSCompiler;
1920

2021
class ResourceLoaderArticlesModule extends ResourceLoaderWikiModule {
2122

@@ -38,7 +39,11 @@ protected function getPages( ResourceLoaderContext $context ) {
3839
foreach ( $articles as $article ) {
3940
if ( substr( $article, -3 ) === '.js' ) {
4041
$pages[ 'MediaWiki:Common.js/' . $article ] = [ 'type' => 'script' ];
41-
} elseif ( substr( $article, -4 ) === '.css' || substr( $article, -5 ) === '.less' ) {
42+
} elseif (
43+
substr( $article, -4 ) === '.css'
44+
|| substr( $article, -5 ) === '.less'
45+
|| substr( $article, -5 ) === '.scss'
46+
) {
4247
$pages[ 'MediaWiki:Common.css/' . $article ] = [ 'type' => 'style' ];
4348
}
4449
}
@@ -99,12 +104,12 @@ static function () use ( $contents, $fileName ) {
99104
* @return array
100105
*/
101106
public function getStyles( ResourceLoaderContext $context ) {
102-
$styles = [];
107+
$less = '';
108+
$scss = '';
103109
foreach ( $this->getPages( $context ) as $titleText => $options ) {
104110
if ( $options[ 'type' ] !== 'style' ) {
105111
continue;
106112
}
107-
$media = isset( $options[ 'media' ] ) ? $options[ 'media' ] : 'all';
108113
$style = $this->getContent( $titleText, $context );
109114
if ( strval( $style ) === '' ) {
110115
continue;
@@ -114,33 +119,43 @@ public function getStyles( ResourceLoaderContext $context ) {
114119
$style = '/* using @import is forbidden */';
115120
}
116121

117-
if ( !isset( $styles[ $media ] ) ) {
118-
$styles[ $media ] = [];
119-
$styles[ $media ][ 0 ] = '';
120-
}
121122
$style = ResourceLoader::makeComment( $titleText ) . $style;
122-
$styles[ $media ][ 0 ] .= $style;
123-
}
124-
foreach ( $styles as $media => $styleItem ) {
125-
/* start of less parser */
126-
try {
127-
$lessc = new Less_Parser;
128-
$lessc->parse( $styleItem[ 0 ] );
129-
$style = $lessc->getCss();
130-
} catch ( exception $e ) {
131-
$style = '/* invalid less: ' . $e->getMessage() . ' */';
132-
}
133-
/* end of less parser */
134-
if ( $this->getFlip( $context ) ) {
135-
$style = CSSJanus::transform( $style, true, false );
123+
if ( substr( $titleText, -5 ) === '.scss' ) {
124+
$scss .= $style;
125+
} else {
126+
$less .= $style;
136127
}
137-
$style = MemoizedCallable::call(
138-
'CSSMin::remap',
139-
[ $style, false, $this->getConfig()->get( 'ScriptPath' ), true ]
140-
);
141-
$styles[ $media ][ 0 ] = $style;
142128
}
143-
return $styles;
129+
/* start of less parser */
130+
try {
131+
$lessc = new Less_Parser;
132+
$lessc->parse( $less );
133+
$compiledLess = $lessc->getCss();
134+
} catch ( \Exception $e ) {
135+
$compiledLess = '/* invalid less: ' . $e->getMessage() . ' */';
136+
}
137+
/* end of less parser */
138+
139+
/* start of scss parser */
140+
try {
141+
$compiler = new SCSSCompiler();
142+
$compiledScss = $compiler->compileString( $scss )->getCss();
143+
} catch ( \Exception $e ) {
144+
$compiledScss = '/* invalid scss: ' . $e->getMessage() . ' */';
145+
}
146+
/* end of scss parser */
147+
148+
$css = $compiledLess . $compiledScss;
149+
150+
if ( $this->getFlip( $context ) ) {
151+
$css = CSSJanus::transform( $css, true, false );
152+
}
153+
$css = MemoizedCallable::call(
154+
'CSSMin::remap',
155+
[ $css, false, $this->getConfig()->get( 'ScriptPath' ), true ]
156+
);
157+
158+
return [ 'all' => [ $css ] ];
144159
}
145160

146161
/**

src/SpecialPage/SpecialResourceLoaderArticles.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,18 @@ public function trimValueCB( $value ) {
346346
*/
347347
public function validatePageCB( $value, $alldata ) {
348348
if (
349-
( $alldata[ 'Type' ] === 'style'
350-
&& !( ( strlen( $value ) > 4 && substr( $value, -4 ) === '.css' )
349+
(
350+
$alldata[ 'Type' ] === 'style'
351+
&& !(
352+
( strlen( $value ) > 4 && substr( $value, -4 ) === '.css' )
351353
|| ( strlen( $value ) > 5 && substr( $value, -5 ) === '.less' )
354+
|| ( strlen( $value ) > 5 && substr( $value, -5 ) === '.scss' )
352355
)
353-
) || ( $alldata[ 'Type' ] === 'script' && !( strlen( $value ) > 3 && substr( $value, -3 ) === '.js' ) )
356+
)
357+
|| (
358+
$alldata[ 'Type' ] === 'script'
359+
&& !( strlen( $value ) > 3 && substr( $value, -3 ) === '.js' )
360+
)
354361
) {
355362
return $this->msg( 'resourceloaderarticles-error-page-invalid' )->text();
356363
}

0 commit comments

Comments
 (0)