summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlaise2022-10-28 18:58:01 -0500
committerBlaise2022-10-28 18:58:01 -0500
commita5105ae3d6a252f16eded4d281c4b0adfad4ccd7 (patch)
tree2b08bfd75bba94008500ede51d7c03e1bafe5123
parent476c93f0007ed41d8c791667f6d1bbe644096b91 (diff)
Highlight <p> tags and use class for styling outside script
-rw-r--r--proselight.js42
1 files changed, 5 insertions, 37 deletions
diff --git a/proselight.js b/proselight.js
index 184cbda..73222b1 100644
--- a/proselight.js
+++ b/proselight.js
@@ -23,12 +23,6 @@
23 _document = document, 23 _document = document,
24 appendChild = 'appendChild', 24 appendChild = 'appendChild',
25 test = 'test', 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 26
33 i, 27 i,
34 microlighted, 28 microlighted,
@@ -36,9 +30,9 @@
36 30
37 31
38 32
39 var reset = function(cls) { 33 var reset = function() {
40 // nodes to highlight 34 // nodes to highlight
41 microlighted = _document.getElementsByClassName(cls||'microlight'); 35 microlighted = _document.querySelectorAll('p');
42 36
43 for (i = 0; el = microlighted[i++];) { 37 for (i = 0; el = microlighted[i++];) {
44 var text = el.textContent, 38 var text = el.textContent,
@@ -68,14 +62,7 @@
68 lastTokenType, 62 lastTokenType,
69 // flag determining if token is multi-character 63 // flag determining if token is multi-character
70 multichar, 64 multichar,
71 node, 65 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 66
80 // running through characters and highlighting 67 // running through characters and highlighting
81 while (prev2 = prev1, 68 while (prev2 = prev1,
@@ -120,26 +107,7 @@
120 // (some types are highlighted similarly) 107 // (some types are highlighted similarly)
121 el[appendChild]( 108 el[appendChild](
122 node = _document.createElement('span') 109 node = _document.createElement('span')
123 ).setAttribute('style', [ 110 ).setAttribute('class', 'ph'+(
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 111 // not formatted
144 !tokenType ? 0 : 112 !tokenType ? 0 :
145 // punctuation 113 // punctuation
@@ -151,7 +119,7 @@
151 // otherwise tokenType == 3, (key)word 119 // otherwise tokenType == 3, (key)word
152 // (1 if regexp matches, 0 otherwise) 120 // (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) 121 + /^(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 ]); 122 ));
155 123
156 node[appendChild](_document.createTextNode(token)); 124 node[appendChild](_document.createTextNode(token));
157 } 125 }