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

Commit 22a1d7d

Browse files
author
mikesamuel@gmail.com
committed
added language handler for TCL from patch in issue 256
1 parent a7cc6fe commit 22a1d7d

3 files changed

Lines changed: 98 additions & 1 deletion

File tree

CHANGES.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,9 @@ <h2>29 March 2011</h2>
151151
vulnerability. If you sanitize and prettify HTML from an
152152
untrusted source, sanitize first.
153153
</ul>
154+
<h2>4 February 2013</h2>
155+
<ul>
156+
<li>Language handlers for Dart, TCL.</li>
157+
</ul>
154158
</body>
155159
</html>

src/lang-tcl.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (C) 2012 Pyrios.
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 TCL
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-tcl">proc foo {} {puts bar}</pre>
25+
*
26+
* I copy-pasted lang-lisp.js, so this is probably not 100% accurate.
27+
* I used http://wiki.tcl.tk/1019 for the keywords, but tried to only
28+
* include as keywords that had more impact on the program flow
29+
* rather than providing convenience. For example, I included 'if'
30+
* since that provides branching, but left off 'open' since that is more
31+
* like a proc. Add more if it makes sense.
32+
*
33+
* @author pyrios@gmail.com
34+
*/
35+
36+
PR['registerLangHandler'](
37+
PR['createSimpleLexer'](
38+
[
39+
['opn', /^\{+/, null, '{'],
40+
['clo', /^\}+/, null, '}'],
41+
// A line comment that starts with ;
42+
[PR['PR_COMMENT'], /^#[^\r\n]*/, null, '#'],
43+
// Whitespace
44+
[PR['PR_PLAIN'], /^[\t\n\r \xA0]+/, null, '\t\n\r \xA0'],
45+
// A double quoted, possibly multi-line, string.
46+
[PR['PR_STRING'], /^\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)/, null, '"']
47+
],
48+
[
49+
[PR['PR_KEYWORD'], /^(?:after|append|apply|array|break|case|catch|continue|error|eval|exec|exit|expr|for|foreach|if|incr|info|proc|return|set|switch|trace|uplevel|upvar|while)\b/, null],
50+
[PR['PR_LITERAL'],
51+
/^[+\-]?(?:[0#]x[0-9a-f]+|\d+\/\d+|(?:\.\d+|\d+(?:\.\d*)?)(?:[ed][+\-]?\d+)?)/i],
52+
// A single quote possibly followed by a word that optionally ends with
53+
// = ! or ?.
54+
[PR['PR_LITERAL'],
55+
/^\'(?:-*(?:\w|\\[\x21-\x7e])(?:[\w-]*|\\[\x21-\x7e])[=!?]?)?/],
56+
// A word that optionally ends with = ! or ?.
57+
[PR['PR_PLAIN'],
58+
/^-*(?:[a-z_]|\\[\x21-\x7e])(?:[\w-]*|\\[\x21-\x7e])[=!?]?/i],
59+
// A printable non-space non-special character
60+
[PR['PR_PUNCTUATION'], /^[^\w\t\n\r \xA0()\"\\\';]+/]
61+
]),
62+
['tcl']);

tests/prettify_test_2.html

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"lang-lisp.js",
1414
"lang-xq.js",
1515
"lang-n.js",
16+
"lang-tcl.js",
1617
"lang-tex.js"
1718
];
1819
var styles = [
@@ -538,6 +539,22 @@ <h1>Dart language handler</h1>
538539
}
539540
</pre>
540541

542+
<h1>TCL_lang</h1>
543+
<pre class="prettyprint lang-tcl" id="tcl_lang">#!/bin/tclsh
544+
proc fib {n} {
545+
set a 0
546+
set b 1
547+
while {$n > 0} {
548+
set tmp $a
549+
set a [expr $a + $b]
550+
set b $tmp
551+
incr n -1
552+
}
553+
return $a
554+
}
555+
</pre>
556+
557+
541558
<script type="text/javascript">
542559
/**
543560
* maps ids of rewritten code to the expected output.
@@ -1002,7 +1019,21 @@ <h1>Dart language handler</h1>
10021019
' `END`TYPString`END`PLN raw `END`PUN=`END`PLN r`END' +
10031020
'`STR\"This is a raw string, where \\n doesn\'t matter\"`END' +
10041021
'`PUN;`END`PLN\n' +
1005-
'`END`PUN}`END'
1022+
'`END`PUN}`END',
1023+
tcl_lang: (
1024+
'`COM#!/bin/tclsh`END`PLN\n' +
1025+
'`END`KWDproc`END`PLN fib `END`OPN{`END`PLNn`END`CLO}`END`PLN `END`OPN{`END`PLN\n' +
1026+
' `END`KWDset`END`PLN a `END`LIT0`END`PLN\n' +
1027+
' `END`KWDset`END`PLN b `END`LIT1`END`PLN\n' +
1028+
' `END`KWDwhile`END`PLN `END`OPN{`END`PUN$`END`PLNn `END`PUN&gt;`END`PLN `END`LIT0`END`CLO}`END`PLN `END`OPN{`END`PLN\n' +
1029+
' `END`KWDset`END`PLN tmp `END`PUN$`END`PLNa\n' +
1030+
' `END`KWDset`END`PLN a `END`PUN[`END`KWDexpr`END`PLN `END`PUN$`END`PLNa `END`PUN+`END`PLN `END`PUN$`END`PLNb`END`PUN]`END`PLN\n' +
1031+
' `END`KWDset`END`PLN b `END`PUN$`END`PLNtmp\n' +
1032+
' `END`KWDincr`END`PLN n `END`LIT-1`END`PLN\n' +
1033+
' `END`CLO}`END`PLN\n' +
1034+
' `END`KWDreturn`END`PLN `END`PUN$`END`PLNa\n' +
1035+
'`END`CLO}`END'
1036+
)
10061037
};
10071038
</script>
10081039

0 commit comments

Comments
 (0)