diff options
-rw-r--r-- | microlight.js | 308 |
1 files changed, 144 insertions, 164 deletions
diff --git a/microlight.js b/microlight.js index a0ae720..597ef13 100644 --- a/microlight.js +++ b/microlight.js | |||
@@ -9,15 +9,7 @@ | |||
9 | */ | 9 | */ |
10 | 10 | ||
11 | 11 | ||
12 | (function (root, factory) { | 12 | ( function () { |
13 | if (typeof define === 'function' && define.amd) { | ||
14 | define(['exports'], factory); | ||
15 | } else if (typeof exports !== 'undefined') { | ||
16 | factory(exports); | ||
17 | } else { | ||
18 | factory((root.microlight = {})); | ||
19 | } | ||
20 | }(this, function (exports) { | ||
21 | // for better compression | 13 | // for better compression |
22 | var _window = window, | 14 | var _window = window, |
23 | _document = document, | 15 | _document = document, |
@@ -28,166 +20,154 @@ | |||
28 | microlighted, | 20 | microlighted, |
29 | el; // current microlighted element to run through | 21 | el; // current microlighted element to run through |
30 | 22 | ||
31 | 23 | // nodes to highlight | |
32 | 24 | microlighted = _document.querySelectorAll('code'); | |
33 | var reset = function() { | 25 | |
34 | // nodes to highlight | 26 | for (i = 0; el = microlighted[i++];) { |
35 | microlighted = _document.querySelectorAll('code'); | 27 | var text = el.textContent, |
36 | 28 | pos = 0, // current position | |
37 | for (i = 0; el = microlighted[i++];) { | 29 | next1 = text[0], // next character |
38 | var text = el.textContent, | 30 | chr = 1, // current character |
39 | pos = 0, // current position | 31 | prev1, // previous character |
40 | next1 = text[0], // next character | 32 | prev2, // the one before the previous |
41 | chr = 1, // current character | 33 | token = // current token content |
42 | prev1, // previous character | 34 | el.innerHTML = '', // (and cleaning the node) |
43 | prev2, // the one before the previous | 35 | |
44 | token = // current token content | 36 | // current token type: |
45 | el.innerHTML = '', // (and cleaning the node) | 37 | // 0: anything else (whitespaces / newlines) |
46 | 38 | // 1: operator or brace | |
47 | // current token type: | 39 | // 2: closing braces (after which '/' is division not regex) |
48 | // 0: anything else (whitespaces / newlines) | 40 | // 3: (key)word |
49 | // 1: operator or brace | 41 | // 4: regex |
50 | // 2: closing braces (after which '/' is division not regex) | 42 | // 5: string starting with " |
51 | // 3: (key)word | 43 | // 6: string starting with ' |
52 | // 4: regex | 44 | // 7: xml comment <!-- --> |
53 | // 5: string starting with " | 45 | // 8: multiline comment /* */ |
54 | // 6: string starting with ' | 46 | // 9: single-line comment starting with two slashes // |
55 | // 7: xml comment <!-- --> | 47 | // 10: single-line comment starting with hash # |
56 | // 8: multiline comment /* */ | 48 | tokenType = 0, |
57 | // 9: single-line comment starting with two slashes // | 49 | |
58 | // 10: single-line comment starting with hash # | 50 | // kept to determine between regex and division |
59 | tokenType = 0, | 51 | lastTokenType, |
60 | 52 | // flag determining if token is multi-character | |
61 | // kept to determine between regex and division | 53 | multichar, |
62 | lastTokenType, | 54 | // first token of line |
63 | // flag determining if token is multi-character | 55 | firstToken, |
64 | multichar, | 56 | node; |
65 | // first token of line | 57 | |
66 | firstToken, | 58 | // running through characters and highlighting |
67 | node; | 59 | while (prev2 = prev1, |
68 | 60 | // escaping if needed (with except for comments) | |
69 | // running through characters and highlighting | 61 | // pervious character will not be therefore |
70 | while (prev2 = prev1, | 62 | // recognized as a token finalize condition |
71 | // escaping if needed (with except for comments) | 63 | prev1 = tokenType < 7 && prev1 == '\\' ? 1 : chr |
72 | // pervious character will not be therefore | 64 | ) { |
73 | // recognized as a token finalize condition | 65 | chr = next1; |
74 | prev1 = tokenType < 7 && prev1 == '\\' ? 1 : chr | 66 | next1=text[++pos]; |
67 | multichar = token.length > 1; | ||
68 | |||
69 | // checking if current token should be finalized | ||
70 | if (!chr || // end of content | ||
71 | // newline | ||
72 | chr == '\n' || prev1 == '\n' || | ||
73 | [ // finalize conditions for other token types | ||
74 | // 0: whitespaces | ||
75 | /\S/[test](chr), // merged together | ||
76 | // 1: operators | ||
77 | 1, // consist of a single character | ||
78 | // 2: braces | ||
79 | 1, // consist of a single character | ||
80 | // 3: (key)word | ||
81 | !/[$\w]/[test](chr), | ||
82 | // 4: regex | ||
83 | (prev1 == '/' || prev1 == '\n') && multichar, | ||
84 | // 5: string with " | ||
85 | prev1 == '"' && multichar, | ||
86 | // 6: string with ' | ||
87 | prev1 == "'" && multichar, | ||
88 | // 7: xml comment | ||
89 | text[pos-4]+prev2+prev1 == '-->', | ||
90 | // 8: multiline comment | ||
91 | prev2+prev1 == '*/' | ||
92 | ][tokenType] | ||
75 | ) { | 93 | ) { |
76 | chr = next1; | 94 | // appending the token to the result |
77 | next1=text[++pos]; | 95 | if (token) { |
78 | multichar = token.length > 1; | 96 | // remapping token type into style |
79 | 97 | // (some types are highlighted similarly) | |
80 | // checking if current token should be finalized | 98 | el[appendChild]( |
81 | if (!chr || // end of content | 99 | node = _document.createElement('span') |
82 | // newline | 100 | ).setAttribute('class', 'f'+(firstToken|0)+' c'+( |
83 | chr == '\n' || prev1 == '\n' || | 101 | // not formatted |
84 | [ // finalize conditions for other token types | 102 | !tokenType ? 0 : |
85 | // 0: whitespaces | 103 | // diff |
86 | /\S/[test](chr), // merged together | 104 | tokenType > 10 ? tokenType : |
87 | // 1: operators | 105 | // punctuation |
88 | 1, // consist of a single character | 106 | tokenType < 3 ? 2 : |
89 | // 2: braces | 107 | // comments |
90 | 1, // consist of a single character | 108 | tokenType > 6 ? 4 : |
91 | // 3: (key)word | 109 | // regex and strings |
92 | !/[$\w]/[test](chr), | 110 | tokenType > 3 ? 3 : |
93 | // 4: regex | 111 | // otherwise tokenType == 3, (key)word |
94 | (prev1 == '/' || prev1 == '\n') && multichar, | 112 | // (1 if regexp matches, 0 otherwise) |
95 | // 5: string with " | 113 | + /^(a(bstract|lias|nd|rguments|rray|s(m|sert)?|uto)|b(ase|egin|ool(ean)?|reak|yte)|c(ase|atch|har|hecked|lass|lone|ompl|onst|ontinue)|de(bugger|cimal|clare|f(ault|er)?|init|l(egate|ete)?)|do|double|e(cho|ls?if|lse(if)?|nd|nsure|num|vent|x(cept|ec|p(licit|ort)|te(nds|nsion|rn)))|f(allthrough|alse|inal(ly)?|ixed|loat|or(each)?|riend|rom|unc(tion)?)|global|goto|guard|#?i(f|mp(lements|licit|ort)|n(it|clude(_once)?|line|out|stanceof|t(erface|ernal)?)?|s)|l(ambda|et|ock|ong)|m(icrolight|odule|utable)|NaN|n(amespace|ative|ext|ew|il|ot|ull)|o(bject|perator|r|ut|verride)|p(ackage|arams|rivate|rotected|rotocol|ublic)|r(aise|e(adonly|do|f|gister|peat|quire(_once)?|scue|strict|try|turn))|s(byte|ealed|elf|hort|igned|izeof|tatic|tring|truct|ubscript|uper|ynchronized|witch)|t(emplate|hen|his|hrows?|ransient|rue|ry|ype(alias|def|id|name|of))|u(n(checked|def(ined)?|ion|less|signed|til)|se|sing)|v(ar|irtual|oid|olatile)|w(char_t|hen|here|hile|ith)|xor|yield)$/[test](token) |
96 | prev1 == '"' && multichar, | 114 | )); |
97 | // 6: string with ' | 115 | |
98 | prev1 == "'" && multichar, | 116 | node[appendChild](_document.createTextNode(token)); |
99 | // 7: xml comment | 117 | } |
100 | text[pos-4]+prev2+prev1 == '-->', | ||
101 | // 8: multiline comment | ||
102 | prev2+prev1 == '*/' | ||
103 | ][tokenType] | ||
104 | ) { | ||
105 | // appending the token to the result | ||
106 | if (token) { | ||
107 | // remapping token type into style | ||
108 | // (some types are highlighted similarly) | ||
109 | el[appendChild]( | ||
110 | node = _document.createElement('span') | ||
111 | ).setAttribute('class', 'f'+(firstToken|0)+' c'+( | ||
112 | // not formatted | ||
113 | !tokenType ? 0 : | ||
114 | // diff | ||
115 | tokenType > 10 ? tokenType : | ||
116 | // punctuation | ||
117 | tokenType < 3 ? 2 : | ||
118 | // comments | ||
119 | tokenType > 6 ? 4 : | ||
120 | // regex and strings | ||
121 | tokenType > 3 ? 3 : | ||
122 | // otherwise tokenType == 3, (key)word | ||
123 | // (1 if regexp matches, 0 otherwise) | ||
124 | + /^(a(bstract|lias|nd|rguments|rray|s(m|sert)?|uto)|b(ase|egin|ool(ean)?|reak|yte)|c(ase|atch|har|hecked|lass|lone|ompl|onst|ontinue)|de(bugger|cimal|clare|f(ault|er)?|init|l(egate|ete)?)|do|double|e(cho|ls?if|lse(if)?|nd|nsure|num|vent|x(cept|ec|p(licit|ort)|te(nds|nsion|rn)))|f(allthrough|alse|inal(ly)?|ixed|loat|or(each)?|riend|rom|unc(tion)?)|global|goto|guard|#?i(f|mp(lements|licit|ort)|n(it|clude(_once)?|line|out|stanceof|t(erface|ernal)?)?|s)|l(ambda|et|ock|ong)|m(icrolight|odule|utable)|NaN|n(amespace|ative|ext|ew|il|ot|ull)|o(bject|perator|r|ut|verride)|p(ackage|arams|rivate|rotected|rotocol|ublic)|r(aise|e(adonly|do|f|gister|peat|quire(_once)?|scue|strict|try|turn))|s(byte|ealed|elf|hort|igned|izeof|tatic|tring|truct|ubscript|uper|ynchronized|witch)|t(emplate|hen|his|hrows?|ransient|rue|ry|ype(alias|def|id|name|of))|u(n(checked|def(ined)?|ion|less|signed|til)|se|sing)|v(ar|irtual|oid|olatile)|w(char_t|hen|here|hile|ith)|xor|yield)$/[test](token) | ||
125 | )); | ||
126 | |||
127 | node[appendChild](_document.createTextNode(token)); | ||
128 | } | ||
129 | |||
130 | // saving the previous token type | ||
131 | // (skipping whitespaces and comments) | ||
132 | lastTokenType = | ||
133 | (tokenType && tokenType < 7) ? | ||
134 | tokenType : lastTokenType; | ||
135 | |||
136 | // initializing a new token | ||
137 | token = ''; | ||
138 | 118 | ||
139 | // first token of line | 119 | // saving the previous token type |
140 | firstToken = pos < 2 || prev1 == '\n'; | 120 | // (skipping whitespaces and comments) |
121 | lastTokenType = | ||
122 | (tokenType && tokenType < 7) ? | ||
123 | tokenType : lastTokenType; | ||
141 | 124 | ||
142 | // determining the new token type (going up the | 125 | // initializing a new token |
143 | // list until matching a token type start | 126 | token = ''; |
144 | // condition) | ||
145 | tokenType = 15; | ||
146 | while (![ | ||
147 | 1, // 0: whitespace | ||
148 | // 1: operator or braces | ||
149 | /[\/{}[(\-+*=<>:;|\\.,?!&@~]/[test](chr), | ||
150 | /[\])]/[test](chr), // 2: closing brace | ||
151 | /[#$\w]/[test](chr), // 3: (key)word | ||
152 | chr == '/' && // 4: regex | ||
153 | // previous token was an | ||
154 | // opening brace or an | ||
155 | // operator (otherwise | ||
156 | // division, not a regex) | ||
157 | (lastTokenType < 2) && | ||
158 | // workaround for xml | ||
159 | // closing tags | ||
160 | prev1 != '<', | ||
161 | chr == '"', // 5: string with " | ||
162 | chr == "'", // 6: string with ' | ||
163 | // 7: xml comment | ||
164 | chr+next1+text[pos+1]+text[pos+2] == '<!--', | ||
165 | chr+next1 == '/*', // 8: multiline comment | ||
166 | chr+next1 == '//', // 9: single-line comment | ||
167 | // 10: hash-style comment | ||
168 | chr == '#' && next1+text[pos+1]+text[pos+2] != 'inc', | ||
169 | // 11: diff add | ||
170 | firstToken && chr == '+', | ||
171 | // 12: diff remove | ||
172 | firstToken && chr == '-', | ||
173 | // 13: diff compared | ||
174 | firstToken && (chr+next1+text[pos+1] == '+++' || chr+next1+text[pos+1] == '---'), | ||
175 | // 14: diff offset | ||
176 | firstToken && chr+next1 == '@@' | ||
177 | ][--tokenType]); | ||
178 | } | ||
179 | 127 | ||
180 | token += chr; | 128 | // first token of line |
129 | firstToken = pos < 2 || prev1 == '\n'; | ||
130 | |||
131 | // determining the new token type (going up the | ||
132 | // list until matching a token type start | ||
133 | // condition) | ||
134 | tokenType = 15; | ||
135 | while (![ | ||
136 | 1, // 0: whitespace | ||
137 | // 1: operator or braces | ||
138 | /[\/{}[(\-+*=<>:;|\\.,?!&@~]/[test](chr), | ||
139 | /[\])]/[test](chr), // 2: closing brace | ||
140 | /[#$\w]/[test](chr), // 3: (key)word | ||
141 | chr == '/' && // 4: regex | ||
142 | // previous token was an | ||
143 | // opening brace or an | ||
144 | // operator (otherwise | ||
145 | // division, not a regex) | ||
146 | (lastTokenType < 2) && | ||
147 | // workaround for xml | ||
148 | // closing tags | ||
149 | prev1 != '<', | ||
150 | chr == '"', // 5: string with " | ||
151 | chr == "'", // 6: string with ' | ||
152 | // 7: xml comment | ||
153 | chr+next1+text[pos+1]+text[pos+2] == '<!--', | ||
154 | chr+next1 == '/*', // 8: multiline comment | ||
155 | chr+next1 == '//', // 9: single-line comment | ||
156 | // 10: hash-style comment | ||
157 | chr == '#' && next1+text[pos+1]+text[pos+2] != 'inc', | ||
158 | // 11: diff add | ||
159 | firstToken && chr == '+', | ||
160 | // 12: diff remove | ||
161 | firstToken && chr == '-', | ||
162 | // 13: diff compared | ||
163 | firstToken && (chr+next1+text[pos+1] == '+++' || chr+next1+text[pos+1] == '---'), | ||
164 | // 14: diff offset | ||
165 | firstToken && chr+next1 == '@@' | ||
166 | ][--tokenType]); | ||
181 | } | 167 | } |
182 | } | ||
183 | } | ||
184 | 168 | ||
185 | exports.reset = reset; | 169 | token += chr; |
186 | 170 | } | |
187 | if (_document.readyState == 'complete') { | ||
188 | reset(); | ||
189 | } else { | ||
190 | _window.addEventListener('load', function(){reset()}, 0); | ||
191 | } | 171 | } |
192 | })); | 172 | } )(); |
193 | 173 | ||