annotate lib/assets/javascripts/prettify.js @ 147:812711965af1

Added support for disabling supported web server without changing Gemfile.
author Edho Arief <edho@myconan.net>
date Sun, 18 Nov 2012 22:57:36 +0700
parents 33b47c2160a3
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
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
56 /*global console, document, navigator, setTimeout, window */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
57
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
58 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
59 * 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
60 * UI events.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
61 * 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
62 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
63 window['PR_SHOULD_USE_CONTINUATION'] = true;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
64
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
65 (function () {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
66 // Keyword lists for various languages.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
67 // 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
68 // 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
69 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
70 var C_KEYWORDS = [FLOW_CONTROL_KEYWORDS,"auto,case,char,const,default," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
71 "double,enum,extern,float,goto,int,long,register,short,signed,sizeof," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
72 "static,struct,switch,typedef,union,unsigned,void,volatile"];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
73 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
74 "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
75 var CPP_KEYWORDS = [COMMON_KEYWORDS,"alignof,align_union,asm,axiom,bool," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
76 "concept,concept_map,const_cast,constexpr,decltype," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
77 "dynamic_cast,explicit,export,friend,inline,late_check," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
78 "mutable,namespace,nullptr,reinterpret_cast,static_assert,static_cast," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
79 "template,typeid,typename,using,virtual,where"];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
80 var JAVA_KEYWORDS = [COMMON_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
81 "abstract,boolean,byte,extends,final,finally,implements,import," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
82 "instanceof,null,native,package,strictfp,super,synchronized,throws," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
83 "transient"];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
84 var CSHARP_KEYWORDS = [JAVA_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
85 "as,base,by,checked,decimal,delegate,descending,dynamic,event," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
86 "fixed,foreach,from,group,implicit,in,interface,internal,into,is,lock," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
87 "object,out,override,orderby,params,partial,readonly,ref,sbyte,sealed," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
88 "stackalloc,string,select,uint,ulong,unchecked,unsafe,ushort,var"];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
89 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
90 "for,if,in,is,isnt,loop,new,no,not,null,of,off,on,or,return,super,then," +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
91 "true,try,unless,until,when,while,yes";
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
92 var JSCRIPT_KEYWORDS = [COMMON_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
93 "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
94 "Infinity,NaN"];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
95 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
96 "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
97 "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
98 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
99 "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
100 "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
101 "False,True,None"];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
102 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
103 "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
104 "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
105 "BEGIN,END"];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
106 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
107 "function,in,local,set,then,until"];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
108 var ALL_KEYWORDS = [
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
109 CPP_KEYWORDS, CSHARP_KEYWORDS, JSCRIPT_KEYWORDS, PERL_KEYWORDS +
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
110 PYTHON_KEYWORDS, RUBY_KEYWORDS, SH_KEYWORDS];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
111 var C_TYPES = /^(DIR|FILE|vector|(de|priority_)?queue|list|stack|(const_)?iterator|(multi)?(set|map)|bitset|u?(int|float)\d*)/;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
112
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
113 // token style names. correspond to css classes
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
114 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
115 * token style for a string literal
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
116 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
117 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
118 var PR_STRING = 'str';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
119 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
120 * token style for a keyword
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
121 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
122 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
123 var PR_KEYWORD = 'kwd';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
124 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
125 * token style for a comment
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
126 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
127 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
128 var PR_COMMENT = 'com';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
129 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
130 * token style for a type
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
131 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
132 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
133 var PR_TYPE = 'typ';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
134 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
135 * 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
136 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
137 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
138 var PR_LITERAL = 'lit';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
139 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
140 * token style for a punctuation string.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
141 * @const
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 var PR_PUNCTUATION = 'pun';
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 punctuation string.
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_PLAIN = 'pln';
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 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
151 * token style for an sgml tag.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
152 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
153 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
154 var PR_TAG = 'tag';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
155 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
156 * 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
157 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
158 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
159 var PR_DECLARATION = 'dec';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
160 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
161 * token style for embedded source.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
162 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
163 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
164 var PR_SOURCE = 'src';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
165 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
166 * token style for an sgml attribute name.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
167 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
168 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
169 var PR_ATTRIB_NAME = 'atn';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
170 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
171 * token style for an sgml attribute value.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
172 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
173 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
174 var PR_ATTRIB_VALUE = 'atv';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
175
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
176 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
177 * 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
178 * embedding of line numbers within code listings.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
179 * @const
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 var PR_NOCODE = 'nocode';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
182
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
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 * A set of tokens that can precede a regular expression literal in
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
187 * javascript
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
188 * http://web.archive.org/web/20070717142515/http://www.mozilla.org/js/language/js20/rationale/syntax.html
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
189 * has the full list, but I've removed ones that might be problematic when
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
190 * seen in languages that don't support regular expression literals.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
191 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
192 * <p>Specifically, I've removed any keywords that can't precede a regexp
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
193 * literal in a syntactically legal javascript program, and I've removed the
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
194 * "in" keyword since it's not a keyword in many languages, and might be used
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
195 * as a count of inches.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
196 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
197 * <p>The link a above does not accurately describe EcmaScript rules since
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
198 * it fails to distinguish between (a=++/b/i) and (a++/b/i) but it works
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
199 * very well in practice.
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 * @private
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 REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|\\!|\\!=|\\!==|\\#|\\%|\\%=|&|&&|&&=|&=|\\(|\\*|\\*=|\\+=|\\,|\\-=|\\->|\\/|\\/=|:|::|\\;|<|<<|<<=|<=|=|==|===|>|>=|>>|>>=|>>>|>>>=|\\?|\\@|\\[|\\^|\\^=|\\^\\^|\\^\\^=|\\{|\\||\\|=|\\|\\||\\|\\|=|\\~|break|case|continue|delete|do|else|finally|instanceof|return|throw|try|typeof)\\s*';
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 // CAVEAT: this does not properly handle the case where a regular
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
207 // expression immediately follows another since a regular expression may
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
208 // have flags for case-sensitivity and the like. Having regexp tokens
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
209 // adjacent is not valid in any language I'm aware of, so I'm punting.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
210 // TODO: maybe style special characters inside a regexp as punctuation.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
211
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
212
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
213 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
214 * 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
215 * 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
216 * 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
217 * 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
218 * @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
219 * @return {RegExp} a global regex.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
220 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
221 function combinePrefixPatterns(regexs) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
222 var capturedGroupIndex = 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
223
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
224 var needToFoldCase = false;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
225 var ignoreCase = false;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
226 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
227 var regex = regexs[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
228 if (regex.ignoreCase) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
229 ignoreCase = true;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
230 } 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
231 /\\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
232 needToFoldCase = true;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
233 ignoreCase = false;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
234 break;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
235 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
236 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
237
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
238 var escapeCharToCodeUnit = {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
239 'b': 8,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
240 't': 9,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
241 'n': 0xa,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
242 'v': 0xb,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
243 'f': 0xc,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
244 'r': 0xd
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
245 };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
246
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
247 function decodeEscape(charsetPart) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
248 var cc0 = charsetPart.charCodeAt(0);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
249 if (cc0 !== 92 /* \\ */) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
250 return cc0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
251 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
252 var c1 = charsetPart.charAt(1);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
253 cc0 = escapeCharToCodeUnit[c1];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
254 if (cc0) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
255 return cc0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
256 } else if ('0' <= c1 && c1 <= '7') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
257 return parseInt(charsetPart.substring(1), 8);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
258 } else if (c1 === 'u' || c1 === 'x') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
259 return parseInt(charsetPart.substring(2), 16);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
260 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
261 return charsetPart.charCodeAt(1);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
262 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
263 }
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 function encodeEscape(charCode) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
266 if (charCode < 0x20) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
267 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
268 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
269 var ch = String.fromCharCode(charCode);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
270 if (ch === '\\' || ch === '-' || ch === '[' || ch === ']') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
271 ch = '\\' + ch;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
272 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
273 return ch;
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 caseFoldCharset(charSet) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
277 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
278 new RegExp(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
279 '\\\\u[0-9A-Fa-f]{4}'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
280 + '|\\\\x[0-9A-Fa-f]{2}'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
281 + '|\\\\[0-3][0-7]{0,2}'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
282 + '|\\\\[0-7]{1,2}'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
283 + '|\\\\[\\s\\S]'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
284 + '|-'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
285 + '|[^-\\\\]',
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
286 'g'));
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
287 var groups = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
288 var ranges = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
289 var inverse = charsetParts[0] === '^';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
290 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
291 var p = charsetParts[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
292 if (/\\[bdsw]/i.test(p)) { // Don't muck with named groups.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
293 groups.push(p);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
294 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
295 var start = decodeEscape(p);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
296 var end;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
297 if (i + 2 < n && '-' === charsetParts[i + 1]) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
298 end = decodeEscape(charsetParts[i + 2]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
299 i += 2;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
300 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
301 end = start;
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 ranges.push([start, end]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
304 // 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
305 // This case handling is too simplistic.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
306 // 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
307 // 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
308 if (!(end < 65 || start > 122)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
309 if (!(end < 65 || start > 90)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
310 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
311 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
312 if (!(end < 97 || start > 122)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
313 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
314 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
315 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
316 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
317 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
318
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
319 // [[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
320 // -> [[1, 12], [14, 14], [16, 17]]
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
321 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
322 var consolidatedRanges = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
323 var lastRange = [NaN, NaN];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
324 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
325 var range = ranges[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
326 if (range[0] <= lastRange[1] + 1) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
327 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
328 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
329 consolidatedRanges.push(lastRange = range);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
330 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
331 }
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 var out = ['['];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
334 if (inverse) { out.push('^'); }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
335 out.push.apply(out, groups);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
336 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
337 var range = consolidatedRanges[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
338 out.push(encodeEscape(range[0]));
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
339 if (range[1] > range[0]) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
340 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
341 out.push(encodeEscape(range[1]));
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
342 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
343 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
344 out.push(']');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
345 return out.join('');
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 function allowAnywhereFoldCaseAndRenumberGroups(regex) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
349 // 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
350 // 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
351 // include any of the above.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
352 var parts = regex.source.match(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
353 new RegExp(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
354 '(?:'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
355 + '\\[(?:[^\\x5C\\x5D]|\\\\[\\s\\S])*\\]' // a character set
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
356 + '|\\\\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
357 + '|\\\\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
358 + '|\\\\[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
359 + '|\\\\[^ux0-9]' // other escape sequence
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
360 + '|\\(\\?[:!=]' // start of a non-capturing group
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
361 + '|[\\(\\)\\^]' // start/emd of a group, or line start
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
362 + '|[^\\x5B\\x5C\\(\\)\\^]+' // run of other characters
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
363 + ')',
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
364 'g'));
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
365 var n = parts.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
366
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
367 // 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
368 // 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
369 // 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
370 var capturedGroups = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
371
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
372 // 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
373 // mapping.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
374 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
375 var p = parts[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
376 if (p === '(') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
377 // 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
378 ++groupIndex;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
379 } else if ('\\' === p.charAt(0)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
380 var decimalValue = +p.substring(1);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
381 if (decimalValue && decimalValue <= groupIndex) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
382 capturedGroups[decimalValue] = -1;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
383 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
384 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
385 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
386
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
387 // 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
388 // where possible.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
389 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
390 if (-1 === capturedGroups[i]) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
391 capturedGroups[i] = ++capturedGroupIndex;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
392 }
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 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
395 var p = parts[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
396 if (p === '(') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
397 ++groupIndex;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
398 if (capturedGroups[groupIndex] === undefined) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
399 parts[i] = '(?:';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
400 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
401 } else if ('\\' === p.charAt(0)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
402 var decimalValue = +p.substring(1);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
403 if (decimalValue && decimalValue <= groupIndex) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
404 parts[i] = '\\' + capturedGroups[groupIndex];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
405 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
406 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
407 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
408
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
409 // 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
410 // ^^ really does mean an anchored match though.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
411 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
412 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
413 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
414
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
415 // 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
416 // case-insensitive patterns if necessary.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
417 if (regex.ignoreCase && needToFoldCase) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
418 for (var i = 0; i < n; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
419 var p = parts[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
420 var ch0 = p.charAt(0);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
421 if (p.length >= 2 && ch0 === '[') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
422 parts[i] = caseFoldCharset(p);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
423 } else if (ch0 !== '\\') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
424 // TODO: handle letters in numeric escapes.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
425 parts[i] = p.replace(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
426 /[a-zA-Z]/g,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
427 function (ch) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
428 var cc = ch.charCodeAt(0);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
429 return '[' + String.fromCharCode(cc & ~32, cc | 32) + ']';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
430 });
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
431 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
432 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
433 }
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 return parts.join('');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
436 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
437
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
438 var rewritten = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
439 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
440 var regex = regexs[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
441 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
442 rewritten.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
443 '(?:' + allowAnywhereFoldCaseAndRenumberGroups(regex) + ')');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
444 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
445
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
446 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
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
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
450 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
451 * 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
452 * 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
453 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
454 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
455 * The HTML DOM structure:</p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
456 * <pre>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
457 * (Element "p"
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
458 * (Element "b"
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
459 * (Text "print ")) ; #1
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
460 * (Text "'Hello '") ; #2
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
461 * (Element "br") ; #3
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
462 * (Text " + 'World';")) ; #4
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
463 * </pre>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
464 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
465 * corresponds to the HTML
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
466 * {@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
467 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
468 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
469 * It will produce the output:</p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
470 * <pre>
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 * sourceCode: "print 'Hello '\n + 'World';",
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
473 * // 1 2
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
474 * // 012345678901234 5678901234567
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
475 * 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
476 * }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
477 * </pre>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
478 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
479 * 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
480 * on for the other text nodes.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
481 * </p>
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 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
484 * 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
485 * 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
486 * that contain the text for those substrings.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
487 * 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
488 * </p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
489 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
490 * @param {Node} node an HTML DOM subtree containing source-code.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
491 * @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
492 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
493 function extractSourceSpans(node) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
494 var nocode = /(?:^|\s)nocode(?:\s|$)/;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
495
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
496 var chunks = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
497 var length = 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
498 var spans = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
499 var k = 0;
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 var whitespace;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
502 if (node.currentStyle) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
503 whitespace = node.currentStyle.whiteSpace;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
504 } else if (window.getComputedStyle) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
505 whitespace = document.defaultView.getComputedStyle(node, null)
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
506 .getPropertyValue('white-space');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
507 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
508 var isPreformatted = whitespace && 'pre' === whitespace.substring(0, 3);
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 function walk(node) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
511 switch (node.nodeType) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
512 case 1: // Element
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
513 if (nocode.test(node.className)) { return; }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
514 for (var child = node.firstChild; child; child = child.nextSibling) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
515 walk(child);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
516 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
517 var nodeName = node.nodeName;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
518 if ('BR' === nodeName || 'LI' === nodeName) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
519 chunks[k] = '\n';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
520 spans[k << 1] = length++;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
521 spans[(k++ << 1) | 1] = node;
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 break;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
524 case 3: case 4: // Text
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
525 var text = node.nodeValue;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
526 if (text.length) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
527 if (!isPreformatted) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
528 text = text.replace(/[ \t\r\n]+/g, ' ');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
529 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
530 text = text.replace(/\r\n?/g, '\n'); // Normalize newlines.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
531 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
532 // TODO: handle tabs here?
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
533 chunks[k] = text;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
534 spans[k << 1] = length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
535 length += text.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
536 spans[(k++ << 1) | 1] = node;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
537 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
538 break;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
539 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
540 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
541
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
542 walk(node);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
543
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
544 return {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
545 sourceCode: chunks.join('').replace(/\n$/, ''),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
546 spans: spans
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
547 };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
548 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
549
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
550
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
551 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
552 * 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
553 * decorations to out.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
554 * @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
555 * whose decorations are already present on out.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
556 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
557 function appendDecorations(basePos, sourceCode, langHandler, out) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
558 if (!sourceCode) { return; }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
559 var job = {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
560 sourceCode: sourceCode,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
561 basePos: basePos
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
562 };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
563 langHandler(job);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
564 out.push.apply(out, job.decorations);
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
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
567 var notWs = /\S/;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
568
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
569 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
570 * 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
571 * 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
572 * Otherwise returns undefined.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
573 * <p>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
574 * 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
575 * 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
576 * 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
577 * 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
578 * is textual content.
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 childContentWrapper(element) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
581 var wrapper = undefined;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
582 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
583 var type = c.nodeType;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
584 wrapper = (type === 1) // Element Node
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
585 ? (wrapper ? element : c)
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
586 : (type === 3) // Text Node
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
587 ? (notWs.test(c.nodeValue) ? element : wrapper)
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
588 : wrapper;
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 return wrapper === element ? undefined : wrapper;
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 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
594 * 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
595 * returns a decoration list of the form
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
596 * [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
597 * 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
598 * 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
599 * 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
600 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
601 * 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
602 * [style : string, pattern : RegExp, DEPRECATED, shortcut : string].
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
603 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
604 * 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
605 * 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
606 * 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
607 * 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
608 * '(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
609 * registered lisp handler for formatting.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
610 * 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
611 * 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
612 * 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
613 * 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
614 * '<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
615 * 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
616 * 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
617 * 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
618 * 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
619 * 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
620 * 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
621 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
622 * 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
623 * 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
624 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
625 * 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
626 * recognized.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
627 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
628 * 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
629 * 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
630 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
631 * @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
632 * 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
633 * @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
634 * 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
635 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
636 * @return {function (Object)} a
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
637 * 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
638 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
639 function createSimpleLexer(shortcutStylePatterns, fallthroughStylePatterns) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
640 var shortcuts = {};
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
641 var tokenizer;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
642 (function () {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
643 var allPatterns = shortcutStylePatterns.concat(fallthroughStylePatterns);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
644 var allRegexs = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
645 var regexKeys = {};
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
646 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
647 var patternParts = allPatterns[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
648 var shortcutChars = patternParts[3];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
649 if (shortcutChars) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
650 for (var c = shortcutChars.length; --c >= 0;) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
651 shortcuts[shortcutChars.charAt(c)] = patternParts;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
652 }
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 var regex = patternParts[1];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
655 var k = '' + regex;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
656 if (!regexKeys.hasOwnProperty(k)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
657 allRegexs.push(regex);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
658 regexKeys[k] = null;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
659 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
660 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
661 allRegexs.push(/[\0-\uffff]/);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
662 tokenizer = combinePrefixPatterns(allRegexs);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
663 })();
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
664
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
665 var nPatterns = fallthroughStylePatterns.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
666
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
667 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
668 * 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
669 * 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
670 * job.sourceCode in order.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
671 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
672 * @param {Object} job an object like <pre>{
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
673 * sourceCode: {string} sourceText plain text,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
674 * 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
675 * sourceCode.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
676 * }</pre>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
677 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
678 var decorate = function (job) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
679 var sourceCode = job.sourceCode, basePos = job.basePos;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
680 /** 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
681 * 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
682 * the end.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
683 * @type {Array.<number|string>}
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
684 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
685 var decorations = [basePos, PR_PLAIN];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
686 var pos = 0; // index into sourceCode
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
687 var tokens = sourceCode.match(tokenizer) || [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
688 var styleCache = {};
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 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
691 var token = tokens[ti];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
692 var style = styleCache[token];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
693 var match = void 0;
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 var isEmbedded;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
696 if (typeof style === 'string') {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
697 isEmbedded = false;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
698 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
699 var patternParts = shortcuts[token.charAt(0)];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
700 if (patternParts) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
701 match = token.match(patternParts[1]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
702 style = patternParts[0];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
703 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
704 for (var i = 0; i < nPatterns; ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
705 patternParts = fallthroughStylePatterns[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
706 match = token.match(patternParts[1]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
707 if (match) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
708 style = patternParts[0];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
709 break;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
710 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
711 }
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 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
714 style = PR_PLAIN;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
715 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
716 }
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 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
719 if (isEmbedded && !(match && typeof match[1] === '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 style = PR_SOURCE;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
722 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
723
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
724 if (!isEmbedded) { styleCache[token] = style; }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
725 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
726
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
727 var tokenStart = pos;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
728 pos += token.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
729
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
730 if (!isEmbedded) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
731 decorations.push(basePos + tokenStart, style);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
732 } 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
733 var embeddedSource = match[1];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
734 var embeddedSourceStart = token.indexOf(embeddedSource);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
735 var embeddedSourceEnd = embeddedSourceStart + embeddedSource.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
736 if (match[2]) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
737 // 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
738 // 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
739 // 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
740 embeddedSourceEnd = token.length - match[2].length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
741 embeddedSourceStart = embeddedSourceEnd - embeddedSource.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
742 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
743 var lang = style.substring(5);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
744 // Decorate the left of the embedded source
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
745 appendDecorations(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
746 basePos + tokenStart,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
747 token.substring(0, embeddedSourceStart),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
748 decorate, decorations);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
749 // Decorate the embedded source
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
750 appendDecorations(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
751 basePos + tokenStart + embeddedSourceStart,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
752 embeddedSource,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
753 langHandlerForExtension(lang, embeddedSource),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
754 decorations);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
755 // Decorate the right of the embedded section
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
756 appendDecorations(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
757 basePos + tokenStart + embeddedSourceEnd,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
758 token.substring(embeddedSourceEnd),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
759 decorate, decorations);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
760 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
761 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
762 job.decorations = decorations;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
763 };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
764 return decorate;
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
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
767 /** 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
768 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
769 * 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
770 * 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
771 * 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
772 * 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
773 * 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
774 * multiple adjacent string literals.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
775 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
776 * 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
777 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
778 * @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
779 * @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
780 * 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
781 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
782 function sourceDecorator(options) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
783 var shortcutStylePatterns = [], fallthroughStylePatterns = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
784 if (options['tripleQuotedStrings']) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
785 // '''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
786 shortcutStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
787 [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
788 null, '\'"']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
789 } else if (options['multiLineStrings']) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
790 // 'multi-line-string', "multi-line-string"
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
791 shortcutStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
792 [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
793 null, '\'"`']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
794 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
795 // 'single-line-string', "single-line-string"
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
796 shortcutStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
797 [PR_STRING,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
798 /^(?:\'(?:[^\\\'\r\n]|\\.)*(?:\'|$)|\"(?:[^\\\"\r\n]|\\.)*(?:\"|$))/,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
799 null, '"\'']);
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 if (options['verbatimStrings']) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
802 // 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
803 fallthroughStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
804 [PR_STRING, /^@\"(?:[^\"]|\"\")*(?:\"|$)/, null]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
805 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
806 var hc = options['hashComments'];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
807 if (hc) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
808 if (options['cStyleComments']) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
809 if (hc > 1) { // multiline hash comments
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
810 shortcutStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
811 [PR_COMMENT, /^#(?:##(?:[^#]|#(?!##))*(?:###|$)|.*)/, null, '#']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
812 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
813 // 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
814 shortcutStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
815 [PR_COMMENT, /^#(?:(?:define|elif|else|endif|error|ifdef|include|ifndef|line|pragma|undef|warning)\b|[^\r\n]*)/,
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 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
818 fallthroughStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
819 [PR_STRING,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
820 /^<(?:(?:(?:\.\.\/)*|\/?)(?:[\w-]+(?:\/[\w-]+)+)?[\w-]+\.h|[a-z]\w*)>/,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
821 null]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
822 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
823 shortcutStylePatterns.push([PR_COMMENT, /^#[^\r\n]*/, null, '#']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
824 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
825 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
826 if (options['cStyleComments']) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
827 fallthroughStylePatterns.push([PR_COMMENT, /^\/\/[^\r\n]*/, null]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
828 fallthroughStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
829 [PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
830 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
831 if (options['regexLiterals']) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
832 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
833 * @const
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
834 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
835 var REGEX_LITERAL = (
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
836 // 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
837 // 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
838 // comments.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
839 '/(?=[^/*])'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
840 // and then contains any number of raw characters,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
841 + '(?:[^/\\x5B\\x5C]'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
842 // escape sequences (\x5C),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
843 + '|\\x5C[\\s\\S]'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
844 // or non-nesting character sets (\x5B\x5D);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
845 + '|\\x5B(?:[^\\x5C\\x5D]|\\x5C[\\s\\S])*(?:\\x5D|$))+'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
846 // finally closed by a /.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
847 + '/');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
848 fallthroughStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
849 ['lang-regex',
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
850 new RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
851 ]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
852 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
853
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
854 var types = options['types'];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
855 if (types) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
856 fallthroughStylePatterns.push([PR_TYPE, types]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
857 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
858
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
859 var keywords = ("" + options['keywords']).replace(/^ | $/g, '');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
860 if (keywords.length) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
861 fallthroughStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
862 [PR_KEYWORD,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
863 new RegExp('^(?:' + keywords.replace(/[\s,]+/g, '|') + ')\\b'),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
864 null]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
865 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
866
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
867 shortcutStylePatterns.push([PR_PLAIN, /^\s+/, null, ' \r\n\t\xA0']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
868 fallthroughStylePatterns.push(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
869 // 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
870 [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
871 [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
872 [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
873 [PR_LITERAL,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
874 new RegExp(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
875 '^(?:'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
876 // A hex number
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
877 + '0x[a-f0-9]+'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
878 // or an octal or decimal number,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
879 + '|(?:\\d(?:_\\d+)*\\d*(?:\\.\\d*)?|\\.\\d\\+)'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
880 // possibly in scientific notation
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
881 + '(?:e[+\\-]?\\d+)?'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
882 + ')'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
883 // 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
884 + '[a-z]*', 'i'),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
885 null, '0123456789'],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
886 // Don't treat escaped quotes in bash as starting strings. See issue 144.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
887 [PR_PLAIN, /^\\[\s\S]?/, null],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
888 [PR_PUNCTUATION, /^.[^\s\w\.$@\'\"\`\/\#\\]*/, null]);
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 return createSimpleLexer(shortcutStylePatterns, fallthroughStylePatterns);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
891 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
892
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
893 var decorateSource = sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
894 'keywords': ALL_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
895 'hashComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
896 'cStyleComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
897 'multiLineStrings': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
898 'regexLiterals': true
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
899 });
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
900
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 * 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
903 * list item.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
904 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
905 * @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
906 * 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
907 * 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
908 * IDs after numbering.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
909 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
910 function numberLines(node, opt_startLineNum) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
911 var nocode = /(?:^|\s)nocode(?:\s|$)/;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
912 var lineBreak = /\r\n?|\n/;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
913
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
914 var document = node.ownerDocument;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
915
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
916 var whitespace;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
917 if (node.currentStyle) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
918 whitespace = node.currentStyle.whiteSpace;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
919 } else if (window.getComputedStyle) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
920 whitespace = document.defaultView.getComputedStyle(node, null)
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
921 .getPropertyValue('white-space');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
922 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
923 // If it's preformatted, then we need to split lines on line breaks
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
924 // in addition to <BR>s.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
925 var isPreformatted = whitespace && 'pre' === whitespace.substring(0, 3);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
926
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
927 var li = document.createElement('LI');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
928 while (node.firstChild) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
929 li.appendChild(node.firstChild);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
930 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
931 // 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
932 // un-split line.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
933 var listItems = [li];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
934
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
935 function walk(node) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
936 switch (node.nodeType) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
937 case 1: // Element
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
938 if (nocode.test(node.className)) { break; }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
939 if ('BR' === node.nodeName) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
940 breakAfter(node);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
941 // Discard the <BR> since it is now flush against a </LI>.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
942 if (node.parentNode) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
943 node.parentNode.removeChild(node);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
944 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
945 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
946 for (var child = node.firstChild; child; child = child.nextSibling) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
947 walk(child);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
948 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
949 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
950 break;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
951 case 3: case 4: // Text
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
952 if (isPreformatted) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
953 var text = node.nodeValue;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
954 var match = text.match(lineBreak);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
955 if (match) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
956 var firstLine = text.substring(0, match.index);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
957 node.nodeValue = firstLine;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
958 var tail = text.substring(match.index + match[0].length);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
959 if (tail) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
960 var parent = node.parentNode;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
961 parent.insertBefore(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
962 document.createTextNode(tail), node.nextSibling);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
963 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
964 breakAfter(node);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
965 if (!firstLine) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
966 // Don't leave blank text nodes in the DOM.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
967 node.parentNode.removeChild(node);
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 }
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 break;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
972 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
973 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
974
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
975 // Split a line after the given node.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
976 function breakAfter(lineEndNode) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
977 // 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
978 // 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
979 // 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
980 while (!lineEndNode.nextSibling) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
981 lineEndNode = lineEndNode.parentNode;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
982 if (!lineEndNode) { return; }
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
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
985 function breakLeftOf(limit, copy) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
986 // 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
987 var rightSide = copy ? limit.cloneNode(false) : limit;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
988 var parent = limit.parentNode;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
989 if (parent) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
990 // We clone the parent chain.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
991 // 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
992 // 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
993 // 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
994 var parentClone = breakLeftOf(parent, 1);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
995 // 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
996 // onto the cloned parent.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
997 var next = limit.nextSibling;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
998 parentClone.appendChild(rightSide);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
999 for (var sibling = next; sibling; sibling = next) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1000 next = sibling.nextSibling;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1001 parentClone.appendChild(sibling);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1002 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1003 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1004 return rightSide;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1005 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1006
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1007 var copiedListItem = breakLeftOf(lineEndNode.nextSibling, 0);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1008
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1009 // 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
1010 for (var parent;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1011 // Check nodeType since IE invents document fragments.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1012 (parent = copiedListItem.parentNode) && parent.nodeType === 1;) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1013 copiedListItem = parent;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1014 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1015 // 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
1016 listItems.push(copiedListItem);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1017 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1018
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1019 // 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
1020 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
1021 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
1022 ++i) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1023 walk(listItems[i]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1024 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1025
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1026 // Make sure numeric indices show correctly.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1027 if (opt_startLineNum === (opt_startLineNum|0)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1028 listItems[0].setAttribute('value', opt_startLineNum);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1029 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1030
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1031 var ol = document.createElement('OL');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1032 ol.className = 'linenums';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1033 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
1034 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
1035 li = listItems[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1036 // 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
1037 // 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
1038 // is co-prime with 10.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1039 li.className = 'L' + ((i + offset) % 10);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1040 if (!li.firstChild) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1041 li.appendChild(document.createTextNode('\xA0'));
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1042 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1043 ol.appendChild(li);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1044 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1045
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1046 node.appendChild(ol);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1047 }
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 * 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
1051 * {@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
1052 * @param {Object} job like <pre>{
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1053 * sourceCode: {string} source as plain text,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1054 * 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
1055 * 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
1056 * span.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1057 * 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
1058 * 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
1059 * }</pre>
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1060 * @private
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1061 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1062 function recombineTagsAndDecorations(job) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1063 var isIE = /\bMSIE\b/.test(navigator.userAgent);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1064 var newlineRe = /\n/g;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1065
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1066 var source = job.sourceCode;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1067 var sourceLength = source.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1068 // 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
1069 var sourceIndex = 0;
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 var spans = job.spans;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1072 var nSpans = spans.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1073 // 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
1074 var spanIndex = 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1075
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1076 var decorations = job.decorations;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1077 var nDecorations = decorations.length;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1078 // 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
1079 // sourceIndex.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1080 var decorationIndex = 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1081
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1082 // Remove all zero-length decorations.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1083 decorations[nDecorations] = sourceLength;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1084 var decPos, i;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1085 for (i = decPos = 0; i < nDecorations;) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1086 if (decorations[i] !== decorations[i + 2]) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1087 decorations[decPos++] = decorations[i++];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1088 decorations[decPos++] = decorations[i++];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1089 } else {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1090 i += 2;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1091 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1092 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1093 nDecorations = decPos;
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 // Simplify decorations.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1096 for (i = decPos = 0; i < nDecorations;) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1097 var startPos = decorations[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1098 // 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
1099 var startDec = decorations[i + 1];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1100 var end = i + 2;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1101 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
1102 end += 2;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1103 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1104 decorations[decPos++] = startPos;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1105 decorations[decPos++] = startDec;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1106 i = end;
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
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1109 nDecorations = decorations.length = decPos;
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 var decoration = null;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1112 while (spanIndex < nSpans) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1113 var spanStart = spans[spanIndex];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1114 var spanEnd = spans[spanIndex + 2] || sourceLength;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1115
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1116 var decStart = decorations[decorationIndex];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1117 var decEnd = decorations[decorationIndex + 2] || sourceLength;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1118
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1119 var end = Math.min(spanEnd, decEnd);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1120
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1121 var textNode = spans[spanIndex + 1];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1122 var styledText;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1123 if (textNode.nodeType !== 1 // Don't muck with <BR>s or <LI>s
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1124 // Don't introduce spans around empty text nodes.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1125 && (styledText = source.substring(sourceIndex, end))) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1126 // This may seem bizarre, and it is. Emitting LF on IE causes the
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1127 // code to display with spaces instead of line breaks.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1128 // Emitting Windows standard issue linebreaks (CRLF) causes a blank
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1129 // space to appear at the beginning of every line but the first.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1130 // Emitting an old Mac OS 9 line separator makes everything spiffy.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1131 if (isIE) { styledText = styledText.replace(newlineRe, '\r'); }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1132 textNode.nodeValue = styledText;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1133 var document = textNode.ownerDocument;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1134 var span = document.createElement('SPAN');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1135 span.className = decorations[decorationIndex + 1];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1136 var parentNode = textNode.parentNode;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1137 parentNode.replaceChild(span, textNode);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1138 span.appendChild(textNode);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1139 if (sourceIndex < spanEnd) { // Split off a text node.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1140 spans[spanIndex + 1] = textNode
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1141 // TODO: Possibly optimize by using '' if there's no flicker.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1142 = document.createTextNode(source.substring(end, spanEnd));
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1143 parentNode.insertBefore(textNode, span.nextSibling);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1144 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1145 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1146
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1147 sourceIndex = end;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1148
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1149 if (sourceIndex >= spanEnd) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1150 spanIndex += 2;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1151 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1152 if (sourceIndex >= decEnd) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1153 decorationIndex += 2;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1154 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1155 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1156 }
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 /** Maps language-specific file extensions to handlers. */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1160 var langHandlerRegistry = {};
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1161 /** 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
1162 * @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
1163 * 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
1164 * 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
1165 * {@code {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1166 * sourceCode: {string} as plain text.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1167 * 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
1168 * 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
1169 * job.sourceCode in order.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1170 * The language handler should assigned this field.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1171 * 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
1172 * 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
1173 * to the larger source chunk.
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 * @param {Array.<string>} fileExtensions
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1176 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1177 function registerLangHandler(handler, fileExtensions) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1178 for (var i = fileExtensions.length; --i >= 0;) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1179 var ext = fileExtensions[i];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1180 if (!langHandlerRegistry.hasOwnProperty(ext)) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1181 langHandlerRegistry[ext] = handler;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1182 } else if (window['console']) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1183 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
1184 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1185 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1186 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1187 function langHandlerForExtension(extension, source) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1188 if (!(extension && langHandlerRegistry.hasOwnProperty(extension))) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1189 // 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
1190 // the last non-whitespace character is a >.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1191 extension = /^\s*</.test(source)
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1192 ? 'default-markup'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1193 : 'default-code';
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1194 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1195 return langHandlerRegistry[extension];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1196 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1197 registerLangHandler(decorateSource, ['default-code']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1198 registerLangHandler(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1199 createSimpleLexer(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1200 [],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1201 [
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1202 [PR_PLAIN, /^[^<?]+/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1203 [PR_DECLARATION, /^<!\w[^>]*(?:>|$)/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1204 [PR_COMMENT, /^<\!--[\s\S]*?(?:-\->|$)/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1205 // Unescaped content in an unknown language
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1206 ['lang-', /^<\?([\s\S]+?)(?:\?>|$)/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1207 ['lang-', /^<%([\s\S]+?)(?:%>|$)/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1208 [PR_PUNCTUATION, /^(?:<[%?]|[%?]>)/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1209 ['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
1210 // Unescaped content in javascript. (Or possibly vbscript).
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1211 ['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
1212 // Contains unescaped stylesheet content
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1213 ['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
1214 ['lang-in.tag', /^(<\/?[a-z][^<>]*>)/i]
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1215 ]),
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1216 ['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
1217 registerLangHandler(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1218 createSimpleLexer(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1219 [
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1220 [PR_PLAIN, /^[\s]+/, null, ' \t\r\n'],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1221 [PR_ATTRIB_VALUE, /^(?:\"[^\"]*\"?|\'[^\']*\'?)/, null, '\"\'']
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1222 ],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1223 [
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1224 [PR_TAG, /^^<\/?[a-z](?:[\w.:-]*\w)?|\/?>$/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1225 [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
1226 ['lang-uq.val', /^=\s*([^>\'\"\s]*(?:[^>\'\"\s\/]|\/(?=\s)))/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1227 [PR_PUNCTUATION, /^[=<>\/]+/],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1228 ['lang-js', /^on\w+\s*=\s*\"([^\"]+)\"/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1229 ['lang-js', /^on\w+\s*=\s*\'([^\']+)\'/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1230 ['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
1231 ['lang-css', /^style\s*=\s*\"([^\"]+)\"/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1232 ['lang-css', /^style\s*=\s*\'([^\']+)\'/i],
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1233 ['lang-css', /^style\s*=\s*([^\"\'>\s]+)/i]
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 ['in.tag']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1236 registerLangHandler(
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1237 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
1238 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1239 'keywords': CPP_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1240 'hashComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1241 'cStyleComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1242 'types': C_TYPES
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1243 }), ['c', 'cc', 'cpp', 'cxx', 'cyc', 'm']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1244 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1245 'keywords': 'null,true,false'
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1246 }), ['json']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1247 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1248 'keywords': CSHARP_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1249 'hashComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1250 'cStyleComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1251 'verbatimStrings': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1252 'types': C_TYPES
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1253 }), ['cs']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1254 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1255 'keywords': JAVA_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1256 'cStyleComments': true
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1257 }), ['java']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1258 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1259 'keywords': SH_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1260 'hashComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1261 'multiLineStrings': true
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1262 }), ['bsh', 'csh', 'sh']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1263 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1264 'keywords': PYTHON_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1265 'hashComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1266 'multiLineStrings': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1267 'tripleQuotedStrings': true
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1268 }), ['cv', 'py']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1269 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1270 'keywords': PERL_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1271 'hashComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1272 'multiLineStrings': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1273 'regexLiterals': true
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1274 }), ['perl', 'pl', 'pm']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1275 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1276 'keywords': RUBY_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1277 'hashComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1278 'multiLineStrings': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1279 'regexLiterals': true
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1280 }), ['rb']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1281 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1282 'keywords': JSCRIPT_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1283 'cStyleComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1284 'regexLiterals': true
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1285 }), ['js']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1286 registerLangHandler(sourceDecorator({
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1287 'keywords': COFFEE_KEYWORDS,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1288 'hashComments': 3, // ### style block comments
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1289 'cStyleComments': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1290 'multilineStrings': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1291 'tripleQuotedStrings': true,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1292 'regexLiterals': true
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1293 }), ['coffee']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1294 registerLangHandler(createSimpleLexer([], [[PR_STRING, /^[\s\S]+/]]), ['regex']);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1295
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1296 function applyDecorator(job) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1297 var opt_langExtension = job.langExtension;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1298
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1299 try {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1300 // Extract tags, and convert the source code to plain text.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1301 var sourceAndSpans = extractSourceSpans(job.sourceNode);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1302 /** Plain text. @type {string} */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1303 var source = sourceAndSpans.sourceCode;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1304 job.sourceCode = source;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1305 job.spans = sourceAndSpans.spans;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1306 job.basePos = 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1307
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1308 // Apply the appropriate language handler
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1309 langHandlerForExtension(opt_langExtension, source)(job);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1310
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1311 // 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
1312 // modifying the sourceNode in place.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1313 recombineTagsAndDecorations(job);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1314 } catch (e) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1315 if ('console' in window) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1316 console['log'](e && e['stack'] ? e['stack'] : e);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1317 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1318 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1319 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1320
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1321 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1322 * @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
1323 * @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
1324 * 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
1325 * @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
1326 * 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
1327 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1328 function prettyPrintOne(sourceCodeHtml, opt_langExtension, opt_numberLines) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1329 var container = document.createElement('PRE');
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1330 // 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
1331 // 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
1332 // We assume that the inner HTML is from a trusted source.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1333 container.innerHTML = sourceCodeHtml;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1334 if (opt_numberLines) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1335 numberLines(container, opt_numberLines);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1336 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1337
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1338 var job = {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1339 langExtension: opt_langExtension,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1340 numberLines: opt_numberLines,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1341 sourceNode: container
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1342 };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1343 applyDecorator(job);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1344 return container.innerHTML;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1345 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1346
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1347 function prettyPrint(opt_whenDone) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1348 function byTagName(tn) { return document.getElementsByTagName(tn); }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1349 // fetch a list of nodes to rewrite
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1350 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
1351 var elements = [];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1352 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
1353 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
1354 elements.push(codeSegments[i][j]);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1355 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1356 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1357 codeSegments = null;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1358
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1359 var clock = Date;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1360 if (!clock['now']) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1361 clock = { 'now': function () { return +(new Date); } };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1362 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1363
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1364 // 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
1365 // 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
1366 var k = 0;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1367 var prettyPrintingJob;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1368
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1369 var langExtensionRe = /\blang(?:uage)?-([\w.]+)(?!\S)/;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1370 var prettyPrintRe = /\bprettyprint\b/;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1371
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1372 function doWork() {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1373 var endTime = (window['PR_SHOULD_USE_CONTINUATION'] ?
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1374 clock['now']() + 250 /* ms */ :
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1375 Infinity);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1376 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
1377 var cs = elements[k];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1378 var className = cs.className;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1379 if (className.indexOf('prettyprint') >= 0) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1380 // If the classes includes a language extensions, use it.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1381 // Language extensions can be specified like
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1382 // <pre class="prettyprint lang-cpp">
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1383 // the language extension "cpp" is used to find a language handler as
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1384 // passed to PR.registerLangHandler.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1385 // HTML5 recommends that a language be specified using "language-"
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1386 // as the prefix instead. Google Code Prettify supports both.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1387 // http://dev.w3.org/html5/spec-author-view/the-code-element.html
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1388 var langExtension = className.match(langExtensionRe);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1389 // Support <pre class="prettyprint"><code class="language-c">
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1390 var wrapper;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1391 if (!langExtension && (wrapper = childContentWrapper(cs))
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1392 && "CODE" === wrapper.tagName) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1393 langExtension = wrapper.className.match(langExtensionRe);
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
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1396 if (langExtension) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1397 langExtension = langExtension[1];
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1398 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1399
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1400 // 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
1401 var nested = false;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1402 for (var p = cs.parentNode; p; p = p.parentNode) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1403 if ((p.tagName === 'pre' || p.tagName === 'code' ||
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1404 p.tagName === 'xmp') &&
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1405 p.className && p.className.indexOf('prettyprint') >= 0) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1406 nested = true;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1407 break;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1408 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1409 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1410 if (!nested) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1411 // 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
1412 // 1-indexed number of the first line.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1413 var lineNums = cs.className.match(/\blinenums\b(?::(\d+))?/);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1414 lineNums = lineNums
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1415 ? lineNums[1] && lineNums[1].length ? +lineNums[1] : true
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1416 : false;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1417 if (lineNums) { numberLines(cs, lineNums); }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1418
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1419 // do the pretty printing
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1420 prettyPrintingJob = {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1421 langExtension: langExtension,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1422 sourceNode: cs,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1423 numberLines: lineNums
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1424 };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1425 applyDecorator(prettyPrintingJob);
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 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1429 if (k < elements.length) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1430 // finish up in a continuation
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1431 setTimeout(doWork, 250);
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1432 } else if (opt_whenDone) {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1433 opt_whenDone();
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1434 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1435 }
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 doWork();
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1438 }
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1439
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1440 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1441 * Find all the {@code <pre>} and {@code <code>} tags in the DOM with
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1442 * {@code class=prettyprint} and prettify them.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1443 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1444 * @param {Function?} opt_whenDone if specified, called when the last entry
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1445 * has been finished.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1446 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1447 window['prettyPrintOne'] = prettyPrintOne;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1448 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1449 * Pretty print a chunk of code.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1450 *
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1451 * @param {string} sourceCodeHtml code as html
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1452 * @return {string} code as html, but prettier
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1453 */
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1454 window['prettyPrint'] = prettyPrint;
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1455 /**
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1456 * Contains functions for creating and registering new language handlers.
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1457 * @type {Object}
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 window['PR'] = {
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1460 'createSimpleLexer': createSimpleLexer,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1461 'registerLangHandler': registerLangHandler,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1462 'sourceDecorator': sourceDecorator,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1463 'PR_ATTRIB_NAME': PR_ATTRIB_NAME,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1464 'PR_ATTRIB_VALUE': PR_ATTRIB_VALUE,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1465 'PR_COMMENT': PR_COMMENT,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1466 'PR_DECLARATION': PR_DECLARATION,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1467 'PR_KEYWORD': PR_KEYWORD,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1468 'PR_LITERAL': PR_LITERAL,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1469 'PR_NOCODE': PR_NOCODE,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1470 'PR_PLAIN': PR_PLAIN,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1471 'PR_PUNCTUATION': PR_PUNCTUATION,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1472 'PR_SOURCE': PR_SOURCE,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1473 'PR_STRING': PR_STRING,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1474 'PR_TAG': PR_TAG,
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1475 'PR_TYPE': PR_TYPE
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1476 };
33b47c2160a3 Use mainline prettify instead of stealing from bootstrap.
Edho Arief <edho@myconan.net>
parents:
diff changeset
1477 })();