annotate trunk/.svn/text-base/prettify.js.svn-base @ 0:845ff8ff4fc9

1st
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Tue, 14 Jan 2014 01:31:42 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 // Copyright (C) 2006 Google Inc.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // Licensed under the Apache License, Version 2.0 (the "License");
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 // you may not use this file except in compliance with the License.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // You may obtain a copy of the License at
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 // http://www.apache.org/licenses/LICENSE-2.0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 //
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 // Unless required by applicable law or agreed to in writing, software
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 // distributed under the License is distributed on an "AS IS" BASIS,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 // See the License for the specific language governing permissions and
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 // limitations under the License.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 /**
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 * @fileoverview
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 * some functions for browser-side pretty printing of code contained in html.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 * <p>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 * For a fairly comprehensive set of languages see the
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 * <a href="http://google-code-prettify.googlecode.com/svn/trunk/README.html#langs">README</a>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 * file that came with this source. At a minimum, the lexer should work on a
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 * number of languages including C and friends, Java, Python, Bash, SQL, HTML,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 * XML, CSS, Javascript, and Makefiles. It works passably on Ruby, PHP and Awk
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 * and a subset of Perl, but, because of commenting conventions, doesn't work on
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 * Smalltalk, Lisp-like, or CAML-like languages without an explicit lang class.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 * <p>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 * Usage: <ol>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 * <li> include this source file in an html page via
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 * {@code <script type="text/javascript" src="/path/to/prettify.js"></script>}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 * <li> define style rules. See the example page for examples.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 * <li> mark the {@code <pre>} and {@code <code>} tags in your source with
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 * {@code class=prettyprint.}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 * You can also use the (html deprecated) {@code <xmp>} tag, but the pretty
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 * printer needs to do more substantial DOM manipulations to support that, so
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 * some css styles may not be preserved.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 * </ol>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 * That's it. I wanted to keep the API as simple as possible, so there's no
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 * need to specify which language the code is in, but if you wish, you can add
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 * another class to the {@code <pre>} or {@code <code>} element to specify the
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 * language, as in {@code <pre class="prettyprint lang-java">}. Any class that
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 * starts with "lang-" followed by a file extension, specifies the file type.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 * See the "lang-*.js" files in this directory for code that implements
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 * per-language file handlers.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 * <p>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 * Change log:<br>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 * cbeust, 2006/08/22
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 * <blockquote>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 * Java annotations (start with "@") are now captured as literals ("lit")
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 * </blockquote>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 * @requires console
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 // JSLint declarations
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 /*global console, document, navigator, setTimeout, window */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 /**
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 * Split {@code prettyPrint} into multiple timeouts so as not to interfere with
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 * UI events.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 * If set to {@code false}, {@code prettyPrint()} is synchronous.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 window['PR_SHOULD_USE_CONTINUATION'] = true;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 /** the number of characters between tab columns */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 window['PR_TAB_WIDTH'] = 8;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 /** Contains functions for creating and registering new language handlers.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 * @type {Object}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 window['PR']
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 /** Pretty print a chunk of code.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 * @param {string} sourceCodeHtml code as html
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 * @return {string} code as html, but prettier
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 = window['prettyPrintOne']
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 /** Find all the {@code <pre>} and {@code <code>} tags in the DOM with
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 * {@code class=prettyprint} and prettify them.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 * @param {Function?} opt_whenDone if specified, called when the last entry
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 * has been finished.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 = window['prettyPrint'] = void 0;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 (function () {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 // Keyword lists for various languages.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 var FLOW_CONTROL_KEYWORDS =
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 "break continue do else for if return while ";
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 var C_KEYWORDS = FLOW_CONTROL_KEYWORDS + "auto case char const default " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 "double enum extern float goto int long register short signed sizeof " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 "static struct switch typedef union unsigned void volatile ";
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 var COMMON_KEYWORDS = C_KEYWORDS + "catch class delete false import " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 "new operator private protected public this throw true try typeof ";
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 var CPP_KEYWORDS = COMMON_KEYWORDS + "alignof align_union asm axiom bool " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 "concept concept_map const_cast constexpr decltype " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 "dynamic_cast explicit export friend inline late_check " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 "mutable namespace nullptr reinterpret_cast static_assert static_cast " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 "template typeid typename using virtual wchar_t where ";
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 var JAVA_KEYWORDS = COMMON_KEYWORDS +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 "abstract boolean byte extends final finally implements import " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 "instanceof null native package strictfp super synchronized throws " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 "transient ";
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 var CSHARP_KEYWORDS = JAVA_KEYWORDS +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 "as base by checked decimal delegate descending dynamic event " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 "fixed foreach from group implicit in interface internal into is lock " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 "object out override orderby params partial readonly ref sbyte sealed " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 "stackalloc string select uint ulong unchecked unsafe ushort var ";
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 var COFFEE_KEYWORDS = "all and by catch class else extends false finally " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 "for if in is isnt loop new no not null of off on or return super then " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 "true try unless until when while yes ";
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 var JSCRIPT_KEYWORDS = COMMON_KEYWORDS +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 "debugger eval export function get null set undefined var with " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 "Infinity NaN ";
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 var PERL_KEYWORDS = "caller delete die do dump elsif eval exit foreach for " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 "goto if import last local my next no our print package redo require " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 "sub undef unless until use wantarray while BEGIN END ";
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 var PYTHON_KEYWORDS = FLOW_CONTROL_KEYWORDS + "and as assert class def del " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 "elif except exec finally from global import in is lambda " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 "nonlocal not or pass print raise try with yield " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 "False True None ";
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 var RUBY_KEYWORDS = FLOW_CONTROL_KEYWORDS + "alias and begin case class def" +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 " defined elsif end ensure false in module next nil not or redo rescue " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 "retry self super then true undef unless until when yield BEGIN END ";
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 var SH_KEYWORDS = FLOW_CONTROL_KEYWORDS + "case done elif esac eval fi " +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 "function in local set then until ";
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 var ALL_KEYWORDS = (
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 CPP_KEYWORDS + CSHARP_KEYWORDS + JSCRIPT_KEYWORDS + PERL_KEYWORDS +
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 PYTHON_KEYWORDS + RUBY_KEYWORDS + SH_KEYWORDS);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 // token style names. correspond to css classes
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 /** token style for a string literal */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 var PR_STRING = 'str';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 /** token style for a keyword */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 var PR_KEYWORD = 'kwd';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 /** token style for a comment */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 var PR_COMMENT = 'com';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 /** token style for a type */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 var PR_TYPE = 'typ';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 /** token style for a literal value. e.g. 1, null, true. */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 var PR_LITERAL = 'lit';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 /** token style for a punctuation string. */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 var PR_PUNCTUATION = 'pun';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 /** token style for a punctuation string. */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 var PR_PLAIN = 'pln';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
147
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 /** token style for an sgml tag. */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 var PR_TAG = 'tag';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 /** token style for a markup declaration such as a DOCTYPE. */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 var PR_DECLARATION = 'dec';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 /** token style for embedded source. */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 var PR_SOURCE = 'src';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 /** token style for an sgml attribute name. */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 var PR_ATTRIB_NAME = 'atn';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
156 /** token style for an sgml attribute value. */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
157 var PR_ATTRIB_VALUE = 'atv';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
158
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 /**
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
160 * A class that indicates a section of markup that is not code, e.g. to allow
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 * embedding of line numbers within code listings.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
162 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 var PR_NOCODE = 'nocode';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
164
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
165 /** A set of tokens that can precede a regular expression literal in
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 * javascript.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 * http://www.mozilla.org/js/language/js20/rationale/syntax.html has the full
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 * list, but I've removed ones that might be problematic when seen in
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
169 * languages that don't support regular expression literals.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 * <p>Specifically, I've removed any keywords that can't precede a regexp
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 * literal in a syntactically legal javascript program, and I've removed the
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 * "in" keyword since it's not a keyword in many languages, and might be used
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 * as a count of inches.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
175 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 * <p>The link a above does not accurately describe EcmaScript rules since
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 * it fails to distinguish between (a=++/b/i) and (a++/b/i) but it works
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 * very well in practice.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 * @private
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 var REGEXP_PRECEDER_PATTERN = function () {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 var preceders = [
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 "!", "!=", "!==", "#", "%", "%=", "&", "&&", "&&=",
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 "&=", "(", "*", "*=", /* "+", */ "+=", ",", /* "-", */ "-=",
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 "->", /*".", "..", "...", handled below */ "/", "/=", ":", "::", ";",
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
187 "<", "<<", "<<=", "<=", "=", "==", "===", ">",
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 ">=", ">>", ">>=", ">>>", ">>>=", "?", "@", "[",
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 "^", "^=", "^^", "^^=", "{", "|", "|=", "||",
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 "||=", "~" /* handles =~ and !~ */,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 "break", "case", "continue", "delete",
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 "do", "else", "finally", "instanceof",
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 "return", "throw", "try", "typeof"
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 ];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 var pattern = '(?:^^|[+-]';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 for (var i = 0; i < preceders.length; ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 pattern += '|' + preceders[i].replace(/([^=<>:&a-z])/g, '\\$1');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 pattern += ')\\s*'; // matches at end, and matches empty string
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 return pattern;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 // CAVEAT: this does not properly handle the case where a regular
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
202 // expression immediately follows another since a regular expression may
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
203 // have flags for case-sensitivity and the like. Having regexp tokens
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
204 // adjacent is not valid in any language I'm aware of, so I'm punting.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 // TODO: maybe style special characters inside a regexp as punctuation.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 }();
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
207
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 /**
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 * Given a group of {@link RegExp}s, returns a {@code RegExp} that globally
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 * matches the union of the sets of strings matched by the input RegExp.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
212 * Since it matches globally, if the input strings have a start-of-input
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 * anchor (/^.../), it is ignored for the purposes of unioning.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 * @param {Array.<RegExp>} regexs non multiline, non-global regexs.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 * @return {RegExp} a global regex.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 function combinePrefixPatterns(regexs) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 var capturedGroupIndex = 0;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
219
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 var needToFoldCase = false;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 var ignoreCase = false;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 for (var i = 0, n = regexs.length; i < n; ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 var regex = regexs[i];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 if (regex.ignoreCase) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 ignoreCase = true;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
226 } else if (/[a-z]/i.test(regex.source.replace(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 /\\u[0-9a-f]{4}|\\x[0-9a-f]{2}|\\[^ux]/gi, ''))) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
228 needToFoldCase = true;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 ignoreCase = false;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 break;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
232 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
233
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 function decodeEscape(charsetPart) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 if (charsetPart.charAt(0) !== '\\') { return charsetPart.charCodeAt(0); }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 switch (charsetPart.charAt(1)) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
237 case 'b': return 8;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 case 't': return 9;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
239 case 'n': return 0xa;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 case 'v': return 0xb;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 case 'f': return 0xc;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 case 'r': return 0xd;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 case 'u': case 'x':
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
244 return parseInt(charsetPart.substring(2), 16)
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
245 || charsetPart.charCodeAt(1);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
246 case '0': case '1': case '2': case '3': case '4':
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 case '5': case '6': case '7':
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
248 return parseInt(charsetPart.substring(1), 8);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 default: return charsetPart.charCodeAt(1);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
251 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
252
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
253 function encodeEscape(charCode) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
254 if (charCode < 0x20) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
255 return (charCode < 0x10 ? '\\x0' : '\\x') + charCode.toString(16);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
256 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 var ch = String.fromCharCode(charCode);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
258 if (ch === '\\' || ch === '-' || ch === '[' || ch === ']') {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
259 ch = '\\' + ch;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
260 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 return ch;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
262 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
263
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 function caseFoldCharset(charSet) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
265 var charsetParts = charSet.substring(1, charSet.length - 1).match(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
266 new RegExp(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 '\\\\u[0-9A-Fa-f]{4}'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
268 + '|\\\\x[0-9A-Fa-f]{2}'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 + '|\\\\[0-3][0-7]{0,2}'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
270 + '|\\\\[0-7]{1,2}'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
271 + '|\\\\[\\s\\S]'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 + '|-'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
273 + '|[^-\\\\]',
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
274 'g'));
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
275 var groups = [];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 var ranges = [];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 var inverse = charsetParts[0] === '^';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
278 for (var i = inverse ? 1 : 0, n = charsetParts.length; i < n; ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 var p = charsetParts[i];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
280 switch (p) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
281 case '\\B': case '\\b':
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 case '\\D': case '\\d':
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
283 case '\\S': case '\\s':
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
284 case '\\W': case '\\w':
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
285 groups.push(p);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
286 continue;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
287 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 var start = decodeEscape(p);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 var end;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
290 if (i + 2 < n && '-' === charsetParts[i + 1]) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
291 end = decodeEscape(charsetParts[i + 2]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 i += 2;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 } else {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
294 end = start;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
295 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
296 ranges.push([start, end]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
297 // If the range might intersect letters, then expand it.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 if (!(end < 65 || start > 122)) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 if (!(end < 65 || start > 90)) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
300 ranges.push([Math.max(65, start) | 32, Math.min(end, 90) | 32]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
301 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
302 if (!(end < 97 || start > 122)) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
303 ranges.push([Math.max(97, start) & ~32, Math.min(end, 122) & ~32]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
304 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
305 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
306 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
307
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
308 // [[1, 10], [3, 4], [8, 12], [14, 14], [16, 16], [17, 17]]
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
309 // -> [[1, 12], [14, 14], [16, 17]]
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
310 ranges.sort(function (a, b) { return (a[0] - b[0]) || (b[1] - a[1]); });
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
311 var consolidatedRanges = [];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
312 var lastRange = [NaN, NaN];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
313 for (var i = 0; i < ranges.length; ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
314 var range = ranges[i];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
315 if (range[0] <= lastRange[1] + 1) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
316 lastRange[1] = Math.max(lastRange[1], range[1]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
317 } else {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
318 consolidatedRanges.push(lastRange = range);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
319 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
321
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
322 var out = ['['];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
323 if (inverse) { out.push('^'); }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
324 out.push.apply(out, groups);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 for (var i = 0; i < consolidatedRanges.length; ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
326 var range = consolidatedRanges[i];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
327 out.push(encodeEscape(range[0]));
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
328 if (range[1] > range[0]) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
329 if (range[1] + 1 > range[0]) { out.push('-'); }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
330 out.push(encodeEscape(range[1]));
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
331 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
332 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
333 out.push(']');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 return out.join('');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
336
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
337 function allowAnywhereFoldCaseAndRenumberGroups(regex) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
338 // Split into character sets, escape sequences, punctuation strings
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
339 // like ('(', '(?:', ')', '^'), and runs of characters that do not
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
340 // include any of the above.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
341 var parts = regex.source.match(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 new RegExp(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
343 '(?:'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
344 + '\\[(?:[^\\x5C\\x5D]|\\\\[\\s\\S])*\\]' // a character set
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
345 + '|\\\\u[A-Fa-f0-9]{4}' // a unicode escape
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
346 + '|\\\\x[A-Fa-f0-9]{2}' // a hex escape
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
347 + '|\\\\[0-9]+' // a back-reference or octal escape
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
348 + '|\\\\[^ux0-9]' // other escape sequence
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
349 + '|\\(\\?[:!=]' // start of a non-capturing group
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
350 + '|[\\(\\)\\^]' // start/emd of a group, or line start
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
351 + '|[^\\x5B\\x5C\\(\\)\\^]+' // run of other characters
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
352 + ')',
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
353 'g'));
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
354 var n = parts.length;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
355
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
356 // Maps captured group numbers to the number they will occupy in
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
357 // the output or to -1 if that has not been determined, or to
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
358 // undefined if they need not be capturing in the output.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
359 var capturedGroups = [];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
360
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
361 // Walk over and identify back references to build the capturedGroups
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
362 // mapping.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
363 for (var i = 0, groupIndex = 0; i < n; ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
364 var p = parts[i];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
365 if (p === '(') {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
366 // groups are 1-indexed, so max group index is count of '('
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
367 ++groupIndex;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
368 } else if ('\\' === p.charAt(0)) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
369 var decimalValue = +p.substring(1);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
370 if (decimalValue && decimalValue <= groupIndex) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
371 capturedGroups[decimalValue] = -1;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
372 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
373 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
374 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
375
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
376 // Renumber groups and reduce capturing groups to non-capturing groups
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
377 // where possible.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
378 for (var i = 1; i < capturedGroups.length; ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
379 if (-1 === capturedGroups[i]) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
380 capturedGroups[i] = ++capturedGroupIndex;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
381 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
382 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
383 for (var i = 0, groupIndex = 0; i < n; ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
384 var p = parts[i];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
385 if (p === '(') {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
386 ++groupIndex;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
387 if (capturedGroups[groupIndex] === undefined) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
388 parts[i] = '(?:';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
389 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
390 } else if ('\\' === p.charAt(0)) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
391 var decimalValue = +p.substring(1);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
392 if (decimalValue && decimalValue <= groupIndex) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
393 parts[i] = '\\' + capturedGroups[groupIndex];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
394 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
395 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
396 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
397
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
398 // Remove any prefix anchors so that the output will match anywhere.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
399 // ^^ really does mean an anchored match though.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
400 for (var i = 0, groupIndex = 0; i < n; ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
401 if ('^' === parts[i] && '^' !== parts[i + 1]) { parts[i] = ''; }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
402 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
403
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
404 // Expand letters to groups to handle mixing of case-sensitive and
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
405 // case-insensitive patterns if necessary.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
406 if (regex.ignoreCase && needToFoldCase) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
407 for (var i = 0; i < n; ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
408 var p = parts[i];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
409 var ch0 = p.charAt(0);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
410 if (p.length >= 2 && ch0 === '[') {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
411 parts[i] = caseFoldCharset(p);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
412 } else if (ch0 !== '\\') {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
413 // TODO: handle letters in numeric escapes.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
414 parts[i] = p.replace(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
415 /[a-zA-Z]/g,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
416 function (ch) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
417 var cc = ch.charCodeAt(0);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
418 return '[' + String.fromCharCode(cc & ~32, cc | 32) + ']';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
419 });
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
420 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
421 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
422 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
423
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
424 return parts.join('');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
425 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
426
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
427 var rewritten = [];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
428 for (var i = 0, n = regexs.length; i < n; ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
429 var regex = regexs[i];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
430 if (regex.global || regex.multiline) { throw new Error('' + regex); }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
431 rewritten.push(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
432 '(?:' + allowAnywhereFoldCaseAndRenumberGroups(regex) + ')');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
433 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
434
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
435 return new RegExp(rewritten.join('|'), ignoreCase ? 'gi' : 'g');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
436 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
437
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
438
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
439 /**
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
440 * Split markup into a string of source code and an array mapping ranges in
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
441 * that string to the text nodes in which they appear.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
442 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
443 * <p>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
444 * The HTML DOM structure:</p>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
445 * <pre>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
446 * (Element "p"
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
447 * (Element "b"
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
448 * (Text "print ")) ; #1
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
449 * (Text "'Hello '") ; #2
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
450 * (Element "br") ; #3
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
451 * (Text " + 'World';")) ; #4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
452 * </pre>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
453 * <p>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
454 * corresponds to the HTML
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
455 * {@code <p><b>print </b>'Hello '<br> + 'World';</p>}.</p>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
456 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
457 * <p>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
458 * It will produce the output:</p>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
459 * <pre>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
460 * {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
461 * source: "print 'Hello '\n + 'World';",
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
462 * // 1 2
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
463 * // 012345678901234 5678901234567
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
464 * spans: [0, #1, 6, #2, 14, #3, 15, #4]
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
465 * }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
466 * </pre>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
467 * <p>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
468 * where #1 is a reference to the {@code "print "} text node above, and so
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
469 * on for the other text nodes.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
470 * </p>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
471 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
472 * <p>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
473 * The {@code} spans array is an array of pairs. Even elements are the start
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
474 * indices of substrings, and odd elements are the text nodes (or BR elements)
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
475 * that contain the text for those substrings.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
476 * Substrings continue until the next index or the end of the source.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
477 * </p>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
478 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
479 * @param {Node} node an HTML DOM subtree containing source-code.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
480 * @return {Object} source code and the text nodes in which they occur.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
481 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
482 function extractSourceSpans(node) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
483 var nocode = /(?:^|\s)nocode(?:\s|$)/;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
484
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
485 var chunks = [];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
486 var length = 0;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
487 var spans = [];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
488 var k = 0;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
489
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
490 var whitespace;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
491 if (node.currentStyle) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
492 whitespace = node.currentStyle.whiteSpace;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
493 } else if (window.getComputedStyle) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
494 whitespace = document.defaultView.getComputedStyle(node, null)
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
495 .getPropertyValue('white-space');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
496 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
497 var isPreformatted = whitespace && 'pre' === whitespace.substring(0, 3);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
498
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
499 function walk(node) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
500 switch (node.nodeType) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
501 case 1: // Element
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
502 if (nocode.test(node.className)) { return; }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
503 for (var child = node.firstChild; child; child = child.nextSibling) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
504 walk(child);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
505 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
506 var nodeName = node.nodeName;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
507 if ('BR' === nodeName || 'LI' === nodeName) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
508 chunks[k] = '\n';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
509 spans[k << 1] = length++;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
510 spans[(k++ << 1) | 1] = node;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
511 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
512 break;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
513 case 3: case 4: // Text
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
514 var text = node.nodeValue;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
515 if (text.length) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
516 if (!isPreformatted) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
517 text = text.replace(/[ \t\r\n]+/g, ' ');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
518 } else {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
519 text = text.replace(/\r\n?/g, '\n'); // Normalize newlines.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
520 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
521 // TODO: handle tabs here?
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
522 chunks[k] = text;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
523 spans[k << 1] = length;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
524 length += text.length;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
525 spans[(k++ << 1) | 1] = node;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
526 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
527 break;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
528 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
529 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
530
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
531 walk(node);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
532
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
533 return {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
534 source: chunks.join('').replace(/\n$/, ''),
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
535 spans: spans
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
536 };
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
537 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
538
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
539
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
540 /**
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
541 * Apply the given language handler to sourceCode and add the resulting
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
542 * decorations to out.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
543 * @param {number} basePos the index of sourceCode within the chunk of source
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
544 * whose decorations are already present on out.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
545 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
546 function appendDecorations(basePos, sourceCode, langHandler, out) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
547 if (!sourceCode) { return; }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
548 var job = {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
549 source: sourceCode,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
550 basePos: basePos
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
551 };
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
552 langHandler(job);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
553 out.push.apply(out, job.decorations);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
554 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
555
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
556 /** Given triples of [style, pattern, context] returns a lexing function,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
557 * The lexing function interprets the patterns to find token boundaries and
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
558 * returns a decoration list of the form
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
559 * [index_0, style_0, index_1, style_1, ..., index_n, style_n]
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
560 * where index_n is an index into the sourceCode, and style_n is a style
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
561 * constant like PR_PLAIN. index_n-1 <= index_n, and style_n-1 applies to
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
562 * all characters in sourceCode[index_n-1:index_n].
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
563 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
564 * The stylePatterns is a list whose elements have the form
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
565 * [style : string, pattern : RegExp, DEPRECATED, shortcut : string].
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
566 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
567 * Style is a style constant like PR_PLAIN, or can be a string of the
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
568 * form 'lang-FOO', where FOO is a language extension describing the
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
569 * language of the portion of the token in $1 after pattern executes.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
570 * E.g., if style is 'lang-lisp', and group 1 contains the text
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
571 * '(hello (world))', then that portion of the token will be passed to the
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
572 * registered lisp handler for formatting.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
573 * The text before and after group 1 will be restyled using this decorator
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
574 * so decorators should take care that this doesn't result in infinite
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
575 * recursion. For example, the HTML lexer rule for SCRIPT elements looks
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
576 * something like ['lang-js', /<[s]cript>(.+?)<\/script>/]. This may match
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
577 * '<script>foo()<\/script>', which would cause the current decorator to
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
578 * be called with '<script>' which would not match the same rule since
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
579 * group 1 must not be empty, so it would be instead styled as PR_TAG by
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
580 * the generic tag rule. The handler registered for the 'js' extension would
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
581 * then be called with 'foo()', and finally, the current decorator would
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
582 * be called with '<\/script>' which would not match the original rule and
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
583 * so the generic tag rule would identify it as a tag.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
584 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
585 * Pattern must only match prefixes, and if it matches a prefix, then that
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
586 * match is considered a token with the same style.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
587 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
588 * Context is applied to the last non-whitespace, non-comment token
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
589 * recognized.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
590 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
591 * Shortcut is an optional string of characters, any of which, if the first
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
592 * character, gurantee that this pattern and only this pattern matches.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
593 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
594 * @param {Array} shortcutStylePatterns patterns that always start with
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
595 * a known character. Must have a shortcut string.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
596 * @param {Array} fallthroughStylePatterns patterns that will be tried in
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
597 * order if the shortcut ones fail. May have shortcuts.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
598 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
599 * @return {function (Object)} a
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
600 * function that takes source code and returns a list of decorations.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
601 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
602 function createSimpleLexer(shortcutStylePatterns, fallthroughStylePatterns) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
603 var shortcuts = {};
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
604 var tokenizer;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
605 (function () {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
606 var allPatterns = shortcutStylePatterns.concat(fallthroughStylePatterns);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
607 var allRegexs = [];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
608 var regexKeys = {};
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
609 for (var i = 0, n = allPatterns.length; i < n; ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
610 var patternParts = allPatterns[i];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
611 var shortcutChars = patternParts[3];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
612 if (shortcutChars) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
613 for (var c = shortcutChars.length; --c >= 0;) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
614 shortcuts[shortcutChars.charAt(c)] = patternParts;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
615 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
616 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
617 var regex = patternParts[1];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
618 var k = '' + regex;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
619 if (!regexKeys.hasOwnProperty(k)) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
620 allRegexs.push(regex);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
621 regexKeys[k] = null;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
622 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
623 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
624 allRegexs.push(/[\0-\uffff]/);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
625 tokenizer = combinePrefixPatterns(allRegexs);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
626 })();
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
627
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
628 var nPatterns = fallthroughStylePatterns.length;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
629 var notWs = /\S/;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
630
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
631 /**
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
632 * Lexes job.source and produces an output array job.decorations of style
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
633 * classes preceded by the position at which they start in job.source in
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
634 * order.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
635 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
636 * @param {Object} job an object like {@code
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
637 * source: {string} sourceText plain text,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
638 * basePos: {int} position of job.source in the larger chunk of
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
639 * sourceCode.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
640 * }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
641 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
642 var decorate = function (job) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
643 var sourceCode = job.source, basePos = job.basePos;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
644 /** Even entries are positions in source in ascending order. Odd enties
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
645 * are style markers (e.g., PR_COMMENT) that run from that position until
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
646 * the end.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
647 * @type {Array.<number|string>}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
648 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
649 var decorations = [basePos, PR_PLAIN];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
650 var pos = 0; // index into sourceCode
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
651 var tokens = sourceCode.match(tokenizer) || [];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
652 var styleCache = {};
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
653
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
654 for (var ti = 0, nTokens = tokens.length; ti < nTokens; ++ti) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
655 var token = tokens[ti];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
656 var style = styleCache[token];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
657 var match = void 0;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
658
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
659 var isEmbedded;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
660 if (typeof style === 'string') {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
661 isEmbedded = false;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
662 } else {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
663 var patternParts = shortcuts[token.charAt(0)];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
664 if (patternParts) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
665 match = token.match(patternParts[1]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
666 style = patternParts[0];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
667 } else {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
668 for (var i = 0; i < nPatterns; ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
669 patternParts = fallthroughStylePatterns[i];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
670 match = token.match(patternParts[1]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
671 if (match) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
672 style = patternParts[0];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
673 break;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
674 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
675 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
676
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
677 if (!match) { // make sure that we make progress
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
678 style = PR_PLAIN;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
679 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
680 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
681
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
682 isEmbedded = style.length >= 5 && 'lang-' === style.substring(0, 5);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
683 if (isEmbedded && !(match && typeof match[1] === 'string')) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
684 isEmbedded = false;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
685 style = PR_SOURCE;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
686 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
687
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
688 if (!isEmbedded) { styleCache[token] = style; }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
689 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
690
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
691 var tokenStart = pos;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
692 pos += token.length;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
693
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
694 if (!isEmbedded) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
695 decorations.push(basePos + tokenStart, style);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
696 } else { // Treat group 1 as an embedded block of source code.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
697 var embeddedSource = match[1];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
698 var embeddedSourceStart = token.indexOf(embeddedSource);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
699 var embeddedSourceEnd = embeddedSourceStart + embeddedSource.length;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
700 if (match[2]) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
701 // If embeddedSource can be blank, then it would match at the
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
702 // beginning which would cause us to infinitely recurse on the
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
703 // entire token, so we catch the right context in match[2].
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
704 embeddedSourceEnd = token.length - match[2].length;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
705 embeddedSourceStart = embeddedSourceEnd - embeddedSource.length;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
706 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
707 var lang = style.substring(5);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
708 // Decorate the left of the embedded source
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
709 appendDecorations(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
710 basePos + tokenStart,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
711 token.substring(0, embeddedSourceStart),
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
712 decorate, decorations);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
713 // Decorate the embedded source
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
714 appendDecorations(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
715 basePos + tokenStart + embeddedSourceStart,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
716 embeddedSource,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
717 langHandlerForExtension(lang, embeddedSource),
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
718 decorations);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
719 // Decorate the right of the embedded section
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
720 appendDecorations(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
721 basePos + tokenStart + embeddedSourceEnd,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
722 token.substring(embeddedSourceEnd),
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
723 decorate, decorations);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
724 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
725 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
726 job.decorations = decorations;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
727 };
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
728 return decorate;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
729 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
730
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
731 /** returns a function that produces a list of decorations from source text.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
732 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
733 * This code treats ", ', and ` as string delimiters, and \ as a string
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
734 * escape. It does not recognize perl's qq() style strings.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
735 * It has no special handling for double delimiter escapes as in basic, or
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
736 * the tripled delimiters used in python, but should work on those regardless
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
737 * although in those cases a single string literal may be broken up into
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
738 * multiple adjacent string literals.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
739 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
740 * It recognizes C, C++, and shell style comments.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
741 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
742 * @param {Object} options a set of optional parameters.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
743 * @return {function (Object)} a function that examines the source code
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
744 * in the input job and builds the decoration list.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
745 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
746 function sourceDecorator(options) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
747 var shortcutStylePatterns = [], fallthroughStylePatterns = [];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
748 if (options['tripleQuotedStrings']) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
749 // '''multi-line-string''', 'single-line-string', and double-quoted
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
750 shortcutStylePatterns.push(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
751 [PR_STRING, /^(?:\'\'\'(?:[^\'\\]|\\[\s\S]|\'{1,2}(?=[^\']))*(?:\'\'\'|$)|\"\"\"(?:[^\"\\]|\\[\s\S]|\"{1,2}(?=[^\"]))*(?:\"\"\"|$)|\'(?:[^\\\']|\\[\s\S])*(?:\'|$)|\"(?:[^\\\"]|\\[\s\S])*(?:\"|$))/,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
752 null, '\'"']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
753 } else if (options['multiLineStrings']) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
754 // 'multi-line-string', "multi-line-string"
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
755 shortcutStylePatterns.push(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
756 [PR_STRING, /^(?:\'(?:[^\\\']|\\[\s\S])*(?:\'|$)|\"(?:[^\\\"]|\\[\s\S])*(?:\"|$)|\`(?:[^\\\`]|\\[\s\S])*(?:\`|$))/,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
757 null, '\'"`']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
758 } else {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
759 // 'single-line-string', "single-line-string"
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
760 shortcutStylePatterns.push(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
761 [PR_STRING,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
762 /^(?:\'(?:[^\\\'\r\n]|\\.)*(?:\'|$)|\"(?:[^\\\"\r\n]|\\.)*(?:\"|$))/,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
763 null, '"\'']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
764 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
765 if (options['verbatimStrings']) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
766 // verbatim-string-literal production from the C# grammar. See issue 93.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
767 fallthroughStylePatterns.push(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
768 [PR_STRING, /^@\"(?:[^\"]|\"\")*(?:\"|$)/, null]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
769 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
770 var hc = options['hashComments'];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
771 if (hc) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
772 if (options['cStyleComments']) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
773 if (hc > 1) { // multiline hash comments
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
774 shortcutStylePatterns.push(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
775 [PR_COMMENT, /^#(?:##(?:[^#]|#(?!##))*(?:###|$)|.*)/, null, '#']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
776 } else {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
777 // Stop C preprocessor declarations at an unclosed open comment
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
778 shortcutStylePatterns.push(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
779 [PR_COMMENT, /^#(?:(?:define|elif|else|endif|error|ifdef|include|ifndef|line|pragma|undef|warning)\b|[^\r\n]*)/,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
780 null, '#']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
781 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
782 fallthroughStylePatterns.push(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
783 [PR_STRING,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
784 /^<(?:(?:(?:\.\.\/)*|\/?)(?:[\w-]+(?:\/[\w-]+)+)?[\w-]+\.h|[a-z]\w*)>/,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
785 null]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
786 } else {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
787 shortcutStylePatterns.push([PR_COMMENT, /^#[^\r\n]*/, null, '#']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
788 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
789 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
790 if (options['cStyleComments']) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
791 fallthroughStylePatterns.push([PR_COMMENT, /^\/\/[^\r\n]*/, null]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
792 fallthroughStylePatterns.push(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
793 [PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
794 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
795 if (options['regexLiterals']) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
796 var REGEX_LITERAL = (
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
797 // A regular expression literal starts with a slash that is
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
798 // not followed by * or / so that it is not confused with
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
799 // comments.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
800 '/(?=[^/*])'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
801 // and then contains any number of raw characters,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
802 + '(?:[^/\\x5B\\x5C]'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
803 // escape sequences (\x5C),
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
804 + '|\\x5C[\\s\\S]'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
805 // or non-nesting character sets (\x5B\x5D);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
806 + '|\\x5B(?:[^\\x5C\\x5D]|\\x5C[\\s\\S])*(?:\\x5D|$))+'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
807 // finally closed by a /.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
808 + '/');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
809 fallthroughStylePatterns.push(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
810 ['lang-regex',
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
811 new RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
812 ]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
813 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
814
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
815 var keywords = options['keywords'].replace(/^\s+|\s+$/g, '');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
816 if (keywords.length) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
817 fallthroughStylePatterns.push(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
818 [PR_KEYWORD,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
819 new RegExp('^(?:' + keywords.replace(/\s+/g, '|') + ')\\b'), null]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
820 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
821
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
822 shortcutStylePatterns.push([PR_PLAIN, /^\s+/, null, ' \r\n\t\xA0']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
823 fallthroughStylePatterns.push(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
824 // TODO(mikesamuel): recognize non-latin letters and numerals in idents
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
825 [PR_LITERAL, /^@[a-z_$][a-z_$@0-9]*/i, null],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
826 [PR_TYPE, /^@?[A-Z]+[a-z][A-Za-z_$@0-9]*/, null],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
827 [PR_PLAIN, /^[a-z_$][a-z_$@0-9]*/i, null],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
828 [PR_LITERAL,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
829 new RegExp(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
830 '^(?:'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
831 // A hex number
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
832 + '0x[a-f0-9]+'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
833 // or an octal or decimal number,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
834 + '|(?:\\d(?:_\\d+)*\\d*(?:\\.\\d*)?|\\.\\d\\+)'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
835 // possibly in scientific notation
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
836 + '(?:e[+\\-]?\\d+)?'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
837 + ')'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
838 // with an optional modifier like UL for unsigned long
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
839 + '[a-z]*', 'i'),
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
840 null, '0123456789'],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
841 // Don't treat escaped quotes in bash as starting strings. See issue 144.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
842 [PR_PLAIN, /^\\[\s\S]?/, null],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
843 [PR_PUNCTUATION, /^.[^\s\w\.$@\'\"\`\/\#\\]*/, null]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
844
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
845 return createSimpleLexer(shortcutStylePatterns, fallthroughStylePatterns);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
846 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
847
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
848 var decorateSource = sourceDecorator({
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
849 'keywords': ALL_KEYWORDS,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
850 'hashComments': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
851 'cStyleComments': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
852 'multiLineStrings': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
853 'regexLiterals': true
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
854 });
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
855
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
856 /**
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
857 * Given a DOM subtree, wraps it in a list, and puts each line into its own
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
858 * list item.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
859 *
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
860 * @param {Node} node modified in place. Its content is pulled into an
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
861 * HTMLOListElement, and each line is moved into a separate list item.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
862 * This requires cloning elements, so the input might not have unique
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
863 * IDs after numbering.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
864 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
865 function numberLines(node, opt_startLineNum) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
866 var nocode = /(?:^|\s)nocode(?:\s|$)/;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
867 var lineBreak = /\r\n?|\n/;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
868
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
869 var document = node.ownerDocument;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
870
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
871 var whitespace;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
872 if (node.currentStyle) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
873 whitespace = node.currentStyle.whiteSpace;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
874 } else if (window.getComputedStyle) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
875 whitespace = document.defaultView.getComputedStyle(node, null)
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
876 .getPropertyValue('white-space');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
877 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
878 // If it's preformatted, then we need to split lines on line breaks
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
879 // in addition to <BR>s.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
880 var isPreformatted = whitespace && 'pre' === whitespace.substring(0, 3);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
881
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
882 var li = document.createElement('LI');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
883 while (node.firstChild) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
884 li.appendChild(node.firstChild);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
885 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
886 // An array of lines. We split below, so this is initialized to one
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
887 // un-split line.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
888 var listItems = [li];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
889
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
890 function walk(node) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
891 switch (node.nodeType) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
892 case 1: // Element
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
893 if (nocode.test(node.className)) { break; }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
894 if ('BR' === node.nodeName) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
895 breakAfter(node);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
896 // Discard the <BR> since it is now flush against a </LI>.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
897 if (node.parentNode) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
898 node.parentNode.removeChild(node);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
899 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
900 } else {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
901 for (var child = node.firstChild; child; child = child.nextSibling) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
902 walk(child);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
903 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
904 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
905 break;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
906 case 3: case 4: // Text
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
907 if (isPreformatted) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
908 var text = node.nodeValue;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
909 var match = text.match(lineBreak);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
910 if (match) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
911 var firstLine = text.substring(0, match.index);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
912 node.nodeValue = firstLine;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
913 var tail = text.substring(match.index + match[0].length);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
914 if (tail) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
915 var parent = node.parentNode;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
916 parent.insertBefore(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
917 document.createTextNode(tail), node.nextSibling);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
918 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
919 breakAfter(node);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
920 if (!firstLine) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
921 // Don't leave blank text nodes in the DOM.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
922 node.parentNode.removeChild(node);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
923 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
924 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
925 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
926 break;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
927 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
928 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
929
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
930 // Split a line after the given node.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
931 function breakAfter(lineEndNode) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
932 // If there's nothing to the right, then we can skip ending the line
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
933 // here, and move root-wards since splitting just before an end-tag
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
934 // would require us to create a bunch of empty copies.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
935 while (!lineEndNode.nextSibling) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
936 lineEndNode = lineEndNode.parentNode;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
937 if (!lineEndNode) { return; }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
938 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
939
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
940 function breakLeftOf(limit, copy) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
941 // Clone shallowly if this node needs to be on both sides of the break.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
942 var rightSide = copy ? limit.cloneNode(false) : limit;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
943 var parent = limit.parentNode;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
944 if (parent) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
945 // We clone the parent chain.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
946 // This helps us resurrect important styling elements that cross lines.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
947 // E.g. in <i>Foo<br>Bar</i>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
948 // should be rewritten to <li><i>Foo</i></li><li><i>Bar</i></li>.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
949 var parentClone = breakLeftOf(parent, 1);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
950 // Move the clone and everything to the right of the original
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
951 // onto the cloned parent.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
952 var next = limit.nextSibling;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
953 parentClone.appendChild(rightSide);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
954 for (var sibling = next; sibling; sibling = next) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
955 next = sibling.nextSibling;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
956 parentClone.appendChild(sibling);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
957 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
958 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
959 return rightSide;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
960 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
961
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
962 var copiedListItem = breakLeftOf(lineEndNode.nextSibling, 0);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
963
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
964 // Walk the parent chain until we reach an unattached LI.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
965 for (var parent;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
966 // Check nodeType since IE invents document fragments.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
967 (parent = copiedListItem.parentNode) && parent.nodeType === 1;) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
968 copiedListItem = parent;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
969 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
970 // Put it on the list of lines for later processing.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
971 listItems.push(copiedListItem);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
972 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
973
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
974 // Split lines while there are lines left to split.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
975 for (var i = 0; // Number of lines that have been split so far.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
976 i < listItems.length; // length updated by breakAfter calls.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
977 ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
978 walk(listItems[i]);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
979 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
980
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
981 // Make sure numeric indices show correctly.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
982 if (opt_startLineNum === (opt_startLineNum|0)) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
983 listItems[0].setAttribute('value', opt_startLineNum);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
984 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
985
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
986 var ol = document.createElement('OL');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
987 ol.className = 'linenums';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
988 var offset = Math.max(0, ((opt_startLineNum - 1 /* zero index */)) | 0) || 0;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
989 for (var i = 0, n = listItems.length; i < n; ++i) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
990 li = listItems[i];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
991 // Stick a class on the LIs so that stylesheets can
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
992 // color odd/even rows, or any other row pattern that
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
993 // is co-prime with 10.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
994 li.className = 'L' + ((i + offset) % 10);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
995 if (!li.firstChild) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
996 li.appendChild(document.createTextNode('\xA0'));
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
997 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
998 ol.appendChild(li);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
999 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1000
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1001 node.appendChild(ol);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1002 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1003
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1004 /**
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1005 * Breaks {@code job.source} around style boundaries in {@code job.decorations}
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1006 * and modifies {@code job.sourceNode} in place.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1007 * @param {Object} job like <pre>{
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1008 * source: {string} source as plain text,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1009 * spans: {Array.<number|Node>} alternating span start indices into source
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1010 * and the text node or element (e.g. {@code <BR>}) corresponding to that
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1011 * span.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1012 * decorations: {Array.<number|string} an array of style classes preceded
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1013 * by the position at which they start in job.source in order
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1014 * }</pre>
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1015 * @private
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1016 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1017 function recombineTagsAndDecorations(job) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1018 var isIE = /\bMSIE\b/.test(navigator.userAgent);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1019 var newlineRe = /\n/g;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1020
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1021 var source = job.source;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1022 var sourceLength = source.length;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1023 // Index into source after the last code-unit recombined.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1024 var sourceIndex = 0;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1025
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1026 var spans = job.spans;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1027 var nSpans = spans.length;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1028 // Index into spans after the last span which ends at or before sourceIndex.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1029 var spanIndex = 0;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1030
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1031 var decorations = job.decorations;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1032 var nDecorations = decorations.length;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1033 // Index into decorations after the last decoration which ends at or before sourceIndex.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1034 var decorationIndex = 0;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1035
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1036 // Simplify decorations.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1037 var decPos = 0;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1038 for (var i = 0; i < nDecorations;) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1039 // Skip over any zero-length decorations.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1040 var startPos = decorations[i];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1041 var start = i;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1042 while (start + 2 < nDecorations && decorations[start + 2] === startPos) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1043 start += 2;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1044 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1045 // Conflate all adjacent decorations that use the same style.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1046 var startDec = decorations[start + 1];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1047 var end = start + 2;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1048 while (end + 2 <= nDecorations
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1049 && (decorations[end + 1] === startDec
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1050 || decorations[end] === decorations[end + 2])) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1051 end += 2;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1052 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1053 decorations[decPos++] = startPos;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1054 decorations[decPos++] = startDec;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1055 i = end;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1056 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1057
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1058 // Strip any zero-length decoration at the end.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1059 if (decPos && decorations[decPos - 2] === sourceLength) { decPos -= 2; }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1060 nDecorations = decorations.length = decPos;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1061
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1062 var decoration = null;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1063 while (spanIndex < nSpans) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1064 var spanStart = spans[spanIndex];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1065 var spanEnd = spans[spanIndex + 2] || sourceLength;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1066
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1067 var decStart = decorations[decorationIndex];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1068 var decEnd = decorations[decorationIndex + 2] || sourceLength;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1069
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1070 var end = Math.min(spanEnd, decEnd);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1071
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1072 var textNode = spans[spanIndex + 1];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1073 if (textNode.nodeType !== 1) { // Don't muck with <BR>s or <LI>s
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1074 var styledText = source.substring(sourceIndex, end);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1075 // This may seem bizarre, and it is. Emitting LF on IE causes the
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1076 // code to display with spaces instead of line breaks.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1077 // Emitting Windows standard issue linebreaks (CRLF) causes a blank
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1078 // space to appear at the beginning of every line but the first.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1079 // Emitting an old Mac OS 9 line separator makes everything spiffy.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1080 if (isIE) { styledText = styledText.replace(newlineRe, '\r'); }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1081 textNode.nodeValue = styledText;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1082 var document = textNode.ownerDocument;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1083 var span = document.createElement('SPAN');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1084 span.className = decorations[decorationIndex + 1];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1085 var parentNode = textNode.parentNode;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1086 parentNode.replaceChild(span, textNode);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1087 span.appendChild(textNode);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1088 if (sourceIndex < spanEnd) { // Split off a text node.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1089 spans[spanIndex + 1] = textNode
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1090 // TODO: Possibly optimize by using '' if there's no flicker.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1091 = document.createTextNode(source.substring(end, spanEnd));
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1092 parentNode.insertBefore(textNode, span.nextSibling);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1093 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1094 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1095
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1096 sourceIndex = end;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1097
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1098 if (sourceIndex >= spanEnd) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1099 spanIndex += 2;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1100 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1101 if (sourceIndex >= decEnd) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1102 decorationIndex += 2;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1103 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1104 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1105 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1106
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1107
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1108 /** Maps language-specific file extensions to handlers. */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1109 var langHandlerRegistry = {};
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1110 /** Register a language handler for the given file extensions.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1111 * @param {function (Object)} handler a function from source code to a list
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1112 * of decorations. Takes a single argument job which describes the
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1113 * state of the computation. The single parameter has the form
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1114 * {@code {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1115 * source: {string} as plain text.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1116 * decorations: {Array.<number|string>} an array of style classes
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1117 * preceded by the position at which they start in
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1118 * job.source in order.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1119 * The language handler should assigned this field.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1120 * basePos: {int} the position of source in the larger source chunk.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1121 * All positions in the output decorations array are relative
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1122 * to the larger source chunk.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1123 * } }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1124 * @param {Array.<string>} fileExtensions
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1125 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1126 function registerLangHandler(handler, fileExtensions) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1127 for (var i = fileExtensions.length; --i >= 0;) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1128 var ext = fileExtensions[i];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1129 if (!langHandlerRegistry.hasOwnProperty(ext)) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1130 langHandlerRegistry[ext] = handler;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1131 } else if ('console' in window) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1132 console['warn']('cannot override language handler %s', ext);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1133 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1134 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1135 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1136 function langHandlerForExtension(extension, source) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1137 if (!(extension && langHandlerRegistry.hasOwnProperty(extension))) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1138 // Treat it as markup if the first non whitespace character is a < and
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1139 // the last non-whitespace character is a >.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1140 extension = /^\s*</.test(source)
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1141 ? 'default-markup'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1142 : 'default-code';
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1143 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1144 return langHandlerRegistry[extension];
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1145 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1146 registerLangHandler(decorateSource, ['default-code']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1147 registerLangHandler(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1148 createSimpleLexer(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1149 [],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1150 [
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1151 [PR_PLAIN, /^[^<?]+/],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1152 [PR_DECLARATION, /^<!\w[^>]*(?:>|$)/],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1153 [PR_COMMENT, /^<\!--[\s\S]*?(?:-\->|$)/],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1154 // Unescaped content in an unknown language
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1155 ['lang-', /^<\?([\s\S]+?)(?:\?>|$)/],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1156 ['lang-', /^<%([\s\S]+?)(?:%>|$)/],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1157 [PR_PUNCTUATION, /^(?:<[%?]|[%?]>)/],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1158 ['lang-', /^<xmp\b[^>]*>([\s\S]+?)<\/xmp\b[^>]*>/i],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1159 // Unescaped content in javascript. (Or possibly vbscript).
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1160 ['lang-js', /^<script\b[^>]*>([\s\S]*?)(<\/script\b[^>]*>)/i],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1161 // Contains unescaped stylesheet content
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1162 ['lang-css', /^<style\b[^>]*>([\s\S]*?)(<\/style\b[^>]*>)/i],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1163 ['lang-in.tag', /^(<\/?[a-z][^<>]*>)/i]
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1164 ]),
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1165 ['default-markup', 'htm', 'html', 'mxml', 'xhtml', 'xml', 'xsl']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1166 registerLangHandler(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1167 createSimpleLexer(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1168 [
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1169 [PR_PLAIN, /^[\s]+/, null, ' \t\r\n'],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1170 [PR_ATTRIB_VALUE, /^(?:\"[^\"]*\"?|\'[^\']*\'?)/, null, '\"\'']
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1171 ],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1172 [
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1173 [PR_TAG, /^^<\/?[a-z](?:[\w.:-]*\w)?|\/?>$/i],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1174 [PR_ATTRIB_NAME, /^(?!style[\s=]|on)[a-z](?:[\w:-]*\w)?/i],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1175 ['lang-uq.val', /^=\s*([^>\'\"\s]*(?:[^>\'\"\s\/]|\/(?=\s)))/],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1176 [PR_PUNCTUATION, /^[=<>\/]+/],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1177 ['lang-js', /^on\w+\s*=\s*\"([^\"]+)\"/i],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1178 ['lang-js', /^on\w+\s*=\s*\'([^\']+)\'/i],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1179 ['lang-js', /^on\w+\s*=\s*([^\"\'>\s]+)/i],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1180 ['lang-css', /^style\s*=\s*\"([^\"]+)\"/i],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1181 ['lang-css', /^style\s*=\s*\'([^\']+)\'/i],
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1182 ['lang-css', /^style\s*=\s*([^\"\'>\s]+)/i]
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1183 ]),
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1184 ['in.tag']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1185 registerLangHandler(
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1186 createSimpleLexer([], [[PR_ATTRIB_VALUE, /^[\s\S]+/]]), ['uq.val']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1187 registerLangHandler(sourceDecorator({
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1188 'keywords': CPP_KEYWORDS,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1189 'hashComments': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1190 'cStyleComments': true
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1191 }), ['c', 'cc', 'cpp', 'cxx', 'cyc', 'm']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1192 registerLangHandler(sourceDecorator({
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1193 'keywords': 'null true false'
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1194 }), ['json']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1195 registerLangHandler(sourceDecorator({
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1196 'keywords': CSHARP_KEYWORDS,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1197 'hashComments': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1198 'cStyleComments': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1199 'verbatimStrings': true
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1200 }), ['cs']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1201 registerLangHandler(sourceDecorator({
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1202 'keywords': JAVA_KEYWORDS,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1203 'cStyleComments': true
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1204 }), ['java']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1205 registerLangHandler(sourceDecorator({
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1206 'keywords': SH_KEYWORDS,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1207 'hashComments': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1208 'multiLineStrings': true
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1209 }), ['bsh', 'csh', 'sh']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1210 registerLangHandler(sourceDecorator({
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1211 'keywords': PYTHON_KEYWORDS,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1212 'hashComments': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1213 'multiLineStrings': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1214 'tripleQuotedStrings': true
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1215 }), ['cv', 'py']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1216 registerLangHandler(sourceDecorator({
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1217 'keywords': PERL_KEYWORDS,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1218 'hashComments': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1219 'multiLineStrings': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1220 'regexLiterals': true
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1221 }), ['perl', 'pl', 'pm']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1222 registerLangHandler(sourceDecorator({
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1223 'keywords': RUBY_KEYWORDS,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1224 'hashComments': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1225 'multiLineStrings': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1226 'regexLiterals': true
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1227 }), ['rb']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1228 registerLangHandler(sourceDecorator({
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1229 'keywords': JSCRIPT_KEYWORDS,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1230 'cStyleComments': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1231 'regexLiterals': true
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1232 }), ['js']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1233 registerLangHandler(sourceDecorator({
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1234 'keywords': COFFEE_KEYWORDS,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1235 'hashComments': 3, // ### style block comments
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1236 'cStyleComments': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1237 'multilineStrings': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1238 'tripleQuotedStrings': true,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1239 'regexLiterals': true
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1240 }), ['coffee']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1241 registerLangHandler(createSimpleLexer([], [[PR_STRING, /^[\s\S]+/]]), ['regex']);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1242
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1243 function applyDecorator(job) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1244 var opt_langExtension = job.langExtension;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1245
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1246 try {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1247 // Extract tags, and convert the source code to plain text.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1248 var sourceAndSpans = extractSourceSpans(job.sourceNode);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1249 /** Plain text. @type {string} */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1250 var source = sourceAndSpans.source;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1251 job.source = source;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1252 job.spans = sourceAndSpans.spans;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1253 job.basePos = 0;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1254
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1255 // Apply the appropriate language handler
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1256 langHandlerForExtension(opt_langExtension, source)(job);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1257
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1258 // Integrate the decorations and tags back into the source code,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1259 // modifying the sourceNode in place.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1260 recombineTagsAndDecorations(job);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1261 } catch (e) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1262 if ('console' in window) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1263 console['log'](e && e['stack'] ? e['stack'] : e);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1264 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1265 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1266 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1267
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1268 /**
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1269 * @param sourceCodeHtml {string} The HTML to pretty print.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1270 * @param opt_langExtension {string} The language name to use.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1271 * Typically, a filename extension like 'cpp' or 'java'.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1272 * @param opt_numberLines {number|boolean} True to number lines,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1273 * or the 1-indexed number of the first line in sourceCodeHtml.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1274 */
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1275 function prettyPrintOne(sourceCodeHtml, opt_langExtension, opt_numberLines) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1276 var container = document.createElement('PRE');
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1277 // This could cause images to load and onload listeners to fire.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1278 // E.g. <img onerror="alert(1337)" src="nosuchimage.png">.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1279 // We assume that the inner HTML is from a trusted source.
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1280 container.innerHTML = sourceCodeHtml;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1281 if (opt_numberLines) {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1282 numberLines(container, opt_numberLines);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1283 }
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1284
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1285 var job = {
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1286 langExtension: opt_langExtension,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1287 numberLines: opt_numberLines,
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1288 sourceNode: container
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1289 };
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1290 applyDecorator(job);
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1291 return container.innerHTML;
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: