Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit 325728e

Browse files
author
mikesamuel
committed
Language handler for CSS
1 parent fa9804d commit 325728e

File tree

5 files changed

+140
-9
lines changed

5 files changed

+140
-9
lines changed

CHANGES.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ <h2>14 Jul 2008</h2>
5959
</ul>
6060
<h2>6 Jan 2009</h2>
6161
<ul>
62-
<li>Language handlers for Visual Basic, Haskell, and WikiText</li>
62+
<li>Language handlers for Visual Basic, Haskell, CSS, and WikiText</li>
6363
<li>Added <tt>.mxml</tt> extension to the markup style handler for
6464
Flex <a href="http://en.wikipedia.org/wiki/MXML">MXML files</a>. See
6565
<a

README.html

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,22 @@ <h3>Which languages does it work for?</h3>
7373
<p>LISPy languages are supported via an extension:
7474
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-lisp.js"
7575
><code>lang-lisp.js</code></a>.</p>
76-
<p>And similarly for <a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-hs"
77-
><code>Haskell</code></a>, <a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-lua.js"
78-
><code>LUA</code></a>, <a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-ml.js"
76+
<p>And similarly for
77+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-css"
78+
><code>CSS</code></a>,
79+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-hs"
80+
><code>Haskell</code></a>,
81+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-lua.js"
82+
><code>LUA</code></a>,
83+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-ml.js"
7984
><code>OCAML, SML, F#</code></a>,
80-
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-vb.js"
85+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-vb.js"
8186
><code>Visual Basic</code></a>,
82-
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-sql.js"
87+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-sql.js"
8388
><code>SQL</code></a>,
84-
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-proto.js"
89+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-proto.js"
8590
><code>Protocol Buffers</code></a>, and
86-
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-wiki.js"
91+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-wiki.js"
8792
><code>WikiText</code></a>..
8893

8994
<p>If you'd like to add an extension for your favorite language, please

src/lang-css.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (C) 2009 Google Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
17+
/**
18+
* @fileoverview
19+
* Registers a language handler for CSS.
20+
*
21+
*
22+
* To use, include prettify.js and this file in your HTML page.
23+
* Then put your code in an HTML tag like
24+
* <pre class="prettyprint lang-css"></pre>
25+
*
26+
*
27+
* http://www.w3.org/TR/CSS21/grammar.html Section G2 defines the lexical
28+
* grammar. This scheme does not recognize keywords containing escapes.
29+
*
30+
31+
*/
32+
33+
PR.registerLangHandler(
34+
PR.createSimpleLexer(
35+
[
36+
// The space production <s>
37+
[PR.PR_PLAIN, /^[ \t\r\n\f]+/, null, ' \t\r\n\f']
38+
],
39+
[
40+
// Quoted strings. <string1> and <string2>
41+
[PR.PR_STRING,
42+
/^\"(?:[^\n\r\f\\\"]|\\(?:\r\n?|\n|\f)|\\[\s\S])*\"/, null],
43+
[PR.PR_STRING,
44+
/^\'(?:[^\n\r\f\\\']|\\(?:\r\n?|\n|\f)|\\[\s\S])*\'/, null],
45+
[PR.PR_STRING, /^\([^\)\"\']*\)/, /\burl/i],
46+
[PR.PR_KEYWORD,
47+
/^(?:url|rgb|\!important|@import|@page|@media|@charset|inherit)(?=[^\-\w]|$)/i,
48+
null],
49+
// A property name -- an identifier followed by a colon.
50+
[PR.PR_KEYWORD, /^-?(?:[_a-z]|(?:\\[0-9a-f]+ ?))(?:[_a-z0-9\-]|\\(?:\\[0-9a-f]+ ?))*(?=\s*:)/i],
51+
// A C style block comment. The <comment> production.
52+
[PR.PR_COMMENT, /^\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\//],
53+
// Escaping text spans
54+
[PR.PR_COMMENT, /^(?:<!--|-->)/],
55+
// A number possibly containing a suffix.
56+
[PR.PR_LITERAL, /^(?:\d+|\d*\.\d+)(?:%|[a-z]+)?/i],
57+
// A hex color
58+
[PR.PR_LITERAL, /^#(?:[0-9a-f]{3}){1,2}/i],
59+
// An identifier
60+
[PR.PR_PLAIN,
61+
/^-?(?:[_a-z]|(?:\\[\da-f]+ ?))(?:[_a-z\d\-]|\\(?:\\[\da-f]+ ?))*/i],
62+
// A run of punctuation
63+
[PR.PR_PUNCTUATION, /^[^\s\w\'\"]+/]
64+
]),
65+
['css']);

src/lang-vb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ PR.registerLangHandler(
5555
// A run of punctuation
5656
[PR.PR_PUNCTUATION,
5757
/^[^\w\t\n\r \"\'\[\]\xA0\u2018\u2019\u201C\u201D\u2028\u2029]+/],
58-
// A run of punctuation
58+
// Square brackets
5959
[PR.PR_PUNCTUATION, /^(?:\[|\])/]
6060
]),
6161
['vb', 'vbs']);

tests/prettify_test.html

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<title>Code Prettifier</title>
55
<script src="../src/prettify.js" type="text/javascript"
66
onerror="alert('Error: failed to load ' + this.src)"></script>
7+
<script src="../src/lang-css.js" type="text/javascript"
8+
onerror="alert('Error: failed to load ' + this.src)"></script>
79
<script src="../src/lang-hs.js" type="text/javascript"
810
onerror="alert('Error: failed to load ' + this.src)"></script>
911
<script src="../src/lang-lisp.js" type="text/javascript"
@@ -993,6 +995,29 @@ <h1>Wiki syntax w/ language specified</h1>
993995
&lt;foo bar="baz"&gt;&lt;boo /&gt;&lt;foo&gt;
994996
}}}
995997
</pre>
998+
999+
<h1>CSS w/ language specified</h1>
1000+
<pre class="prettyprint lang-css" id="css">
1001+
&lt;!--
1002+
@charset('UTF-8');
1003+
1004+
/** A url that is not quoted. */
1005+
@import(url(/more-styles.css));
1006+
1007+
HTML { content-before: 'hello\20'; content-after: 'w\6f rld';
1008+
-moz-spiff: inherit !important }
1009+
1010+
/* Test units on numbers. */
1011+
BODY { margin-bottom: 4px; margin-left: 3in; margin-bottom: 0; margin-top: 5% }
1012+
1013+
/** Test number literals and quoted values. */
1014+
TABLE.foo TR.bar A#visited { color: #001123; font-family: "monospace" }
1015+
/** bolder is not a name, so should be plain. !IMPORTANT is a keyword
1016+
* regardless of case.
1017+
*/
1018+
blink { text-decoration: BLINK !IMPORTANT; font-weight: bolder }
1019+
--&gt;
1020+
</pre>
9961021
</body>
9971022

9981023
<script type="text/javascript">
@@ -2307,6 +2332,42 @@ <h1>Wiki syntax w/ language specified</h1>
23072332
'`ATV"baz"`END`PUN&gt;&lt;`END`TAGboo`END`PLN `END' +
23082333
'`PUN/&gt;&lt;`END`TAGfoo`END`PUN&gt;`END`PLN<br>' +
23092334
'`END`SRC}}}`END'
2335+
),
2336+
css: (
2337+
'`COM&lt;!--`END`PLN<br>' +
2338+
'`END`KWD@charset`END`PUN(`END`STR\'UTF-8\'`END`PUN);`END`PLN<br>' +
2339+
'<br>' +
2340+
'`END`COM/** A url that is not quoted. */`END`PLN<br>' +
2341+
'`END`KWD@import`END`PUN(`END`KWDurl`END`STR(/more-styles.css)`END' +
2342+
'`PUN);`END`PLN<br>' +
2343+
'<br>' +
2344+
'HTML `END`PUN{`END`PLN `END`KWDcontent-before`END`PUN:`END`PLN `END' +
2345+
'`STR\'hello\20\'`END`PUN;`END`PLN `END`KWDcontent-after`END' +
2346+
'`PUN:`END`PLN `END`STR\'w\6f rld\'`END`PUN;`END`PLN<br>' +
2347+
'&nbsp; &nbsp; &nbsp; &nbsp;`END`KWD-moz-spiff`END`PUN:`END`PLN `END' +
2348+
'`KWDinherit`END`PLN `END`KWD!important`END`PLN `END`PUN}`END' +
2349+
'`PLN<br>' +
2350+
'<br>' +
2351+
'`END`COM/* Test units on numbers. */`END`PLN<br>' +
2352+
'BODY `END`PUN{`END`PLN `END`KWDmargin-bottom`END`PUN:`END`PLN `END' +
2353+
'`LIT4px`END`PUN;`END`PLN `END`KWDmargin-left`END`PUN:`END' +
2354+
'`PLN `END`LIT3in`END`PUN;`END`PLN `END`KWDmargin-bottom`END' +
2355+
'`PUN:`END`PLN `END`LIT0`END`PUN;`END`PLN `END`KWDmargin-top`END' +
2356+
'`PUN:`END`PLN `END`LIT5%`END`PLN `END`PUN}`END`PLN<br>' +
2357+
'<br>' +
2358+
'`END`COM/** Test number literals and quoted values. */`END`PLN<br>' +
2359+
'TABLE`END`PUN.`END`PLNfoo TR`END`PUN.`END`PLNbar A`END`PUN#`END' +
2360+
'`PLNvisited `END`PUN{`END`PLN `END`KWDcolor`END`PUN:`END`PLN `END' +
2361+
'`LIT#001123`END`PUN;`END`PLN `END`KWDfont-family`END`PUN:`END' +
2362+
'`PLN `END`STR"monospace"`END`PLN `END`PUN}`END`PLN<br>' +
2363+
'`END`COM/** bolder is not a name, so should be plain. ' +
2364+
'&nbsp;!IMPORTANT is a keyword<br>' +
2365+
'&nbsp; * regardless of case.<br>' +
2366+
'&nbsp; */`END`PLN<br>' +
2367+
'blink `END`PUN{`END`PLN `END`KWDtext-decoration`END`PUN:`END' +
2368+
'`PLN BLINK `END`KWD!IMPORTANT`END`PUN;`END`PLN `END' +
2369+
'`KWDfont-weight`END`PUN:`END`PLN bolder `END`PUN}`END`PLN<br>' +
2370+
'`END`COM--&gt;`END'
23102371
)
23112372
};
23122373

0 commit comments

Comments
 (0)