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

Commit 9ae3b7b

Browse files
author
mikesamuel
committed
language handler for WikiText
1 parent 73ff5b7 commit 9ae3b7b

File tree

5 files changed

+197
-53
lines changed

5 files changed

+197
-53
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 and Haskell</li>
62+
<li>Language handlers for Visual Basic, Haskell, and WikiText</li>
6363
</ul>
6464
</body>
6565
</html>

README.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ <h2>FAQ</h2>
6565
<h3>Which languages does it work for?</h3>
6666
<p>The comments in <tt>prettify.js</tt> are authoritative but the lexer
6767
should work on a number of languages including C and friends,
68-
Java, Python, Bash, VB, SQL, HTML, XML, CSS, Javascript, and Makefiles.
69-
It works passably on Ruby, PHP and Awk and a decent subset of Perl, but,
70-
because of commenting conventions, doesn't work on Smalltalk, or
71-
CAML-like languages.</p>
68+
Java, Python, Bash, SQL, HTML, XML, CSS, Javascript, and Makefiles.
69+
It works passably on Ruby, PHP, VB, and Awk and a decent subset of Perl
70+
and Ruby, but, because of commenting conventions, doesn't work on
71+
Smalltalk, or CAML-like languages.</p>
7272

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"
@@ -80,9 +80,11 @@ <h3>Which languages does it work for?</h3>
8080
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-vb.js"
8181
><code>Visual Basic</code></a>,
8282
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-sql.js"
83-
><code>SQL</code></a>, and
83+
><code>SQL</code></a>,
8484
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-proto.js"
85-
><code>Protocol Buffers</code></a>.
85+
><code>Protocol Buffers</code></a>, and
86+
<a href="http://code.google.com/p/google-code-prettify/source/browse/trunk/src/lang-wiki.js"
87+
><code>WikiText</code></a>..
8688

8789
<p>If you'd like to add an extension for your favorite language, please
8890
look at <tt>src/lang-lisp.js</tt> and file an

src/lang-wiki.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
* @fileoverview
18+
* Registers a language handler for Wiki pages.
19+
*
20+
* Based on WikiSyntax at http://code.google.com/p/support/wiki/WikiSyntax
21+
*
22+
23+
*/
24+
25+
PR.registerLangHandler(
26+
PR.createSimpleLexer(
27+
[
28+
// Whitespace
29+
[PR.PR_PLAIN, /^[\t\n\r \xA0a-gi-z0-9]+/, null,
30+
'\t\n\r \xA0abcdefgijklmnopqrstuvwxyz0123456789'],
31+
// Wiki formatting
32+
[PR.PR_PUNCTUATION, /^[=*~\^\[\]]+/, null, '=*~^[]']
33+
],
34+
[
35+
// Meta-info like #summary, #labels, etc.
36+
[PR.PR_KEYWORD, /^#[a-z]+\b/, /(?:^|[\r\n])$/],
37+
// A WikiWord
38+
[PR.PR_LITERAL, /^(?:[A-Z][a-z][a-z0-9]+[A-Z][a-z][a-zA-Z0-9]+)\b/
39+
],
40+
// A preformatted block in an unknown language
41+
['lang-', /^\{\{\{([\s\S]*?)\}\}\}/],
42+
// A block of source code in an unknown language
43+
['lang-', /^`([^\r\n`]+)`/],
44+
// An inline URL.
45+
[PR.PR_STRING,
46+
/^https?:\/\/[^/?#\s]*(?:\/[^?#\s]*)?(?:\?[^#\s]*)?(?:#\S*)?/i],
47+
[PR.PR_PLAIN, /^[\s\S][^#=*~^A-Zh\{`\[]+/]
48+
]),
49+
['wiki']);

src/prettify.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,13 @@ function _pr_isIE6() {
519519
*
520520
* The stylePatterns is a list whose elements have the form
521521
* [style : string, pattern : RegExp, context : RegExp, shortcut : string].
522-
&
523-
* Style is a style constant like PR_PLAIN.
522+
*
523+
* Style is a style constant like PR_PLAIN, or can be a string of the
524+
* form 'lang-FOO', where FOO is a language extension describing the
525+
* language of the portion of the token in $1 after pattern executes.
526+
* E.g., if style is 'lang-lisp', and group 1 contains the text
527+
* '(hello (world))', then that portion of the token will be passed to the
528+
* registered lisp handler for formatting.
524529
*
525530
* Pattern must only match prefixes, and if it matches a prefix and context
526531
* is null or matches the last non-comment token parsed, then that match is
@@ -598,7 +603,32 @@ function _pr_isIE6() {
598603
}
599604
}
600605

601-
decorations.push(opt_basePos + pos, style);
606+
if (!match || !match[1] || 'lang-' !== style.substring(0, 5)) {
607+
decorations.push(opt_basePos + pos, style);
608+
} else { // Treat group 1 as an embedded block of source code.
609+
var lang = style.substring(5);
610+
var embeddedSource = match[1];
611+
var embeddedSourceStart = token.indexOf(embeddedSource);
612+
var embeddedSourceEnd = embeddedSourceStart + embeddedSource.length;
613+
if (embeddedSourceStart) {
614+
decorations.push(opt_basePos + pos, PR_SOURCE);
615+
}
616+
if (!langHandlerRegistry.hasOwnProperty(lang)) {
617+
lang = /^\s*</.test(embeddedSource)
618+
? 'default-markup'
619+
: 'default-code'
620+
}
621+
var delegate = langHandlerRegistry[lang];
622+
var embeddedOffset = opt_basePos + pos + embeddedSourceStart;
623+
var embeddedDecorations = delegate.call({}, embeddedSource);
624+
for (var i = 0, n = embeddedDecorations.length; i < n; i += 2) {
625+
decorations.push(embeddedOffset + embeddedDecorations[i],
626+
embeddedDecorations[i + 1]);
627+
}
628+
if (embeddedSourceEnd < token.length) {
629+
decorations.push(opt_basePos + pos + embeddedSourceEnd, PR_SOURCE);
630+
}
631+
}
602632
pos += token.length;
603633
tail = tail.substring(token.length);
604634
if (style !== PR_COMMENT && notWs.test(token)) { lastToken = token; }
@@ -969,7 +999,7 @@ function _pr_isIE6() {
969999
var newlineRe = /\r\n?|\n/g;
9701000
var trailingSpaceRe = /[ \r\n]$/;
9711001
var lastWasSpace = true; // the last text chunk emitted ended with a space.
972-
1002+
9731003
// A helper function that is responsible for opening sections of decoration
9741004
// and outputing properly escaped chunks of source
9751005
function emitTextUpTo(sourceIdx) {

tests/prettify_test.html

Lines changed: 105 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
onerror="alert('Error: failed to load ' + this.src)"></script>
1717
<script src="../src/lang-sql.js" type="text/javascript"
1818
onerror="alert('Error: failed to load ' + this.src)"></script>
19+
<script src="../src/lang-wiki.js" type="text/javascript"
20+
onerror="alert('Error: failed to load ' + this.src)"></script>
1921
<script src="../src/lang-vb.js" type="text/javascript"
2022
onerror="alert('Error: failed to load ' + this.src)"></script>
2123
<script type="text/javascript">
@@ -803,46 +805,6 @@ <h1>Bug 24 - LUA Syntax Highlighting</h1>
803805
return shellsort
804806
end</pre>
805807

806-
<h1>Bug 42 - Lisp Syntax Highlighting</h1>
807-
<pre class="prettyprint lang-el" id="issue42"
808-
>; -*- mode: lisp -*-
809-
810-
(defun back-six-lines () (interactive) (forward-line -6))
811-
(defun forward-six-lines () (interactive) (forward-line 6))
812-
813-
(global-set-key "\M-l" 'goto-line)
814-
(global-set-key "\C-z" 'advertised-undo)
815-
(global-set-key [C-insert] 'clipboard-kill-ring-save)
816-
(global-set-key [S-insert] 'clipboard-yank)
817-
(global-set-key [C-up] 'back-six-lines)
818-
(global-set-key [C-down] 'forward-six-lines)
819-
820-
(setq visible-bell t)
821-
(setq user-mail-address "[email protected]")
822-
(setq default-major-mode 'text-mode)
823-
824-
(setenv "TERM" "emacs")
825-
(c-set-offset 'case-label 2)
826-
(setq c-basic-offset 2)
827-
(setq perl-indent-level 2)
828-
(setq delete-key-deletes-forward t)
829-
(setq indent-tabs-mode nil)
830-
831-
;; Text mode
832-
(add-hook 'text-mode-hook
833-
'(lambda ()
834-
(turn-on-auto-fill)
835-
)
836-
)
837-
838-
;; Fundamental mode
839-
(add-hook 'fundamental-mode-hook
840-
'(lambda ()
841-
(turn-on-auto-fill)
842-
)
843-
)
844-
</pre>
845-
846808
<h1>Bug 27 - VBScript w/ language specified</h1>
847809
<pre class="prettyprint lang-vb" id="issue27">
848810
Imports System
@@ -911,7 +873,7 @@ <h1>Bug 30 - Haskell w/ language specified</h1>
911873
Nil :: Foo a b c
912874
Cons :: a b c -> Foo abc -> Foo a b c
913875

914-
str = "Foo\\Bar"
876+
str = &quot;Foo\\Bar"
915877
char = 'x'
916878
Not.A.Char = 'too long' -- Don't barf. Show that 't is a lexical error.
917879

@@ -940,6 +902,46 @@ <h1>Bug 33 - OCaml and F#</h1>
940902
</pre>
941903
<p>Still TODO: handle nested <code>(* (* comments *) *)</code> properly.</p>
942904

905+
<h1>Bug 42 - Lisp Syntax Highlighting</h1>
906+
<pre class="prettyprint lang-el" id="issue42"
907+
>; -*- mode: lisp -*-
908+
909+
(defun back-six-lines () (interactive) (forward-line -6))
910+
(defun forward-six-lines () (interactive) (forward-line 6))
911+
912+
(global-set-key "\M-l" 'goto-line)
913+
(global-set-key "\C-z" 'advertised-undo)
914+
(global-set-key [C-insert] 'clipboard-kill-ring-save)
915+
(global-set-key [S-insert] 'clipboard-yank)
916+
(global-set-key [C-up] 'back-six-lines)
917+
(global-set-key [C-down] 'forward-six-lines)
918+
919+
(setq visible-bell t)
920+
(setq user-mail-address "[email protected]")
921+
(setq default-major-mode 'text-mode)
922+
923+
(setenv "TERM" "emacs")
924+
(c-set-offset 'case-label 2)
925+
(setq c-basic-offset 2)
926+
(setq perl-indent-level 2)
927+
(setq delete-key-deletes-forward t)
928+
(setq indent-tabs-mode nil)
929+
930+
;; Text mode
931+
(add-hook 'text-mode-hook
932+
'(lambda ()
933+
(turn-on-auto-fill)
934+
)
935+
)
936+
937+
;; Fundamental mode
938+
(add-hook 'fundamental-mode-hook
939+
'(lambda ()
940+
(turn-on-auto-fill)
941+
)
942+
)
943+
</pre>
944+
943945
<h1>Bug 45 - Square brackets in strings</h1>
944946
<pre class="prettyprint" id="issue45">
945947
throw new RuntimeException("Element [" + element.getName() +
@@ -965,6 +967,32 @@ <h1>Protocol Buffers</h1>
965967
optional Corpus corpus = 4 [default = UNIVERSAL];
966968
}</pre>
967969

970+
<h1>Wiki syntax w/ language specified</h1>
971+
<pre class="prettyprint lang-wiki" id="wiki">
972+
#summary hello world
973+
#labels HelloWorld WikiWord Hiya
974+
975+
[http://www.google.com/?q=WikiSyntax+site:code.google.com WikiSyntax]
976+
977+
Lorem Ipsum `while (1) print("blah blah");`
978+
979+
* Bullet
980+
* Points
981+
* NestedBullet
982+
983+
==DroningOnAndOn==
984+
{{{
985+
// Some EmbeddedSourceCode
986+
void main() {
987+
Print('hello world');
988+
}
989+
}}}
990+
991+
{{{
992+
&lt;!-- Embedded XML --&gt;
993+
&lt;foo bar="baz"&gt;&lt;boo /&gt;&lt;foo&gt;
994+
}}}
995+
</pre>
968996
</body>
969997

970998
<script type="text/javascript">
@@ -2244,7 +2272,42 @@ <h1>Protocol Buffers</h1>
22442272
'&nbsp; `END`KWDoptional`END`PLN `END`TYPCorpus`END`PLN corpus `END' +
22452273
'`PUN=`END`PLN `END`LIT4`END`PLN `END`PUN[`END`KWDdefault`END' +
22462274
'`PLN `END`PUN=`END`PLN UNIVERSAL`END`PUN];`END`PLN<br>' +
2247-
'`END`PUN}`END')
2275+
'`END`PUN}`END'),
2276+
wiki: (
2277+
'`KWD#summary`END`PLN hello world<br>' +
2278+
'`END`KWD#labels`END`PLN `END`LITHelloWorld`END`PLN `END' +
2279+
'`LITWikiWord`END`PLN Hiya<br>' +
2280+
'<br>' +
2281+
'`END`PUN[`END' +
2282+
'`STRhttp://www.google.com/?q=WikiSyntax+site:code.google.com`END' +
2283+
'`PLN `END`LITWikiSyntax`END`PUN]`END`PLN<br>' +
2284+
'<br>' +
2285+
'Lorem Ipsum `END`SRC``END`KWDwhile`END`PLN `END`PUN(`END`LIT1`END' +
2286+
'`PUN)`END`PLN `END`KWDprint`END`PUN(`END`STR"blah blah"`END' +
2287+
'`PUN);`END`SRC``END`PLN<br>' +
2288+
'<br>' +
2289+
'&nbsp; &nbsp;`END`PUN*`END`PLN Bullet<br>' +
2290+
'&nbsp; &nbsp;`END`PUN*`END`PLN Points<br>' +
2291+
'&nbsp; &nbsp; &nbsp; `END`PUN*`END`PLN `END`LITNestedBullet`END' +
2292+
'`PLN<br>' +
2293+
'<br>' +
2294+
'`END`PUN==`END`LITDroningOnAndOn`END`PUN==`END`PLN<br>' +
2295+
'`END`SRC{{{`END`PLN<br>' +
2296+
'&nbsp; `END`COM// Some EmbeddedSourceCode`END`PLN<br>' +
2297+
'&nbsp; `END`KWDvoid`END`PLN main`END`PUN()`END`PLN `END`PUN{`END' +
2298+
'`PLN<br>' +
2299+
'&nbsp; &nbsp; `END`TYPPrint`END`PUN(`END`STR\'hello world\'`END' +
2300+
'`PUN);`END`PLN<br>' +
2301+
'&nbsp; `END`PUN}`END`PLN<br>' +
2302+
'`END`SRC}}}`END`PLN<br>' +
2303+
'<br>' +
2304+
'`END`SRC{{{`END`PLN<br>' +
2305+
'&nbsp; `END`COM&lt;!-- Embedded XML --&gt;`END`PLN<br>' +
2306+
'&nbsp; `END`PUN&lt;`END`TAGfoo`END`PLN `END`ATNbar`END`PUN=`END' +
2307+
'`ATV"baz"`END`PUN&gt;&lt;`END`TAGboo`END`PLN `END' +
2308+
'`PUN/&gt;&lt;`END`TAGfoo`END`PUN&gt;`END`PLN<br>' +
2309+
'`END`SRC}}}`END'
2310+
)
22482311
};
22492312

22502313

0 commit comments

Comments
 (0)