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

Commit 63ced17

Browse files
added Jeremy Wall's Clojure mode
1 parent b498927 commit 63ced17

File tree

3 files changed

+105
-10
lines changed

3 files changed

+105
-10
lines changed

README.html

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ <h3 id="langs">Which languages does it work for?</h3>
7777
<p>And similarly for
7878
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-css.js"
7979
><code>CSS</code></a>,
80+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-clj.js"
81+
><code>Clojure</code></a>,
8082
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-go.js"
8183
><code>Go</code></a>,
8284
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-hs.js"
@@ -85,16 +87,20 @@ <h3 id="langs">Which languages does it work for?</h3>
8587
><code>Lua</code></a>,
8688
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-ml.js"
8789
><code>OCAML, SML, F#</code></a>,
88-
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-vb.js"
89-
><code>Visual Basic</code></a>,
90+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-proto.js"
91+
><code>Protocol Buffers</code></a>,
9092
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-scala.js"
9193
><code>Scala</code></a>,
9294
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-sql.js"
9395
><code>SQL</code></a>,
94-
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-proto.js"
95-
><code>Protocol Buffers</code></a>, and
96+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-vhdl.js"
97+
><code>VHDL</code></a>,
98+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-vb.js"
99+
><code>Visual Basic</code></a>,
96100
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-wiki.js"
97-
><code>WikiText</code></a>..
101+
><code>WikiText</code></a>, and
102+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-yaml.js"
103+
><code>YAML</code></a>.
98104

99105
<p>If you'd like to add an extension for your favorite language, please
100106
look at <tt>src/lang-lisp.js</tt> and file an
@@ -209,9 +215,7 @@ <h3>How can I customize the colors and styles of my code?</h3>
209215

210216
<div class="footer">
211217
<!-- Created: Tue Oct 3 17:51:56 PDT 2006 -->
212-
<!-- hhmts start -->
213-
Last modified: Wed Jul 19 13:56:00 PST 2010
214-
<!-- hhmts end -->
218+
<!-- hhmts start -->Last modified: Fri May 20 12:43:46 PDT 2011 <!-- hhmts end -->
215219
</div>
216220
</body>
217221
</html>

src/lang-clj.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @license Copyright (C) 2011 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* @fileoverview
19+
* Registers a language handler for Clojure.
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-lisp">(my lisp code)</pre>
25+
* The lang-cl class identifies the language as common lisp.
26+
* This file supports the following language extensions:
27+
* lang-clj - Clojure
28+
*
29+
*
30+
* I used lang-lisp.js as the basis for this adding the clojure specific
31+
* keywords and syntax.
32+
*
33+
* "Name" = 'Clojure'
34+
* "Author" = 'Rich Hickey'
35+
* "Version" = '1.2'
36+
* "About" = 'Clojure is a lisp for the jvm with concurrency primitives and a richer set of types.'
37+
*
38+
*
39+
* I used <a href="http://clojure.org/Reference">Clojure.org Reference</a> as
40+
* the basis for the reserved word list.
41+
*
42+
*
43+
44+
*/
45+
46+
PR['registerLangHandler'](
47+
PR['createSimpleLexer'](
48+
[
49+
// clojure has more paren types than minimal lisp.
50+
['opn', /^[\(\{\[]+/, null, '([{'],
51+
['clo', /^[\)\}\]]+/, null, ')]}'],
52+
// A line comment that starts with ;
53+
[PR['PR_COMMENT'], /^;[^\r\n]*/, null, ';'],
54+
// Whitespace
55+
[PR['PR_PLAIN'], /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'],
56+
// A double quoted, possibly multi-line, string.
57+
[PR['PR_STRING'], /^\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)/, null, '"']
58+
],
59+
[
60+
// clojure has a much larger set of keywords
61+
[PR['PR_KEYWORD'], /^(?:def|if|do|let|quote|var|fn|loop|recur|throw|try|monitor-enter|monitor-exit|defmacro|defn|defn-|macroexpand|macroexpand-1|for|doseq|dosync|dotimes|and|or|when|not|assert|doto|proxy|defstruct|first|rest|cons|defprotocol|deftype|defrecord|reify|defmulti|defmethod|meta|with-meta|ns|in-ns|create-ns|import|intern|refer|alias|namespace|resolve|ref|deref|refset|new|set!|memfn|to-array|into-array|aset|gen-class|reduce|map|filter|find|nil?|empty?|hash-map|hash-set|vec|vector|seq|flatten|reverse|assoc|dissoc|list|list?|disj|get|union|difference|intersection|extend|extend-type|extend-protocol|prn)\b/, null],
62+
[PR['PR_TYPE'], /^:[0-9a-zA-Z\-]+/]
63+
]),
64+
['clj']);

tests/prettify_test_2.html

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
1+
<!DOCTYPE HTML PUBLIC "-//W5C//DTD HTML 4.01 Transitional//EN">
22
<html>
33
<head>
44
<title>Code Prettifier</title>
@@ -8,6 +8,8 @@
88
onerror="alert('Error: failed to load ' + this.src)"></script>
99

1010
<!-- Language extensions tested -->
11+
<script src="../src/lang-clj.js" type="text/javascript"
12+
onerror="alert('Error: failed to load ' + this.src)"></script>
1113
<script src="../src/lang-xq.js" type="text/javascript"
1214
onerror="alert('Error: failed to load ' + this.src)"></script>
1315
<script src="../src/lang-n.js" type="text/javascript"
@@ -422,6 +424,20 @@ <h1>Issue 145</h1>
422424
&lt;/script&gt;
423425
</pre>
424426

427+
<h1>Clojure Syntax Highlighting</h1>
428+
<pre class="prettyprint lang-clj" id="clojure">
429+
; Clojure test comment
430+
(ns test
431+
(:gen-class))
432+
433+
(def foo "bar")
434+
(defn bar [arg1 arg2 & args]
435+
"sample function"
436+
(for [arg args]
437+
(prn arg)))
438+
439+
(bar "foo" "bar" "blah" :baz)
440+
</pre>
425441
</body>
426442

427443
<script type="text/javascript">
@@ -806,7 +822,18 @@ <h1>Issue 145</h1>
806822
' printf `END`STR"%x "`END`PLN \\\'$i`END`PUN;`END`PLN\n' +
807823
'`END`KWDdone`END`PUN;`END`PLN\n' +
808824
'echo`END'
809-
)
825+
),
826+
clojure: '`COM; Clojure test comment`END`PLN\n' +
827+
'`END`OPN(`END`KWDns`END`PLN test\n' +
828+
' `END`OPN(`END`TYP:gen-class`END`CLO))`END`PLN\n' +
829+
'\n' +
830+
'`END`OPN(`END`KWDdef`END`PLN foo `END`STR"bar"`END`CLO)`END`PLN\n' +
831+
'`END`OPN(`END`KWDdefn`END`PLN bar `END`OPN[`END`PLNarg1 arg2 &amp; args`END`CLO]`END`PLN\n' +
832+
' `END`STR"sample function"`END`PLN\n' +
833+
' `END`OPN(`END`KWDfor`END`PLN `END`OPN[`END`PLNarg args`END`CLO]`END`PLN\n' +
834+
' `END`OPN(`END`KWDprn`END`PLN arg`END`CLO)))`END`PLN\n' +
835+
'\n' +
836+
'`END`OPN(`END`PLNbar `END`STR"foo"`END`PLN `END`STR"bar"`END`PLN `END`STR"blah"`END`PLN `END`TYP:baz`END`CLO)`END'
810837
};
811838
</script>
812839

0 commit comments

Comments
 (0)