/** * @fileoverview proselight, based on microlight - syntax highlightning library * @version 0.0.7 * * @license MIT, see http://github.com/asvd/microlight * @copyright 2016 asvd * * Code structure aims at minimizing the compressed library size */ (function (root, factory) { if (typeof define === 'function' && define.amd) { define(['exports'], factory); } else if (typeof exports !== 'undefined') { factory(exports); } else { factory((root.proselight = {})); } }(this, function (exports) { // for better compression var _window = window, _document = document, appendChild = 'appendChild', test = 'test', i, proselighted, cn, //child nodes el; // current proselighted element to run through var reset = function() { // nodes to highlight proselighted = _document.querySelectorAll('p'); for (i = 0; el = proselighted[i++];) { cn = Array.from(el.childNodes); for (j = 0; j < cn.length; j++) { if (cn[j].nodeType != Node.TEXT_NODE) { el.appendChild(cn[j]); continue; } el.removeChild(cn[j]); var text = cn[j].textContent, pos = 0, // current position next1 = text[0], // next character chr = 1, // current character prev1, // previous character prev2, // the one before the previous token = '', // current token content // current token type: tokenType = 0, // flag determining if token is multi-character multichar, node; // running through characters and highlighting while (prev2 = prev1, prev1 = chr) { chr = next1; next1=text[++pos]; multichar = token.length > 1; // checking if current token should be finalized if (!chr || // end of content [ // finalize conditions for other token types // 0: unformatted /[":;,\\.?!\])\/{}[(|]/[test](chr), // 1: parentesis or braces /[\]){}[(]/[test](prev1) && multichar, // 2: terminators 1, // consist of a single character // 3: separators 1, // consist of a single character // 4: quotes prev1 == '"' && multichar, ][tokenType] ) { // appending the token to the result if (token) { // map token type into class el[appendChild]( node = _document.createElement('span') ).setAttribute('class', 'ph'+tokenType); node[appendChild](_document.createTextNode(token)); } // initializing a new token token = ''; // determining the new token type (going up the // list until matching a token type start // condition) tokenType = 5; while (![ 1, // 0: unformatted /[\]){}[(]/[test](chr), // 1: parenthesis or braces /[\\.?!]/[test](chr), // 2: terminators /[:;,]/[test](chr), // 3: separators chr == '"', // 4: quotes ][--tokenType]); } token += chr; } } } } exports.reset = reset; if (_document.readyState == 'complete') { reset(); } else { _window.addEventListener('load', function(){reset()}, 0); } }));