annotate vendor/assets/javascripts/prettify.js @ 388:797e351b2e2b

Remove alerts and title from show. Already shown by layout.
author nanaya <me@myconan.net>
date Sun, 07 Jun 2015 10:33:17 +0900
parents 183e8560a337
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1 // Copyright (C) 2006 Google Inc.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
2 //
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
3 // Licensed under the Apache License, Version 2.0 (the "License");
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
4 // you may not use this file except in compliance with the License.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
5 // You may obtain a copy of the License at
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
6 //
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
7 // http://www.apache.org/licenses/LICENSE-2.0
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
8 //
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
9 // Unless required by applicable law or agreed to in writing, software
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
10 // distributed under the License is distributed on an "AS IS" BASIS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
12 // See the License for the specific language governing permissions and
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
13 // limitations under the License.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
14
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
15
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
16 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
17 * @fileoverview
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
18 * some functions for browser-side pretty printing of code contained in html.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
19 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
20 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
21 * For a fairly comprehensive set of languages see the
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
22 * <a href="http://google-code-prettify.googlecode.com/svn/trunk/README.html#langs">README</a>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
23 * file that came with this source. At a minimum, the lexer should work on a
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
24 * number of languages including C and friends, Java, Python, Bash, SQL, HTML,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
25 * XML, CSS, Javascript, and Makefiles. It works passably on Ruby, PHP and Awk
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
26 * and a subset of Perl, but, because of commenting conventions, doesn't work on
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
27 * Smalltalk, Lisp-like, or CAML-like languages without an explicit lang class.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
28 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
29 * Usage: <ol>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
30 * <li> include this source file in an html page via
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
31 * {@code <script type="text/javascript" src="/path/to/prettify.js"></script>}
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
32 * <li> define style rules. See the example page for examples.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
33 * <li> mark the {@code <pre>} and {@code <code>} tags in your source with
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
34 * {@code class=prettyprint.}
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
35 * You can also use the (html deprecated) {@code <xmp>} tag, but the pretty
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
36 * printer needs to do more substantial DOM manipulations to support that, so
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
37 * some css styles may not be preserved.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
38 * </ol>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
39 * That's it. I wanted to keep the API as simple as possible, so there's no
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
40 * need to specify which language the code is in, but if you wish, you can add
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
41 * another class to the {@code <pre>} or {@code <code>} element to specify the
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
42 * language, as in {@code <pre class="prettyprint lang-java">}. Any class that
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
43 * starts with "lang-" followed by a file extension, specifies the file type.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
44 * See the "lang-*.js" files in this directory for code that implements
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
45 * per-language file handlers.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
46 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
47 * Change log:<br>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
48 * cbeust, 2006/08/22
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
49 * <blockquote>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
50 * Java annotations (start with "@") are now captured as literals ("lit")
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
51 * </blockquote>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
52 * @requires console
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
53 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
54
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
55 // JSLint declarations
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
56 /*global console, document, navigator, setTimeout, window, define */
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
57
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
58 /** @define {boolean} */
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
59 var IN_GLOBAL_SCOPE = true;
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
60
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
61 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
62 * Split {@code prettyPrint} into multiple timeouts so as not to interfere with
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
63 * UI events.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
64 * If set to {@code false}, {@code prettyPrint()} is synchronous.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
65 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
66 window['PR_SHOULD_USE_CONTINUATION'] = true;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
67
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
68 /**
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
69 * Pretty print a chunk of code.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
70 * @param {string} sourceCodeHtml The HTML to pretty print.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
71 * @param {string} opt_langExtension The language name to use.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
72 * Typically, a filename extension like 'cpp' or 'java'.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
73 * @param {number|boolean} opt_numberLines True to number lines,
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
74 * or the 1-indexed number of the first line in sourceCodeHtml.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
75 * @return {string} code as html, but prettier
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
76 */
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
77 var prettyPrintOne;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
78 /**
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
79 * Find all the {@code <pre>} and {@code <code>} tags in the DOM with
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
80 * {@code class=prettyprint} and prettify them.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
81 *
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
82 * @param {Function} opt_whenDone called when prettifying is done.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
83 * @param {HTMLElement|HTMLDocument} opt_root an element or document
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
84 * containing all the elements to pretty print.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
85 * Defaults to {@code document.body}.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
86 */
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
87 var prettyPrint;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
88
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
89
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
90 (function () {
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
91 var win = window;
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
92 // Keyword lists for various languages.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
93 // We use things that coerce to strings to make them compact when minified
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
94 // and to defeat aggressive optimizers that fold large string constants.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
95 var FLOW_CONTROL_KEYWORDS = ["break,continue,do,else,for,if,return,while"];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
96 var C_KEYWORDS = [FLOW_CONTROL_KEYWORDS,"auto,case,char,const,default," +
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
97 "double,enum,extern,float,goto,inline,int,long,register,short,signed," +
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
98 "sizeof,static,struct,switch,typedef,union,unsigned,void,volatile"];
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
99 var COMMON_KEYWORDS = [C_KEYWORDS,"catch,class,delete,false,import," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
100 "new,operator,private,protected,public,this,throw,true,try,typeof"];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
101 var CPP_KEYWORDS = [COMMON_KEYWORDS,"alignof,align_union,asm,axiom,bool," +
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
102 "concept,concept_map,const_cast,constexpr,decltype,delegate," +
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
103 "dynamic_cast,explicit,export,friend,generic,late_check," +
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
104 "mutable,namespace,nullptr,property,reinterpret_cast,static_assert," +
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
105 "static_cast,template,typeid,typename,using,virtual,where"];
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
106 var JAVA_KEYWORDS = [COMMON_KEYWORDS,
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
107 "abstract,assert,boolean,byte,extends,final,finally,implements,import," +
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
108 "instanceof,interface,null,native,package,strictfp,super,synchronized," +
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
109 "throws,transient"];
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
110 var CSHARP_KEYWORDS = [JAVA_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
111 "as,base,by,checked,decimal,delegate,descending,dynamic,event," +
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
112 "fixed,foreach,from,group,implicit,in,internal,into,is,let," +
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
113 "lock,object,out,override,orderby,params,partial,readonly,ref,sbyte," +
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
114 "sealed,stackalloc,string,select,uint,ulong,unchecked,unsafe,ushort," +
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
115 "var,virtual,where"];
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
116 var COFFEE_KEYWORDS = "all,and,by,catch,class,else,extends,false,finally," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
117 "for,if,in,is,isnt,loop,new,no,not,null,of,off,on,or,return,super,then," +
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
118 "throw,true,try,unless,until,when,while,yes";
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
119 var JSCRIPT_KEYWORDS = [COMMON_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
120 "debugger,eval,export,function,get,null,set,undefined,var,with," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
121 "Infinity,NaN"];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
122 var PERL_KEYWORDS = "caller,delete,die,do,dump,elsif,eval,exit,foreach,for," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
123 "goto,if,import,last,local,my,next,no,our,print,package,redo,require," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
124 "sub,undef,unless,until,use,wantarray,while,BEGIN,END";
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
125 var PYTHON_KEYWORDS = [FLOW_CONTROL_KEYWORDS, "and,as,assert,class,def,del," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
126 "elif,except,exec,finally,from,global,import,in,is,lambda," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
127 "nonlocal,not,or,pass,print,raise,try,with,yield," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
128 "False,True,None"];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
129 var RUBY_KEYWORDS = [FLOW_CONTROL_KEYWORDS, "alias,and,begin,case,class," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
130 "def,defined,elsif,end,ensure,false,in,module,next,nil,not,or,redo," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
131 "rescue,retry,self,super,then,true,undef,unless,until,when,yield," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
132 "BEGIN,END"];
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
133 var RUST_KEYWORDS = [FLOW_CONTROL_KEYWORDS, "as,assert,const,copy,drop," +
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
134 "enum,extern,fail,false,fn,impl,let,log,loop,match,mod,move,mut,priv," +
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
135 "pub,pure,ref,self,static,struct,true,trait,type,unsafe,use"];
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
136 var SH_KEYWORDS = [FLOW_CONTROL_KEYWORDS, "case,done,elif,esac,eval,fi," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
137 "function,in,local,set,then,until"];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
138 var ALL_KEYWORDS = [
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
139 CPP_KEYWORDS, CSHARP_KEYWORDS, JSCRIPT_KEYWORDS, PERL_KEYWORDS,
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
140 PYTHON_KEYWORDS, RUBY_KEYWORDS, SH_KEYWORDS];
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
141 var C_TYPES = /^(DIR|FILE|vector|(de|priority_)?queue|list|stack|(const_)?iterator|(multi)?(set|map)|bitset|u?(int|float)\d*)\b/;
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
142
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
143 // token style names. correspond to css classes
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
144 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
145 * token style for a string literal
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
146 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
147 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
148 var PR_STRING = 'str';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
149 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
150 * token style for a keyword
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
151 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
152 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
153 var PR_KEYWORD = 'kwd';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
154 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
155 * token style for a comment
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
156 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
157 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
158 var PR_COMMENT = 'com';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
159 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
160 * token style for a type
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
161 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
162 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
163 var PR_TYPE = 'typ';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
164 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
165 * token style for a literal value. e.g. 1, null, true.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
166 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
167 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
168 var PR_LITERAL = 'lit';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
169 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
170 * token style for a punctuation string.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
171 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
172 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
173 var PR_PUNCTUATION = 'pun';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
174 /**
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
175 * token style for plain text.
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
176 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
177 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
178 var PR_PLAIN = 'pln';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
179
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
180 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
181 * token style for an sgml tag.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
182 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
183 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
184 var PR_TAG = 'tag';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
185 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
186 * token style for a markup declaration such as a DOCTYPE.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
187 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
188 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
189 var PR_DECLARATION = 'dec';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
190 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
191 * token style for embedded source.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
192 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
193 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
194 var PR_SOURCE = 'src';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
195 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
196 * token style for an sgml attribute name.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
197 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
198 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
199 var PR_ATTRIB_NAME = 'atn';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
200 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
201 * token style for an sgml attribute value.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
202 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
203 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
204 var PR_ATTRIB_VALUE = 'atv';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
205
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
206 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
207 * A class that indicates a section of markup that is not code, e.g. to allow
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
208 * embedding of line numbers within code listings.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
209 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
210 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
211 var PR_NOCODE = 'nocode';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
212
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
213
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
214
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
215 /**
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
216 * A set of tokens that can precede a regular expression literal in
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
217 * javascript
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
218 * http://web.archive.org/web/20070717142515/http://www.mozilla.org/js/language/js20/rationale/syntax.html
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
219 * has the full list, but I've removed ones that might be problematic when
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
220 * seen in languages that don't support regular expression literals.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
221 *
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
222 * <p>Specifically, I've removed any keywords that can't precede a regexp
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
223 * literal in a syntactically legal javascript program, and I've removed the
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
224 * "in" keyword since it's not a keyword in many languages, and might be used
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
225 * as a count of inches.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
226 *
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
227 * <p>The link above does not accurately describe EcmaScript rules since
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
228 * it fails to distinguish between (a=++/b/i) and (a++/b/i) but it works
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
229 * very well in practice.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
230 *
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
231 * @private
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
232 * @const
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
233 */
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
234 var REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|[!=]=?=?|\\#|%=?|&&?=?|\\(|\\*=?|[+\\-]=|->|\\/=?|::?|<<?=?|>>?>?=?|,|;|\\?|@|\\[|~|{|\\^\\^?=?|\\|\\|?=?|break|case|continue|delete|do|else|finally|instanceof|return|throw|try|typeof)\\s*';
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
235
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
236 // CAVEAT: this does not properly handle the case where a regular
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
237 // expression immediately follows another since a regular expression may
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
238 // have flags for case-sensitivity and the like. Having regexp tokens
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
239 // adjacent is not valid in any language I'm aware of, so I'm punting.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
240 // TODO: maybe style special characters inside a regexp as punctuation.
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
241
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
242 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
243 * Given a group of {@link RegExp}s, returns a {@code RegExp} that globally
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
244 * matches the union of the sets of strings matched by the input RegExp.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
245 * Since it matches globally, if the input strings have a start-of-input
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
246 * anchor (/^.../), it is ignored for the purposes of unioning.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
247 * @param {Array.<RegExp>} regexs non multiline, non-global regexs.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
248 * @return {RegExp} a global regex.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
249 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
250 function combinePrefixPatterns(regexs) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
251 var capturedGroupIndex = 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
252
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
253 var needToFoldCase = false;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
254 var ignoreCase = false;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
255 for (var i = 0, n = regexs.length; i < n; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
256 var regex = regexs[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
257 if (regex.ignoreCase) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
258 ignoreCase = true;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
259 } else if (/[a-z]/i.test(regex.source.replace(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
260 /\\u[0-9a-f]{4}|\\x[0-9a-f]{2}|\\[^ux]/gi, ''))) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
261 needToFoldCase = true;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
262 ignoreCase = false;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
263 break;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
264 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
265 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
266
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
267 var escapeCharToCodeUnit = {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
268 'b': 8,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
269 't': 9,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
270 'n': 0xa,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
271 'v': 0xb,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
272 'f': 0xc,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
273 'r': 0xd
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
274 };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
275
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
276 function decodeEscape(charsetPart) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
277 var cc0 = charsetPart.charCodeAt(0);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
278 if (cc0 !== 92 /* \\ */) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
279 return cc0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
280 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
281 var c1 = charsetPart.charAt(1);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
282 cc0 = escapeCharToCodeUnit[c1];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
283 if (cc0) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
284 return cc0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
285 } else if ('0' <= c1 && c1 <= '7') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
286 return parseInt(charsetPart.substring(1), 8);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
287 } else if (c1 === 'u' || c1 === 'x') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
288 return parseInt(charsetPart.substring(2), 16);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
289 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
290 return charsetPart.charCodeAt(1);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
291 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
292 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
293
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
294 function encodeEscape(charCode) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
295 if (charCode < 0x20) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
296 return (charCode < 0x10 ? '\\x0' : '\\x') + charCode.toString(16);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
297 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
298 var ch = String.fromCharCode(charCode);
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
299 return (ch === '\\' || ch === '-' || ch === ']' || ch === '^')
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
300 ? "\\" + ch : ch;
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
301 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
302
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
303 function caseFoldCharset(charSet) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
304 var charsetParts = charSet.substring(1, charSet.length - 1).match(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
305 new RegExp(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
306 '\\\\u[0-9A-Fa-f]{4}'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
307 + '|\\\\x[0-9A-Fa-f]{2}'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
308 + '|\\\\[0-3][0-7]{0,2}'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
309 + '|\\\\[0-7]{1,2}'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
310 + '|\\\\[\\s\\S]'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
311 + '|-'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
312 + '|[^-\\\\]',
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
313 'g'));
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
314 var ranges = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
315 var inverse = charsetParts[0] === '^';
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
316
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
317 var out = ['['];
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
318 if (inverse) { out.push('^'); }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
319
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
320 for (var i = inverse ? 1 : 0, n = charsetParts.length; i < n; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
321 var p = charsetParts[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
322 if (/\\[bdsw]/i.test(p)) { // Don't muck with named groups.
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
323 out.push(p);
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
324 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
325 var start = decodeEscape(p);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
326 var end;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
327 if (i + 2 < n && '-' === charsetParts[i + 1]) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
328 end = decodeEscape(charsetParts[i + 2]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
329 i += 2;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
330 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
331 end = start;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
332 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
333 ranges.push([start, end]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
334 // If the range might intersect letters, then expand it.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
335 // This case handling is too simplistic.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
336 // It does not deal with non-latin case folding.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
337 // It works for latin source code identifiers though.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
338 if (!(end < 65 || start > 122)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
339 if (!(end < 65 || start > 90)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
340 ranges.push([Math.max(65, start) | 32, Math.min(end, 90) | 32]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
341 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
342 if (!(end < 97 || start > 122)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
343 ranges.push([Math.max(97, start) & ~32, Math.min(end, 122) & ~32]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
344 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
345 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
346 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
347 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
348
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
349 // [[1, 10], [3, 4], [8, 12], [14, 14], [16, 16], [17, 17]]
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
350 // -> [[1, 12], [14, 14], [16, 17]]
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
351 ranges.sort(function (a, b) { return (a[0] - b[0]) || (b[1] - a[1]); });
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
352 var consolidatedRanges = [];
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
353 var lastRange = [];
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
354 for (var i = 0; i < ranges.length; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
355 var range = ranges[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
356 if (range[0] <= lastRange[1] + 1) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
357 lastRange[1] = Math.max(lastRange[1], range[1]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
358 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
359 consolidatedRanges.push(lastRange = range);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
360 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
361 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
362
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
363 for (var i = 0; i < consolidatedRanges.length; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
364 var range = consolidatedRanges[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
365 out.push(encodeEscape(range[0]));
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
366 if (range[1] > range[0]) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
367 if (range[1] + 1 > range[0]) { out.push('-'); }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
368 out.push(encodeEscape(range[1]));
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
369 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
370 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
371 out.push(']');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
372 return out.join('');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
373 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
374
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
375 function allowAnywhereFoldCaseAndRenumberGroups(regex) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
376 // Split into character sets, escape sequences, punctuation strings
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
377 // like ('(', '(?:', ')', '^'), and runs of characters that do not
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
378 // include any of the above.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
379 var parts = regex.source.match(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
380 new RegExp(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
381 '(?:'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
382 + '\\[(?:[^\\x5C\\x5D]|\\\\[\\s\\S])*\\]' // a character set
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
383 + '|\\\\u[A-Fa-f0-9]{4}' // a unicode escape
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
384 + '|\\\\x[A-Fa-f0-9]{2}' // a hex escape
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
385 + '|\\\\[0-9]+' // a back-reference or octal escape
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
386 + '|\\\\[^ux0-9]' // other escape sequence
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
387 + '|\\(\\?[:!=]' // start of a non-capturing group
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
388 + '|[\\(\\)\\^]' // start/end of a group, or line start
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
389 + '|[^\\x5B\\x5C\\(\\)\\^]+' // run of other characters
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
390 + ')',
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
391 'g'));
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
392 var n = parts.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
393
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
394 // Maps captured group numbers to the number they will occupy in
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
395 // the output or to -1 if that has not been determined, or to
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
396 // undefined if they need not be capturing in the output.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
397 var capturedGroups = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
398
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
399 // Walk over and identify back references to build the capturedGroups
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
400 // mapping.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
401 for (var i = 0, groupIndex = 0; i < n; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
402 var p = parts[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
403 if (p === '(') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
404 // groups are 1-indexed, so max group index is count of '('
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
405 ++groupIndex;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
406 } else if ('\\' === p.charAt(0)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
407 var decimalValue = +p.substring(1);
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
408 if (decimalValue) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
409 if (decimalValue <= groupIndex) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
410 capturedGroups[decimalValue] = -1;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
411 } else {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
412 // Replace with an unambiguous escape sequence so that
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
413 // an octal escape sequence does not turn into a backreference
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
414 // to a capturing group from an earlier regex.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
415 parts[i] = encodeEscape(decimalValue);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
416 }
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
417 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
418 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
419 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
420
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
421 // Renumber groups and reduce capturing groups to non-capturing groups
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
422 // where possible.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
423 for (var i = 1; i < capturedGroups.length; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
424 if (-1 === capturedGroups[i]) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
425 capturedGroups[i] = ++capturedGroupIndex;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
426 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
427 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
428 for (var i = 0, groupIndex = 0; i < n; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
429 var p = parts[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
430 if (p === '(') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
431 ++groupIndex;
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
432 if (!capturedGroups[groupIndex]) {
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
433 parts[i] = '(?:';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
434 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
435 } else if ('\\' === p.charAt(0)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
436 var decimalValue = +p.substring(1);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
437 if (decimalValue && decimalValue <= groupIndex) {
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
438 parts[i] = '\\' + capturedGroups[decimalValue];
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
439 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
440 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
441 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
442
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
443 // Remove any prefix anchors so that the output will match anywhere.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
444 // ^^ really does mean an anchored match though.
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
445 for (var i = 0; i < n; ++i) {
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
446 if ('^' === parts[i] && '^' !== parts[i + 1]) { parts[i] = ''; }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
447 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
448
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
449 // Expand letters to groups to handle mixing of case-sensitive and
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
450 // case-insensitive patterns if necessary.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
451 if (regex.ignoreCase && needToFoldCase) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
452 for (var i = 0; i < n; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
453 var p = parts[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
454 var ch0 = p.charAt(0);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
455 if (p.length >= 2 && ch0 === '[') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
456 parts[i] = caseFoldCharset(p);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
457 } else if (ch0 !== '\\') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
458 // TODO: handle letters in numeric escapes.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
459 parts[i] = p.replace(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
460 /[a-zA-Z]/g,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
461 function (ch) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
462 var cc = ch.charCodeAt(0);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
463 return '[' + String.fromCharCode(cc & ~32, cc | 32) + ']';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
464 });
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
465 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
466 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
467 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
468
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
469 return parts.join('');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
470 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
471
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
472 var rewritten = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
473 for (var i = 0, n = regexs.length; i < n; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
474 var regex = regexs[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
475 if (regex.global || regex.multiline) { throw new Error('' + regex); }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
476 rewritten.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
477 '(?:' + allowAnywhereFoldCaseAndRenumberGroups(regex) + ')');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
478 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
479
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
480 return new RegExp(rewritten.join('|'), ignoreCase ? 'gi' : 'g');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
481 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
482
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
483 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
484 * Split markup into a string of source code and an array mapping ranges in
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
485 * that string to the text nodes in which they appear.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
486 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
487 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
488 * The HTML DOM structure:</p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
489 * <pre>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
490 * (Element "p"
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
491 * (Element "b"
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
492 * (Text "print ")) ; #1
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
493 * (Text "'Hello '") ; #2
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
494 * (Element "br") ; #3
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
495 * (Text " + 'World';")) ; #4
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
496 * </pre>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
497 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
498 * corresponds to the HTML
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
499 * {@code <p><b>print </b>'Hello '<br> + 'World';</p>}.</p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
500 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
501 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
502 * It will produce the output:</p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
503 * <pre>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
504 * {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
505 * sourceCode: "print 'Hello '\n + 'World';",
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
506 * // 1 2
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
507 * // 012345678901234 5678901234567
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
508 * spans: [0, #1, 6, #2, 14, #3, 15, #4]
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
509 * }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
510 * </pre>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
511 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
512 * where #1 is a reference to the {@code "print "} text node above, and so
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
513 * on for the other text nodes.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
514 * </p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
515 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
516 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
517 * The {@code} spans array is an array of pairs. Even elements are the start
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
518 * indices of substrings, and odd elements are the text nodes (or BR elements)
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
519 * that contain the text for those substrings.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
520 * Substrings continue until the next index or the end of the source.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
521 * </p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
522 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
523 * @param {Node} node an HTML DOM subtree containing source-code.
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
524 * @param {boolean} isPreformatted true if white-space in text nodes should
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
525 * be considered significant.
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
526 * @return {Object} source code and the text nodes in which they occur.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
527 */
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
528 function extractSourceSpans(node, isPreformatted) {
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
529 var nocode = /(?:^|\s)nocode(?:\s|$)/;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
530
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
531 var chunks = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
532 var length = 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
533 var spans = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
534 var k = 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
535
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
536 function walk(node) {
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
537 var type = node.nodeType;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
538 if (type == 1) { // Element
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
539 if (nocode.test(node.className)) { return; }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
540 for (var child = node.firstChild; child; child = child.nextSibling) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
541 walk(child);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
542 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
543 var nodeName = node.nodeName.toLowerCase();
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
544 if ('br' === nodeName || 'li' === nodeName) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
545 chunks[k] = '\n';
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
546 spans[k << 1] = length++;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
547 spans[(k++ << 1) | 1] = node;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
548 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
549 } else if (type == 3 || type == 4) { // Text
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
550 var text = node.nodeValue;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
551 if (text.length) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
552 if (!isPreformatted) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
553 text = text.replace(/[ \t\r\n]+/g, ' ');
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
554 } else {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
555 text = text.replace(/\r\n?/g, '\n'); // Normalize newlines.
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
556 }
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
557 // TODO: handle tabs here?
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
558 chunks[k] = text;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
559 spans[k << 1] = length;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
560 length += text.length;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
561 spans[(k++ << 1) | 1] = node;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
562 }
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
563 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
564 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
565
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
566 walk(node);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
567
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
568 return {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
569 sourceCode: chunks.join('').replace(/\n$/, ''),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
570 spans: spans
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
571 };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
572 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
573
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
574 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
575 * Apply the given language handler to sourceCode and add the resulting
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
576 * decorations to out.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
577 * @param {number} basePos the index of sourceCode within the chunk of source
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
578 * whose decorations are already present on out.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
579 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
580 function appendDecorations(basePos, sourceCode, langHandler, out) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
581 if (!sourceCode) { return; }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
582 var job = {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
583 sourceCode: sourceCode,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
584 basePos: basePos
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
585 };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
586 langHandler(job);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
587 out.push.apply(out, job.decorations);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
588 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
589
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
590 var notWs = /\S/;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
591
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
592 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
593 * Given an element, if it contains only one child element and any text nodes
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
594 * it contains contain only space characters, return the sole child element.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
595 * Otherwise returns undefined.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
596 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
597 * This is meant to return the CODE element in {@code <pre><code ...>} when
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
598 * there is a single child element that contains all the non-space textual
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
599 * content, but not to return anything where there are multiple child elements
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
600 * as in {@code <pre><code>...</code><code>...</code></pre>} or when there
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
601 * is textual content.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
602 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
603 function childContentWrapper(element) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
604 var wrapper = undefined;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
605 for (var c = element.firstChild; c; c = c.nextSibling) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
606 var type = c.nodeType;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
607 wrapper = (type === 1) // Element Node
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
608 ? (wrapper ? element : c)
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
609 : (type === 3) // Text Node
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
610 ? (notWs.test(c.nodeValue) ? element : wrapper)
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
611 : wrapper;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
612 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
613 return wrapper === element ? undefined : wrapper;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
614 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
615
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
616 /** Given triples of [style, pattern, context] returns a lexing function,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
617 * The lexing function interprets the patterns to find token boundaries and
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
618 * returns a decoration list of the form
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
619 * [index_0, style_0, index_1, style_1, ..., index_n, style_n]
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
620 * where index_n is an index into the sourceCode, and style_n is a style
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
621 * constant like PR_PLAIN. index_n-1 <= index_n, and style_n-1 applies to
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
622 * all characters in sourceCode[index_n-1:index_n].
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
623 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
624 * The stylePatterns is a list whose elements have the form
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
625 * [style : string, pattern : RegExp, DEPRECATED, shortcut : string].
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
626 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
627 * Style is a style constant like PR_PLAIN, or can be a string of the
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
628 * form 'lang-FOO', where FOO is a language extension describing the
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
629 * language of the portion of the token in $1 after pattern executes.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
630 * E.g., if style is 'lang-lisp', and group 1 contains the text
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
631 * '(hello (world))', then that portion of the token will be passed to the
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
632 * registered lisp handler for formatting.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
633 * The text before and after group 1 will be restyled using this decorator
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
634 * so decorators should take care that this doesn't result in infinite
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
635 * recursion. For example, the HTML lexer rule for SCRIPT elements looks
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
636 * something like ['lang-js', /<[s]cript>(.+?)<\/script>/]. This may match
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
637 * '<script>foo()<\/script>', which would cause the current decorator to
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
638 * be called with '<script>' which would not match the same rule since
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
639 * group 1 must not be empty, so it would be instead styled as PR_TAG by
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
640 * the generic tag rule. The handler registered for the 'js' extension would
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
641 * then be called with 'foo()', and finally, the current decorator would
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
642 * be called with '<\/script>' which would not match the original rule and
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
643 * so the generic tag rule would identify it as a tag.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
644 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
645 * Pattern must only match prefixes, and if it matches a prefix, then that
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
646 * match is considered a token with the same style.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
647 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
648 * Context is applied to the last non-whitespace, non-comment token
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
649 * recognized.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
650 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
651 * Shortcut is an optional string of characters, any of which, if the first
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
652 * character, gurantee that this pattern and only this pattern matches.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
653 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
654 * @param {Array} shortcutStylePatterns patterns that always start with
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
655 * a known character. Must have a shortcut string.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
656 * @param {Array} fallthroughStylePatterns patterns that will be tried in
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
657 * order if the shortcut ones fail. May have shortcuts.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
658 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
659 * @return {function (Object)} a
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
660 * function that takes source code and returns a list of decorations.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
661 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
662 function createSimpleLexer(shortcutStylePatterns, fallthroughStylePatterns) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
663 var shortcuts = {};
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
664 var tokenizer;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
665 (function () {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
666 var allPatterns = shortcutStylePatterns.concat(fallthroughStylePatterns);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
667 var allRegexs = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
668 var regexKeys = {};
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
669 for (var i = 0, n = allPatterns.length; i < n; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
670 var patternParts = allPatterns[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
671 var shortcutChars = patternParts[3];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
672 if (shortcutChars) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
673 for (var c = shortcutChars.length; --c >= 0;) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
674 shortcuts[shortcutChars.charAt(c)] = patternParts;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
675 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
676 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
677 var regex = patternParts[1];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
678 var k = '' + regex;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
679 if (!regexKeys.hasOwnProperty(k)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
680 allRegexs.push(regex);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
681 regexKeys[k] = null;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
682 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
683 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
684 allRegexs.push(/[\0-\uffff]/);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
685 tokenizer = combinePrefixPatterns(allRegexs);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
686 })();
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
687
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
688 var nPatterns = fallthroughStylePatterns.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
689
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
690 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
691 * Lexes job.sourceCode and produces an output array job.decorations of
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
692 * style classes preceded by the position at which they start in
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
693 * job.sourceCode in order.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
694 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
695 * @param {Object} job an object like <pre>{
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
696 * sourceCode: {string} sourceText plain text,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
697 * basePos: {int} position of job.sourceCode in the larger chunk of
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
698 * sourceCode.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
699 * }</pre>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
700 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
701 var decorate = function (job) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
702 var sourceCode = job.sourceCode, basePos = job.basePos;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
703 /** Even entries are positions in source in ascending order. Odd enties
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
704 * are style markers (e.g., PR_COMMENT) that run from that position until
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
705 * the end.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
706 * @type {Array.<number|string>}
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
707 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
708 var decorations = [basePos, PR_PLAIN];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
709 var pos = 0; // index into sourceCode
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
710 var tokens = sourceCode.match(tokenizer) || [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
711 var styleCache = {};
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
712
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
713 for (var ti = 0, nTokens = tokens.length; ti < nTokens; ++ti) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
714 var token = tokens[ti];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
715 var style = styleCache[token];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
716 var match = void 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
717
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
718 var isEmbedded;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
719 if (typeof style === 'string') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
720 isEmbedded = false;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
721 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
722 var patternParts = shortcuts[token.charAt(0)];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
723 if (patternParts) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
724 match = token.match(patternParts[1]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
725 style = patternParts[0];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
726 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
727 for (var i = 0; i < nPatterns; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
728 patternParts = fallthroughStylePatterns[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
729 match = token.match(patternParts[1]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
730 if (match) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
731 style = patternParts[0];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
732 break;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
733 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
734 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
735
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
736 if (!match) { // make sure that we make progress
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
737 style = PR_PLAIN;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
738 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
739 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
740
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
741 isEmbedded = style.length >= 5 && 'lang-' === style.substring(0, 5);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
742 if (isEmbedded && !(match && typeof match[1] === 'string')) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
743 isEmbedded = false;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
744 style = PR_SOURCE;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
745 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
746
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
747 if (!isEmbedded) { styleCache[token] = style; }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
748 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
749
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
750 var tokenStart = pos;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
751 pos += token.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
752
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
753 if (!isEmbedded) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
754 decorations.push(basePos + tokenStart, style);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
755 } else { // Treat group 1 as an embedded block of source code.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
756 var embeddedSource = match[1];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
757 var embeddedSourceStart = token.indexOf(embeddedSource);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
758 var embeddedSourceEnd = embeddedSourceStart + embeddedSource.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
759 if (match[2]) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
760 // If embeddedSource can be blank, then it would match at the
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
761 // beginning which would cause us to infinitely recurse on the
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
762 // entire token, so we catch the right context in match[2].
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
763 embeddedSourceEnd = token.length - match[2].length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
764 embeddedSourceStart = embeddedSourceEnd - embeddedSource.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
765 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
766 var lang = style.substring(5);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
767 // Decorate the left of the embedded source
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
768 appendDecorations(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
769 basePos + tokenStart,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
770 token.substring(0, embeddedSourceStart),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
771 decorate, decorations);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
772 // Decorate the embedded source
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
773 appendDecorations(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
774 basePos + tokenStart + embeddedSourceStart,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
775 embeddedSource,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
776 langHandlerForExtension(lang, embeddedSource),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
777 decorations);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
778 // Decorate the right of the embedded section
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
779 appendDecorations(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
780 basePos + tokenStart + embeddedSourceEnd,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
781 token.substring(embeddedSourceEnd),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
782 decorate, decorations);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
783 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
784 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
785 job.decorations = decorations;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
786 };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
787 return decorate;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
788 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
789
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
790 /** returns a function that produces a list of decorations from source text.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
791 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
792 * This code treats ", ', and ` as string delimiters, and \ as a string
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
793 * escape. It does not recognize perl's qq() style strings.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
794 * It has no special handling for double delimiter escapes as in basic, or
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
795 * the tripled delimiters used in python, but should work on those regardless
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
796 * although in those cases a single string literal may be broken up into
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
797 * multiple adjacent string literals.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
798 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
799 * It recognizes C, C++, and shell style comments.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
800 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
801 * @param {Object} options a set of optional parameters.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
802 * @return {function (Object)} a function that examines the source code
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
803 * in the input job and builds the decoration list.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
804 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
805 function sourceDecorator(options) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
806 var shortcutStylePatterns = [], fallthroughStylePatterns = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
807 if (options['tripleQuotedStrings']) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
808 // '''multi-line-string''', 'single-line-string', and double-quoted
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
809 shortcutStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
810 [PR_STRING, /^(?:\'\'\'(?:[^\'\\]|\\[\s\S]|\'{1,2}(?=[^\']))*(?:\'\'\'|$)|\"\"\"(?:[^\"\\]|\\[\s\S]|\"{1,2}(?=[^\"]))*(?:\"\"\"|$)|\'(?:[^\\\']|\\[\s\S])*(?:\'|$)|\"(?:[^\\\"]|\\[\s\S])*(?:\"|$))/,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
811 null, '\'"']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
812 } else if (options['multiLineStrings']) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
813 // 'multi-line-string', "multi-line-string"
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
814 shortcutStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
815 [PR_STRING, /^(?:\'(?:[^\\\']|\\[\s\S])*(?:\'|$)|\"(?:[^\\\"]|\\[\s\S])*(?:\"|$)|\`(?:[^\\\`]|\\[\s\S])*(?:\`|$))/,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
816 null, '\'"`']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
817 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
818 // 'single-line-string', "single-line-string"
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
819 shortcutStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
820 [PR_STRING,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
821 /^(?:\'(?:[^\\\'\r\n]|\\.)*(?:\'|$)|\"(?:[^\\\"\r\n]|\\.)*(?:\"|$))/,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
822 null, '"\'']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
823 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
824 if (options['verbatimStrings']) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
825 // verbatim-string-literal production from the C# grammar. See issue 93.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
826 fallthroughStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
827 [PR_STRING, /^@\"(?:[^\"]|\"\")*(?:\"|$)/, null]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
828 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
829 var hc = options['hashComments'];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
830 if (hc) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
831 if (options['cStyleComments']) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
832 if (hc > 1) { // multiline hash comments
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
833 shortcutStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
834 [PR_COMMENT, /^#(?:##(?:[^#]|#(?!##))*(?:###|$)|.*)/, null, '#']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
835 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
836 // Stop C preprocessor declarations at an unclosed open comment
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
837 shortcutStylePatterns.push(
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
838 [PR_COMMENT, /^#(?:(?:define|e(?:l|nd)if|else|error|ifn?def|include|line|pragma|undef|warning)\b|[^\r\n]*)/,
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
839 null, '#']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
840 }
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
841 // #include <stdio.h>
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
842 fallthroughStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
843 [PR_STRING,
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
844 /^<(?:(?:(?:\.\.\/)*|\/?)(?:[\w-]+(?:\/[\w-]+)+)?[\w-]+\.h(?:h|pp|\+\+)?|[a-z]\w*)>/,
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
845 null]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
846 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
847 shortcutStylePatterns.push([PR_COMMENT, /^#[^\r\n]*/, null, '#']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
848 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
849 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
850 if (options['cStyleComments']) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
851 fallthroughStylePatterns.push([PR_COMMENT, /^\/\/[^\r\n]*/, null]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
852 fallthroughStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
853 [PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
854 }
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
855 var regexLiterals = options['regexLiterals'];
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
856 if (regexLiterals) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
857 /**
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
858 * @const
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
859 */
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
860 var regexExcls = regexLiterals > 1
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
861 ? '' // Multiline regex literals
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
862 : '\n\r';
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
863 /**
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
864 * @const
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
865 */
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
866 var regexAny = regexExcls ? '.' : '[\\S\\s]';
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
867 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
868 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
869 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
870 var REGEX_LITERAL = (
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
871 // A regular expression literal starts with a slash that is
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
872 // not followed by * or / so that it is not confused with
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
873 // comments.
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
874 '/(?=[^/*' + regexExcls + '])'
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
875 // and then contains any number of raw characters,
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
876 + '(?:[^/\\x5B\\x5C' + regexExcls + ']'
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
877 // escape sequences (\x5C),
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
878 + '|\\x5C' + regexAny
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
879 // or non-nesting character sets (\x5B\x5D);
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
880 + '|\\x5B(?:[^\\x5C\\x5D' + regexExcls + ']'
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
881 + '|\\x5C' + regexAny + ')*(?:\\x5D|$))+'
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
882 // finally closed by a /.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
883 + '/');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
884 fallthroughStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
885 ['lang-regex',
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
886 RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
887 ]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
888 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
889
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
890 var types = options['types'];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
891 if (types) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
892 fallthroughStylePatterns.push([PR_TYPE, types]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
893 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
894
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
895 var keywords = ("" + options['keywords']).replace(/^ | $/g, '');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
896 if (keywords.length) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
897 fallthroughStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
898 [PR_KEYWORD,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
899 new RegExp('^(?:' + keywords.replace(/[\s,]+/g, '|') + ')\\b'),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
900 null]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
901 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
902
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
903 shortcutStylePatterns.push([PR_PLAIN, /^\s+/, null, ' \r\n\t\xA0']);
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
904
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
905 var punctuation =
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
906 // The Bash man page says
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
907
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
908 // A word is a sequence of characters considered as a single
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
909 // unit by GRUB. Words are separated by metacharacters,
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
910 // which are the following plus space, tab, and newline: { }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
911 // | & $ ; < >
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
912 // ...
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
913
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
914 // A word beginning with # causes that word and all remaining
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
915 // characters on that line to be ignored.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
916
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
917 // which means that only a '#' after /(?:^|[{}|&$;<>\s])/ starts a
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
918 // comment but empirically
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
919 // $ echo {#}
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
920 // {#}
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
921 // $ echo \$#
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
922 // $#
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
923 // $ echo }#
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
924 // }#
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
925
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
926 // so /(?:^|[|&;<>\s])/ is more appropriate.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
927
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
928 // http://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_1.html#SEC3
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
929 // suggests that this definition is compatible with a
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
930 // default mode that tries to use a single token definition
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
931 // to recognize both bash/python style comments and C
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
932 // preprocessor directives.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
933
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
934 // This definition of punctuation does not include # in the list of
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
935 // follow-on exclusions, so # will not be broken before if preceeded
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
936 // by a punctuation character. We could try to exclude # after
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
937 // [|&;<>] but that doesn't seem to cause many major problems.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
938 // If that does turn out to be a problem, we should change the below
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
939 // when hc is truthy to include # in the run of punctuation characters
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
940 // only when not followint [|&;<>].
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
941 '^.[^\\s\\w.$@\'"`/\\\\]*';
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
942 if (options['regexLiterals']) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
943 punctuation += '(?!\s*\/)';
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
944 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
945
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
946 fallthroughStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
947 // TODO(mikesamuel): recognize non-latin letters and numerals in idents
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
948 [PR_LITERAL, /^@[a-z_$][a-z_$@0-9]*/i, null],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
949 [PR_TYPE, /^(?:[@_]?[A-Z]+[a-z][A-Za-z_$@0-9]*|\w+_t\b)/, null],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
950 [PR_PLAIN, /^[a-z_$][a-z_$@0-9]*/i, null],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
951 [PR_LITERAL,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
952 new RegExp(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
953 '^(?:'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
954 // A hex number
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
955 + '0x[a-f0-9]+'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
956 // or an octal or decimal number,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
957 + '|(?:\\d(?:_\\d+)*\\d*(?:\\.\\d*)?|\\.\\d\\+)'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
958 // possibly in scientific notation
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
959 + '(?:e[+\\-]?\\d+)?'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
960 + ')'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
961 // with an optional modifier like UL for unsigned long
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
962 + '[a-z]*', 'i'),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
963 null, '0123456789'],
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
964 // Don't treat escaped quotes in bash as starting strings.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
965 // See issue 144.
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
966 [PR_PLAIN, /^\\[\s\S]?/, null],
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
967 [PR_PUNCTUATION, new RegExp(punctuation), null]);
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
968
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
969 return createSimpleLexer(shortcutStylePatterns, fallthroughStylePatterns);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
970 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
971
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
972 var decorateSource = sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
973 'keywords': ALL_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
974 'hashComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
975 'cStyleComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
976 'multiLineStrings': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
977 'regexLiterals': true
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
978 });
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
979
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
980 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
981 * Given a DOM subtree, wraps it in a list, and puts each line into its own
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
982 * list item.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
983 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
984 * @param {Node} node modified in place. Its content is pulled into an
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
985 * HTMLOListElement, and each line is moved into a separate list item.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
986 * This requires cloning elements, so the input might not have unique
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
987 * IDs after numbering.
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
988 * @param {boolean} isPreformatted true iff white-space in text nodes should
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
989 * be treated as significant.
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
990 */
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
991 function numberLines(node, opt_startLineNum, isPreformatted) {
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
992 var nocode = /(?:^|\s)nocode(?:\s|$)/;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
993 var lineBreak = /\r\n?|\n/;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
994
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
995 var document = node.ownerDocument;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
996
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
997 var li = document.createElement('li');
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
998 while (node.firstChild) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
999 li.appendChild(node.firstChild);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1000 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1001 // An array of lines. We split below, so this is initialized to one
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1002 // un-split line.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1003 var listItems = [li];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1004
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1005 function walk(node) {
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1006 var type = node.nodeType;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1007 if (type == 1 && !nocode.test(node.className)) { // Element
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1008 if ('br' === node.nodeName) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1009 breakAfter(node);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1010 // Discard the <BR> since it is now flush against a </LI>.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1011 if (node.parentNode) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1012 node.parentNode.removeChild(node);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1013 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1014 } else {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1015 for (var child = node.firstChild; child; child = child.nextSibling) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1016 walk(child);
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1017 }
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1018 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1019 } else if ((type == 3 || type == 4) && isPreformatted) { // Text
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1020 var text = node.nodeValue;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1021 var match = text.match(lineBreak);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1022 if (match) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1023 var firstLine = text.substring(0, match.index);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1024 node.nodeValue = firstLine;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1025 var tail = text.substring(match.index + match[0].length);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1026 if (tail) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1027 var parent = node.parentNode;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1028 parent.insertBefore(
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1029 document.createTextNode(tail), node.nextSibling);
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1030 }
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1031 breakAfter(node);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1032 if (!firstLine) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1033 // Don't leave blank text nodes in the DOM.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1034 node.parentNode.removeChild(node);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1035 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1036 }
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1037 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1038 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1039
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1040 // Split a line after the given node.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1041 function breakAfter(lineEndNode) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1042 // If there's nothing to the right, then we can skip ending the line
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1043 // here, and move root-wards since splitting just before an end-tag
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1044 // would require us to create a bunch of empty copies.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1045 while (!lineEndNode.nextSibling) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1046 lineEndNode = lineEndNode.parentNode;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1047 if (!lineEndNode) { return; }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1048 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1049
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1050 function breakLeftOf(limit, copy) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1051 // Clone shallowly if this node needs to be on both sides of the break.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1052 var rightSide = copy ? limit.cloneNode(false) : limit;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1053 var parent = limit.parentNode;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1054 if (parent) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1055 // We clone the parent chain.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1056 // This helps us resurrect important styling elements that cross lines.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1057 // E.g. in <i>Foo<br>Bar</i>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1058 // should be rewritten to <li><i>Foo</i></li><li><i>Bar</i></li>.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1059 var parentClone = breakLeftOf(parent, 1);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1060 // Move the clone and everything to the right of the original
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1061 // onto the cloned parent.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1062 var next = limit.nextSibling;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1063 parentClone.appendChild(rightSide);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1064 for (var sibling = next; sibling; sibling = next) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1065 next = sibling.nextSibling;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1066 parentClone.appendChild(sibling);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1067 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1068 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1069 return rightSide;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1070 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1071
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1072 var copiedListItem = breakLeftOf(lineEndNode.nextSibling, 0);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1073
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1074 // Walk the parent chain until we reach an unattached LI.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1075 for (var parent;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1076 // Check nodeType since IE invents document fragments.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1077 (parent = copiedListItem.parentNode) && parent.nodeType === 1;) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1078 copiedListItem = parent;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1079 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1080 // Put it on the list of lines for later processing.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1081 listItems.push(copiedListItem);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1082 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1083
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1084 // Split lines while there are lines left to split.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1085 for (var i = 0; // Number of lines that have been split so far.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1086 i < listItems.length; // length updated by breakAfter calls.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1087 ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1088 walk(listItems[i]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1089 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1090
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1091 // Make sure numeric indices show correctly.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1092 if (opt_startLineNum === (opt_startLineNum|0)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1093 listItems[0].setAttribute('value', opt_startLineNum);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1094 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1095
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1096 var ol = document.createElement('ol');
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1097 ol.className = 'linenums';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1098 var offset = Math.max(0, ((opt_startLineNum - 1 /* zero index */)) | 0) || 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1099 for (var i = 0, n = listItems.length; i < n; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1100 li = listItems[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1101 // Stick a class on the LIs so that stylesheets can
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1102 // color odd/even rows, or any other row pattern that
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1103 // is co-prime with 10.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1104 li.className = 'L' + ((i + offset) % 10);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1105 if (!li.firstChild) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1106 li.appendChild(document.createTextNode('\xA0'));
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1107 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1108 ol.appendChild(li);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1109 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1110
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1111 node.appendChild(ol);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1112 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1113 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1114 * Breaks {@code job.sourceCode} around style boundaries in
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1115 * {@code job.decorations} and modifies {@code job.sourceNode} in place.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1116 * @param {Object} job like <pre>{
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1117 * sourceCode: {string} source as plain text,
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1118 * sourceNode: {HTMLElement} the element containing the source,
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1119 * spans: {Array.<number|Node>} alternating span start indices into source
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1120 * and the text node or element (e.g. {@code <BR>}) corresponding to that
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1121 * span.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1122 * decorations: {Array.<number|string} an array of style classes preceded
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1123 * by the position at which they start in job.sourceCode in order
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1124 * }</pre>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1125 * @private
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1126 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1127 function recombineTagsAndDecorations(job) {
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1128 var isIE8OrEarlier = /\bMSIE\s(\d+)/.exec(navigator.userAgent);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1129 isIE8OrEarlier = isIE8OrEarlier && +isIE8OrEarlier[1] <= 8;
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1130 var newlineRe = /\n/g;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1131
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1132 var source = job.sourceCode;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1133 var sourceLength = source.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1134 // Index into source after the last code-unit recombined.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1135 var sourceIndex = 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1136
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1137 var spans = job.spans;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1138 var nSpans = spans.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1139 // Index into spans after the last span which ends at or before sourceIndex.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1140 var spanIndex = 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1141
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1142 var decorations = job.decorations;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1143 var nDecorations = decorations.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1144 // Index into decorations after the last decoration which ends at or before
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1145 // sourceIndex.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1146 var decorationIndex = 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1147
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1148 // Remove all zero-length decorations.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1149 decorations[nDecorations] = sourceLength;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1150 var decPos, i;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1151 for (i = decPos = 0; i < nDecorations;) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1152 if (decorations[i] !== decorations[i + 2]) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1153 decorations[decPos++] = decorations[i++];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1154 decorations[decPos++] = decorations[i++];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1155 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1156 i += 2;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1157 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1158 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1159 nDecorations = decPos;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1160
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1161 // Simplify decorations.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1162 for (i = decPos = 0; i < nDecorations;) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1163 var startPos = decorations[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1164 // Conflate all adjacent decorations that use the same style.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1165 var startDec = decorations[i + 1];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1166 var end = i + 2;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1167 while (end + 2 <= nDecorations && decorations[end + 1] === startDec) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1168 end += 2;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1169 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1170 decorations[decPos++] = startPos;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1171 decorations[decPos++] = startDec;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1172 i = end;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1173 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1174
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1175 nDecorations = decorations.length = decPos;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1176
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1177 var sourceNode = job.sourceNode;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1178 var oldDisplay;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1179 if (sourceNode) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1180 oldDisplay = sourceNode.style.display;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1181 sourceNode.style.display = 'none';
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1182 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1183 try {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1184 var decoration = null;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1185 while (spanIndex < nSpans) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1186 var spanStart = spans[spanIndex];
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1187 var spanEnd = spans[spanIndex + 2] || sourceLength;
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1188
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1189 var decEnd = decorations[decorationIndex + 2] || sourceLength;
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1190
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1191 var end = Math.min(spanEnd, decEnd);
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1192
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1193 var textNode = spans[spanIndex + 1];
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1194 var styledText;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1195 if (textNode.nodeType !== 1 // Don't muck with <BR>s or <LI>s
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1196 // Don't introduce spans around empty text nodes.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1197 && (styledText = source.substring(sourceIndex, end))) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1198 // This may seem bizarre, and it is. Emitting LF on IE causes the
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1199 // code to display with spaces instead of line breaks.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1200 // Emitting Windows standard issue linebreaks (CRLF) causes a blank
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1201 // space to appear at the beginning of every line but the first.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1202 // Emitting an old Mac OS 9 line separator makes everything spiffy.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1203 if (isIE8OrEarlier) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1204 styledText = styledText.replace(newlineRe, '\r');
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1205 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1206 textNode.nodeValue = styledText;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1207 var document = textNode.ownerDocument;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1208 var span = document.createElement('span');
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1209 span.className = decorations[decorationIndex + 1];
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1210 var parentNode = textNode.parentNode;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1211 parentNode.replaceChild(span, textNode);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1212 span.appendChild(textNode);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1213 if (sourceIndex < spanEnd) { // Split off a text node.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1214 spans[spanIndex + 1] = textNode
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1215 // TODO: Possibly optimize by using '' if there's no flicker.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1216 = document.createTextNode(source.substring(end, spanEnd));
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1217 parentNode.insertBefore(textNode, span.nextSibling);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1218 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1219 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1221 sourceIndex = end;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1222
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1223 if (sourceIndex >= spanEnd) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1224 spanIndex += 2;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1225 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1226 if (sourceIndex >= decEnd) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1227 decorationIndex += 2;
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1228 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1229 }
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1230 } finally {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1231 if (sourceNode) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1232 sourceNode.style.display = oldDisplay;
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1233 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1234 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1235 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1236
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1237 /** Maps language-specific file extensions to handlers. */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1238 var langHandlerRegistry = {};
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1239 /** Register a language handler for the given file extensions.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1240 * @param {function (Object)} handler a function from source code to a list
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1241 * of decorations. Takes a single argument job which describes the
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1242 * state of the computation. The single parameter has the form
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1243 * {@code {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1244 * sourceCode: {string} as plain text.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1245 * decorations: {Array.<number|string>} an array of style classes
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1246 * preceded by the position at which they start in
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1247 * job.sourceCode in order.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1248 * The language handler should assigned this field.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1249 * basePos: {int} the position of source in the larger source chunk.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1250 * All positions in the output decorations array are relative
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1251 * to the larger source chunk.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1252 * } }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1253 * @param {Array.<string>} fileExtensions
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1254 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1255 function registerLangHandler(handler, fileExtensions) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1256 for (var i = fileExtensions.length; --i >= 0;) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1257 var ext = fileExtensions[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1258 if (!langHandlerRegistry.hasOwnProperty(ext)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1259 langHandlerRegistry[ext] = handler;
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1260 } else if (win['console']) {
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1261 console['warn']('cannot override language handler %s', ext);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1262 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1263 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1264 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1265 function langHandlerForExtension(extension, source) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1266 if (!(extension && langHandlerRegistry.hasOwnProperty(extension))) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1267 // Treat it as markup if the first non whitespace character is a < and
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1268 // the last non-whitespace character is a >.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1269 extension = /^\s*</.test(source)
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1270 ? 'default-markup'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1271 : 'default-code';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1272 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1273 return langHandlerRegistry[extension];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1274 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1275 registerLangHandler(decorateSource, ['default-code']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1276 registerLangHandler(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1277 createSimpleLexer(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1278 [],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1279 [
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1280 [PR_PLAIN, /^[^<?]+/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1281 [PR_DECLARATION, /^<!\w[^>]*(?:>|$)/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1282 [PR_COMMENT, /^<\!--[\s\S]*?(?:-\->|$)/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1283 // Unescaped content in an unknown language
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1284 ['lang-', /^<\?([\s\S]+?)(?:\?>|$)/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1285 ['lang-', /^<%([\s\S]+?)(?:%>|$)/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1286 [PR_PUNCTUATION, /^(?:<[%?]|[%?]>)/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1287 ['lang-', /^<xmp\b[^>]*>([\s\S]+?)<\/xmp\b[^>]*>/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1288 // Unescaped content in javascript. (Or possibly vbscript).
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1289 ['lang-js', /^<script\b[^>]*>([\s\S]*?)(<\/script\b[^>]*>)/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1290 // Contains unescaped stylesheet content
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1291 ['lang-css', /^<style\b[^>]*>([\s\S]*?)(<\/style\b[^>]*>)/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1292 ['lang-in.tag', /^(<\/?[a-z][^<>]*>)/i]
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1293 ]),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1294 ['default-markup', 'htm', 'html', 'mxml', 'xhtml', 'xml', 'xsl']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1295 registerLangHandler(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1296 createSimpleLexer(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1297 [
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1298 [PR_PLAIN, /^[\s]+/, null, ' \t\r\n'],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1299 [PR_ATTRIB_VALUE, /^(?:\"[^\"]*\"?|\'[^\']*\'?)/, null, '\"\'']
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1300 ],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1301 [
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1302 [PR_TAG, /^^<\/?[a-z](?:[\w.:-]*\w)?|\/?>$/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1303 [PR_ATTRIB_NAME, /^(?!style[\s=]|on)[a-z](?:[\w:-]*\w)?/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1304 ['lang-uq.val', /^=\s*([^>\'\"\s]*(?:[^>\'\"\s\/]|\/(?=\s)))/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1305 [PR_PUNCTUATION, /^[=<>\/]+/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1306 ['lang-js', /^on\w+\s*=\s*\"([^\"]+)\"/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1307 ['lang-js', /^on\w+\s*=\s*\'([^\']+)\'/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1308 ['lang-js', /^on\w+\s*=\s*([^\"\'>\s]+)/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1309 ['lang-css', /^style\s*=\s*\"([^\"]+)\"/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1310 ['lang-css', /^style\s*=\s*\'([^\']+)\'/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1311 ['lang-css', /^style\s*=\s*([^\"\'>\s]+)/i]
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1312 ]),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1313 ['in.tag']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1314 registerLangHandler(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1315 createSimpleLexer([], [[PR_ATTRIB_VALUE, /^[\s\S]+/]]), ['uq.val']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1316 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1317 'keywords': CPP_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1318 'hashComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1319 'cStyleComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1320 'types': C_TYPES
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1321 }), ['c', 'cc', 'cpp', 'cxx', 'cyc', 'm']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1322 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1323 'keywords': 'null,true,false'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1324 }), ['json']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1325 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1326 'keywords': CSHARP_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1327 'hashComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1328 'cStyleComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1329 'verbatimStrings': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1330 'types': C_TYPES
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1331 }), ['cs']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1332 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1333 'keywords': JAVA_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1334 'cStyleComments': true
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1335 }), ['java']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1336 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1337 'keywords': SH_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1338 'hashComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1339 'multiLineStrings': true
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1340 }), ['bash', 'bsh', 'csh', 'sh']);
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1341 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1342 'keywords': PYTHON_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1343 'hashComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1344 'multiLineStrings': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1345 'tripleQuotedStrings': true
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1346 }), ['cv', 'py', 'python']);
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1347 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1348 'keywords': PERL_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1349 'hashComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1350 'multiLineStrings': true,
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1351 'regexLiterals': 2 // multiline regex literals
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1352 }), ['perl', 'pl', 'pm']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1353 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1354 'keywords': RUBY_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1355 'hashComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1356 'multiLineStrings': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1357 'regexLiterals': true
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1358 }), ['rb', 'ruby']);
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1359 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1360 'keywords': JSCRIPT_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1361 'cStyleComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1362 'regexLiterals': true
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1363 }), ['javascript', 'js']);
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1364 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1365 'keywords': COFFEE_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1366 'hashComments': 3, // ### style block comments
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1367 'cStyleComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1368 'multilineStrings': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1369 'tripleQuotedStrings': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1370 'regexLiterals': true
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1371 }), ['coffee']);
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1372 registerLangHandler(sourceDecorator({
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1373 'keywords': RUST_KEYWORDS,
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1374 'cStyleComments': true,
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1375 'multilineStrings': true
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1376 }), ['rc', 'rs', 'rust']);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1377 registerLangHandler(
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1378 createSimpleLexer([], [[PR_STRING, /^[\s\S]+/]]), ['regex']);
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1379
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1380 function applyDecorator(job) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1381 var opt_langExtension = job.langExtension;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1382
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1383 try {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1384 // Extract tags, and convert the source code to plain text.
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1385 var sourceAndSpans = extractSourceSpans(job.sourceNode, job.pre);
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1386 /** Plain text. @type {string} */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1387 var source = sourceAndSpans.sourceCode;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1388 job.sourceCode = source;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1389 job.spans = sourceAndSpans.spans;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1390 job.basePos = 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1391
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1392 // Apply the appropriate language handler
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1393 langHandlerForExtension(opt_langExtension, source)(job);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1394
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1395 // Integrate the decorations and tags back into the source code,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1396 // modifying the sourceNode in place.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1397 recombineTagsAndDecorations(job);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1398 } catch (e) {
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1399 if (win['console']) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1400 console['log'](e && e['stack'] || e);
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1401 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1402 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1403 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1404
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1405 /**
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1406 * Pretty print a chunk of code.
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1407 * @param sourceCodeHtml {string} The HTML to pretty print.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1408 * @param opt_langExtension {string} The language name to use.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1409 * Typically, a filename extension like 'cpp' or 'java'.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1410 * @param opt_numberLines {number|boolean} True to number lines,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1411 * or the 1-indexed number of the first line in sourceCodeHtml.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1412 */
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1413 function $prettyPrintOne(sourceCodeHtml, opt_langExtension, opt_numberLines) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1414 var container = document.createElement('div');
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1415 // This could cause images to load and onload listeners to fire.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1416 // E.g. <img onerror="alert(1337)" src="nosuchimage.png">.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1417 // We assume that the inner HTML is from a trusted source.
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1418 // The pre-tag is required for IE8 which strips newlines from innerHTML
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1419 // when it is injected into a <pre> tag.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1420 // http://stackoverflow.com/questions/451486/pre-tag-loses-line-breaks-when-setting-innerhtml-in-ie
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1421 // http://stackoverflow.com/questions/195363/inserting-a-newline-into-a-pre-tag-ie-javascript
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1422 container.innerHTML = '<pre>' + sourceCodeHtml + '</pre>';
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1423 container = container.firstChild;
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1424 if (opt_numberLines) {
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1425 numberLines(container, opt_numberLines, true);
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1426 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1427
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1428 var job = {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1429 langExtension: opt_langExtension,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1430 numberLines: opt_numberLines,
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1431 sourceNode: container,
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1432 pre: 1
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1433 };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1434 applyDecorator(job);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1435 return container.innerHTML;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1436 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1437
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1438 /**
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1439 * Find all the {@code <pre>} and {@code <code>} tags in the DOM with
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1440 * {@code class=prettyprint} and prettify them.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1441 *
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1442 * @param {Function} opt_whenDone called when prettifying is done.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1443 * @param {HTMLElement|HTMLDocument} opt_root an element or document
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1444 * containing all the elements to pretty print.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1445 * Defaults to {@code document.body}.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1446 */
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1447 function $prettyPrint(opt_whenDone, opt_root) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1448 var root = opt_root || document.body;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1449 var doc = root.ownerDocument || document;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1450 function byTagName(tn) { return root.getElementsByTagName(tn); }
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1451 // fetch a list of nodes to rewrite
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1452 var codeSegments = [byTagName('pre'), byTagName('code'), byTagName('xmp')];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1453 var elements = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1454 for (var i = 0; i < codeSegments.length; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1455 for (var j = 0, n = codeSegments[i].length; j < n; ++j) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1456 elements.push(codeSegments[i][j]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1457 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1458 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1459 codeSegments = null;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1460
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1461 var clock = Date;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1462 if (!clock['now']) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1463 clock = { 'now': function () { return +(new Date); } };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1464 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1465
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1466 // The loop is broken into a series of continuations to make sure that we
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1467 // don't make the browser unresponsive when rewriting a large page.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1468 var k = 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1469 var prettyPrintingJob;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1470
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1471 var langExtensionRe = /\blang(?:uage)?-([\w.]+)(?!\S)/;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1472 var prettyPrintRe = /\bprettyprint\b/;
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1473 var prettyPrintedRe = /\bprettyprinted\b/;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1474 var preformattedTagNameRe = /pre|xmp/i;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1475 var codeRe = /^code$/i;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1476 var preCodeXmpRe = /^(?:pre|code|xmp)$/i;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1477 var EMPTY = {};
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1478
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1479 function doWork() {
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1480 var endTime = (win['PR_SHOULD_USE_CONTINUATION'] ?
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1481 clock['now']() + 250 /* ms */ :
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1482 Infinity);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1483 for (; k < elements.length && clock['now']() < endTime; k++) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1484 var cs = elements[k];
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1485
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1486 // Look for a preceding comment like
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1487 // <?prettify lang="..." linenums="..."?>
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1488 var attrs = EMPTY;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1489 {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1490 for (var preceder = cs; (preceder = preceder.previousSibling);) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1491 var nt = preceder.nodeType;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1492 // <?foo?> is parsed by HTML 5 to a comment node (8)
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1493 // like <!--?foo?-->, but in XML is a processing instruction
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1494 var value = (nt === 7 || nt === 8) && preceder.nodeValue;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1495 if (value
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1496 ? !/^\??prettify\b/.test(value)
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1497 : (nt !== 3 || /\S/.test(preceder.nodeValue))) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1498 // Skip over white-space text nodes but not others.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1499 break;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1500 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1501 if (value) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1502 attrs = {};
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1503 value.replace(
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1504 /\b(\w+)=([\w:.%+-]+)/g,
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1505 function (_, name, value) { attrs[name] = value; });
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1506 break;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1507 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1508 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1509 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1510
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1511 var className = cs.className;
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1512 if ((attrs !== EMPTY || prettyPrintRe.test(className))
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1513 // Don't redo this if we've already done it.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1514 // This allows recalling pretty print to just prettyprint elements
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1515 // that have been added to the page since last call.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1516 && !prettyPrintedRe.test(className)) {
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1517
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1518 // make sure this is not nested in an already prettified element
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1519 var nested = false;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1520 for (var p = cs.parentNode; p; p = p.parentNode) {
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1521 var tn = p.tagName;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1522 if (preCodeXmpRe.test(tn)
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1523 && p.className && prettyPrintRe.test(p.className)) {
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1524 nested = true;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1525 break;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1526 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1527 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1528 if (!nested) {
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1529 // Mark done. If we fail to prettyprint for whatever reason,
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1530 // we shouldn't try again.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1531 cs.className += ' prettyprinted';
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1532
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1533 // If the classes includes a language extensions, use it.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1534 // Language extensions can be specified like
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1535 // <pre class="prettyprint lang-cpp">
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1536 // the language extension "cpp" is used to find a language handler
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1537 // as passed to PR.registerLangHandler.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1538 // HTML5 recommends that a language be specified using "language-"
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1539 // as the prefix instead. Google Code Prettify supports both.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1540 // http://dev.w3.org/html5/spec-author-view/the-code-element.html
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1541 var langExtension = attrs['lang'];
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1542 if (!langExtension) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1543 langExtension = className.match(langExtensionRe);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1544 // Support <pre class="prettyprint"><code class="language-c">
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1545 var wrapper;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1546 if (!langExtension && (wrapper = childContentWrapper(cs))
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1547 && codeRe.test(wrapper.tagName)) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1548 langExtension = wrapper.className.match(langExtensionRe);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1549 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1550
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1551 if (langExtension) { langExtension = langExtension[1]; }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1552 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1553
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1554 var preformatted;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1555 if (preformattedTagNameRe.test(cs.tagName)) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1556 preformatted = 1;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1557 } else {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1558 var currentStyle = cs['currentStyle'];
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1559 var defaultView = doc.defaultView;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1560 var whitespace = (
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1561 currentStyle
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1562 ? currentStyle['whiteSpace']
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1563 : (defaultView
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1564 && defaultView.getComputedStyle)
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1565 ? defaultView.getComputedStyle(cs, null)
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1566 .getPropertyValue('white-space')
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1567 : 0);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1568 preformatted = whitespace
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1569 && 'pre' === whitespace.substring(0, 3);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1570 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1571
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1572 // Look for a class like linenums or linenums:<n> where <n> is the
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1573 // 1-indexed number of the first line.
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1574 var lineNums = attrs['linenums'];
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1575 if (!(lineNums = lineNums === 'true' || +lineNums)) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1576 lineNums = className.match(/\blinenums\b(?::(\d+))?/);
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1577 lineNums =
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1578 lineNums
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1579 ? lineNums[1] && lineNums[1].length
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1580 ? +lineNums[1] : true
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1581 : false;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1582 }
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1583 if (lineNums) { numberLines(cs, lineNums, preformatted); }
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1584
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1585 // do the pretty printing
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1586 prettyPrintingJob = {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1587 langExtension: langExtension,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1588 sourceNode: cs,
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1589 numberLines: lineNums,
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1590 pre: preformatted
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1591 };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1592 applyDecorator(prettyPrintingJob);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1593 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1594 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1595 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1596 if (k < elements.length) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1597 // finish up in a continuation
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1598 setTimeout(doWork, 250);
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1599 } else if ('function' === typeof opt_whenDone) {
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1600 opt_whenDone();
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1601 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1602 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1603
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1604 doWork();
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1605 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1606
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1607 /**
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1608 * Contains functions for creating and registering new language handlers.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1609 * @type {Object}
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1610 */
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1611 var PR = win['PR'] = {
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1612 'createSimpleLexer': createSimpleLexer,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1613 'registerLangHandler': registerLangHandler,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1614 'sourceDecorator': sourceDecorator,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1615 'PR_ATTRIB_NAME': PR_ATTRIB_NAME,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1616 'PR_ATTRIB_VALUE': PR_ATTRIB_VALUE,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1617 'PR_COMMENT': PR_COMMENT,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1618 'PR_DECLARATION': PR_DECLARATION,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1619 'PR_KEYWORD': PR_KEYWORD,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1620 'PR_LITERAL': PR_LITERAL,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1621 'PR_NOCODE': PR_NOCODE,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1622 'PR_PLAIN': PR_PLAIN,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1623 'PR_PUNCTUATION': PR_PUNCTUATION,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1624 'PR_SOURCE': PR_SOURCE,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1625 'PR_STRING': PR_STRING,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1626 'PR_TAG': PR_TAG,
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1627 'PR_TYPE': PR_TYPE,
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1628 'prettyPrintOne':
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1629 IN_GLOBAL_SCOPE
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1630 ? (win['prettyPrintOne'] = $prettyPrintOne)
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1631 : (prettyPrintOne = $prettyPrintOne),
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1632 'prettyPrint': prettyPrint =
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1633 IN_GLOBAL_SCOPE
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1634 ? (win['prettyPrint'] = $prettyPrint)
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1635 : (prettyPrint = $prettyPrint)
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1636 };
220
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1637
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1638 // Make PR available via the Asynchronous Module Definition (AMD) API.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1639 // Per https://github.com/amdjs/amdjs-api/wiki/AMD:
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1640 // The Asynchronous Module Definition (AMD) API specifies a
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1641 // mechanism for defining modules such that the module and its
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1642 // dependencies can be asynchronously loaded.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1643 // ...
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1644 // To allow a clear indicator that a global define function (as
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1645 // needed for script src browser loading) conforms to the AMD API,
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1646 // any global define function SHOULD have a property called "amd"
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1647 // whose value is an object. This helps avoid conflict with any
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1648 // other existing JavaScript code that could have defined a define()
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1649 // function that does not conform to the AMD API.
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1650 if (typeof define === "function" && define['amd']) {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1651 define("google-code-prettify", [], function () {
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1652 return PR;
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1653 });
183e8560a337 Update prettify to 2013/03/04.
Edho Arief <edho@myconan.net>
parents: 219
diff changeset
1654 }
58
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1655 })();