Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions components/prism-scss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Prism.languages.scss = Prism.languages.extend('css', {
'comment': {
pattern: /(^|[^\\])(\/\*[\w\W]*?\*\/|\/\/.*?(\r?\n|$))/g,
lookbehind: true
},
// aturle is just the @***, not the entire rule (to highlight var & stuffs)
// + add ability to highlight number & unit for media queries
'atrule': /@[\w-]+(?=\s+(\(|\{|;))/gi,
// url, compassified
'url': /([-a-z]+-)*url(?=\()/gi,
// CSS selector regex is not appropriate for Sass
// since there can be lot more things (var, @ directive, nesting..)
// a selector must start at the end of a property or after a brace (end of other rules or nesting)
// it can contain some caracters that aren't used for defining rules or end of selector, & (parent selector), or interpolated variable
// the end of a selector is found when there is no rules in it ( {} or {\s}) or if there is a property (because an interpolated var
// can "pass" as a selector- e.g: proper#{$erty})
// this one was ard to do, so please be careful if you edit this one :)
'selector': /([^@;:\{\}\(\)]?([^@;:\{\}\(\)]|&|\#\{\$[-_\w]+\})+)(?=\s*\{(\}|\s|[^\}]+(:|\{)[^\}]+))/gm
});

Prism.languages.insertBefore('scss', 'atrule', {
'keyword': /@(if|else if|else|for|each|while|import|extend|debug|warn|mixin|include|function|return)|(?=@for\s+\$[-_\w]+\s)+from/i
});

Prism.languages.insertBefore('scss', 'property', {
'variable': /\$[-_\w]+/i
});

Prism.languages.insertBefore('scss', 'ignore', {
'placeholder': /%[-_\w]+/i,
'statement': /\B!(default|optional)\b/gi,
'boolean': /\b(true|false)\b/g,
'null': /\b(null)\b/g,
'operator': /\s+([-+]{1,2}|={1,2}|!=|\|?\||\?|\*|\/|\%)\s+/g
});
1 change: 1 addition & 0 deletions components/prism-scss.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 134 additions & 0 deletions examples.html
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,140 @@ <h2>Simple class example</h2>
<p>As you can notice String keyword is not highlighted because it's not a Java language keyword (cf. <a href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html">Java Language Keywords</a>). The main reason is that String is not a primitive type such as <em>int</em> but a class type like <em>Integer</em>.</p>
</section>

<section class="language-scss">
<h1>Sass (Scss)</h2>

<h2>Comments</h2>
<pre><code>// This is a simple one line comment</code></pre>

<h2>Variables</h2>
<pre><code>$var: value;
$bool: true;
$nullValue: null;
$interpolation: -vendor-#{$var};</code></pre>

<h2>Nesting</h2>
<pre><code>sel {
ector {
#chi,
ld .ren { } inline {} * {}
}

&.parentSelector {
nested & test #{$inter}pol {}
}
}</code></pre>

<h2>Directives</h2>
<pre><code>@import "compass";

.class {
@extend %placeholder;
}

@debug $var;

@warn "This is a Warning in the console";</code></pre>

<h2>Keywords</h2>
<pre><code>@mixin super-mixin() {
@include great-mixin();
}

@function fn() {
@return value;
}
</code></pre>

<h2>URLs Functions (Compass Style)</h2>
<pre><code>selector {
background: image-url('stuff.png');
}

@font-face {
src: font-url('font.woff');
}</code></pre>

<h2>Statements</h2>
<pre><code>$var: value !default;

selector {
@extend %stuff !optional;
}</code></pre>

<h2>Controls</h2>
<pre><code>@if(true) {
// ...
}
@else if {
// ...
}
@else {
// ...
}

@for $i from 1 through 3 {
// ...
}

@each $animal in puma, sea-slug, egret, salamander {
// ...
}

$i: 2;
@while $i > 0 {
//...
$i: $i - 1; // note the operator
$i: 3 * 2 + $i % 5;
}</code></pre>

<h2>Scss full example</h2>
<pre><code>@import "compass";

@mixin mixin($param) {
@if($param==true) {
selector-#{$test}-stuff,
sel2 {
property: -moz-#{$param};
aze: rty;

child {
test: $param * 3;
a: bcd;
color: #f45f55;
}
}
}
@else {
property: $param + #aaa;
test: stupid;
}
}

$i: 2;
@while $i > 0 {
sel#{$i}: $i * 2
}

selector {
@media (min-width: 800px) {
stuff: $thing;
}
}</code></pre>

<p>
This Scss syntax can be used just for CSS too since regular CSS is valid Scss too.
Highlight renders better with an appropriate theme (to customize specific tokens).
</p>
<h2>Known issues</h2>
<ul>
<li>&amp; keyword and interpolated variables are not highlighted in selectors.</li>
<li>Some control directives parameters are parsed as selectors.</li>
<li>Does not work very well with indented Sass syntax. That's why it's just called "scss".</li>
</ul>
<p>If you are good with regex, maybe you can help to improve this.</p>
</section>

<section id="failures" class="language-javascript">
<h1>Known failures (JavaScript)</h1>
<p>There are certain edge cases where Prism will fail.
Expand Down
40 changes: 40 additions & 0 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,3 +492,43 @@ Prism.languages.java = Prism.languages.extend('clike', {
lookbehind: true
}
});

/* **********************************************
Begin prism-scss.js
********************************************** */

Prism.languages.scss = Prism.languages.extend('css', {
'comment': {
pattern: /(^|[^\\])(\/\*[\w\W]*?\*\/|\/\/.*?(\r?\n|$))/g,
lookbehind: true
},
// aturle is just the @***, not the entire rule (to highlight var & stuffs)
// + add ability to highlight number & unit for media queries
'atrule': /@[\w-]+(?=\s+(\(|\{|;))/gi,
// url, compassified
'url': /([-a-z]+-)*url(?=\()/gi,
// CSS selector regex is not appropriate for Sass
// since there can be lot more things (var, @ directive, nesting..)
// a selector must start at the end of a property or after a brace (end of other rules or nesting)
// it can contain some caracters that aren't used for defining rules or end of selector, & (parent selector), or interpolated variable
// the end of a selector is found when there is no rules in it ( {} or {\s}) or if there is a property (because an interpolated var
// can "pass" as a selector- e.g: proper#{$erty})
// this one was ard to do, so please be careful if you edit this one :)
'selector': /([^@;:\{\}\(\)]?([^@;:\{\}\(\)]|&amp;|\#\{\$[-_\w]+\})+)(?=\s*\{(\}|\s|[^\}]+(:|\{)[^\}]+))/gm
});

Prism.languages.insertBefore('scss', 'atrule', {
'keyword': /@(if|else if|else|for|each|while|import|extend|debug|warn|mixin|include|function|return)|(?=@for\s+\$[-_\w]+\s)+from/i
});

Prism.languages.insertBefore('scss', 'property', {
'variable': /\$[-_\w]+/i
});

Prism.languages.insertBefore('scss', 'ignore', {
'placeholder': /%[-_\w]+/i,
'statement': /\B!(default|optional)\b/gi,
'boolean': /\b(true|false)\b/g,
'null': /\b(null)\b/g,
'operator': /\s+([-+]{1,2}|={1,2}|!=|\|?\||\?|\*|\/|\%)\s+/g
});