summaryrefslogtreecommitdiff
path: root/proselight.js
diff options
context:
space:
mode:
authorBlaise2022-10-28 18:35:37 -0500
committerBlaise2022-10-28 18:35:37 -0500
commit476c93f0007ed41d8c791667f6d1bbe644096b91 (patch)
tree43060c4d9f7aa4b78b30921c75ccb5882348acdc /proselight.js
parent7a6214635351abd35ed9c05c73713d22e4e2439e (diff)
Rename to proselight
Diffstat (limited to 'proselight.js')
-rw-r--r--proselight.js210
1 files changed, 210 insertions, 0 deletions
diff --git a/proselight.js b/proselight.js
new file mode 100644
index 0000000..184cbda
--- /dev/null
+++ b/proselight.js
@@ -0,0 +1,210 @@
1/**
2 * @fileoverview proselight, based on microlight - syntax highlightning library
3 * @version 0.0.7
4 *
5 * @license MIT, see http://github.com/asvd/microlight
6 * @copyright 2016 asvd <heliosframework@gmail.com>
7 *
8 * Code structure aims at minimizing the compressed library size
9 */
10
11
12(function (root, factory) {
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
22 var _window = window,
23 _document = document,
24 appendChild = 'appendChild',
25 test = 'test',
26 // style and color templates
27 textShadow = ';text-shadow:',
28 opacity = 'opacity:.',
29 _0px_0px = ' 0px 0px ',
30 _3px_0px_5 = '3px 0px 5',
31 brace = ')',
32
33 i,
34 microlighted,
35 el; // current microlighted element to run through
36
37
38
39 var reset = function(cls) {
40 // nodes to highlight
41 microlighted = _document.getElementsByClassName(cls||'microlight');
42
43 for (i = 0; el = microlighted[i++];) {
44 var text = el.textContent,
45 pos = 0, // current position
46 next1 = text[0], // next character
47 chr = 1, // current character
48 prev1, // previous character
49 prev2, // the one before the previous
50 token = // current token content
51 el.innerHTML = '', // (and cleaning the node)
52
53 // current token type:
54 // 0: anything else (whitespaces / newlines)
55 // 1: operator or brace
56 // 2: closing braces (after which '/' is division not regex)
57 // 3: (key)word
58 // 4: regex
59 // 5: string starting with "
60 // 6: string starting with '
61 // 7: xml comment <!-- -->
62 // 8: multiline comment /* */
63 // 9: single-line comment starting with two slashes //
64 // 10: single-line comment starting with hash #
65 tokenType = 0,
66
67 // kept to determine between regex and division
68 lastTokenType,
69 // flag determining if token is multi-character
70 multichar,
71 node,
72
73 // calculating the colors for the style templates
74 colorArr = /(\d*\, \d*\, \d*)(, ([.\d]*))?/g.exec(
75 _window.getComputedStyle(el).color
76 ),
77 pxColor = 'px rgba('+colorArr[1]+',',
78 alpha = colorArr[3]||1;
79
80 // running through characters and highlighting
81 while (prev2 = prev1,
82 // escaping if needed (with except for comments)
83 // pervious character will not be therefore
84 // recognized as a token finalize condition
85 prev1 = tokenType < 7 && prev1 == '\\' ? 1 : chr
86 ) {
87 chr = next1;
88 next1=text[++pos];
89 multichar = token.length > 1;
90
91 // checking if current token should be finalized
92 if (!chr || // end of content
93 // types 9-10 (single-line comments) end with a
94 // newline
95 (tokenType > 8 && chr == '\n') ||
96 [ // finalize conditions for other token types
97 // 0: whitespaces
98 /\S/[test](chr), // merged together
99 // 1: operators
100 1, // consist of a single character
101 // 2: braces
102 1, // consist of a single character
103 // 3: (key)word
104 !/[$\w]/[test](chr),
105 // 4: regex
106 (prev1 == '/' || prev1 == '\n') && multichar,
107 // 5: string with "
108 prev1 == '"' && multichar,
109 // 6: string with '
110 prev1 == "'" && multichar,
111 // 7: xml comment
112 text[pos-4]+prev2+prev1 == '-->',
113 // 8: multiline comment
114 prev2+prev1 == '*/'
115 ][tokenType]
116 ) {
117 // appending the token to the result
118 if (token) {
119 // remapping token type into style
120 // (some types are highlighted similarly)
121 el[appendChild](
122 node = _document.createElement('span')
123 ).setAttribute('style', [
124 // 0: not formatted
125 '',
126 // 1: keywords
127 textShadow + _0px_0px+9+pxColor + alpha * .7 + '),' +
128 _0px_0px+2+pxColor + alpha * .4 + brace,
129 // 2: punctuation
130 opacity + 6 +
131 textShadow + _0px_0px+7+pxColor + alpha / 4 + '),' +
132 _0px_0px+3+pxColor + alpha / 4 + brace,
133 // 3: strings and regexps
134 opacity + 7 +
135 textShadow + _3px_0px_5+pxColor + alpha / 5 + '),-' +
136 _3px_0px_5+pxColor + alpha / 5 + brace,
137 // 4: comments
138 'font-style:italic;'+
139 opacity + 5 +
140 textShadow + _3px_0px_5+pxColor + alpha / 4 + '),-' +
141 _3px_0px_5+pxColor + alpha / 4 + brace
142 ][
143 // not formatted
144 !tokenType ? 0 :
145 // punctuation
146 tokenType < 3 ? 2 :
147 // comments
148 tokenType > 6 ? 4 :
149 // regex and strings
150 tokenType > 3 ? 3 :
151 // otherwise tokenType == 3, (key)word
152 // (1 if regexp matches, 0 otherwise)
153 + /^(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)
154 ]);
155
156 node[appendChild](_document.createTextNode(token));
157 }
158
159 // saving the previous token type
160 // (skipping whitespaces and comments)
161 lastTokenType =
162 (tokenType && tokenType < 7) ?
163 tokenType : lastTokenType;
164
165 // initializing a new token
166 token = '';
167
168 // determining the new token type (going up the
169 // list until matching a token type start
170 // condition)
171 tokenType = 11;
172 while (![
173 1, // 0: whitespace
174 // 1: operator or braces
175 /[\/{}[(\-+*=<>:;|\\.,?!&@~]/[test](chr),
176 /[\])]/[test](chr), // 2: closing brace
177 /[$\w]/[test](chr), // 3: (key)word
178 chr == '/' && // 4: regex
179 // previous token was an
180 // opening brace or an
181 // operator (otherwise
182 // division, not a regex)
183 (lastTokenType < 2) &&
184 // workaround for xml
185 // closing tags
186 prev1 != '<',
187 chr == '"', // 5: string with "
188 chr == "'", // 6: string with '
189 // 7: xml comment
190 chr+next1+text[pos+1]+text[pos+2] == '<!--',
191 chr+next1 == '/*', // 8: multiline comment
192 chr+next1 == '//', // 9: single-line comment
193 chr == '#' // 10: hash-style comment
194 ][--tokenType]);
195 }
196
197 token += chr;
198 }
199 }
200 }
201
202 exports.reset = reset;
203
204 if (_document.readyState == 'complete') {
205 reset();
206 } else {
207 _window.addEventListener('load', function(){reset()}, 0);
208 }
209}));
210