copy-paste-bookmarks/plugin/highlight/highlight.js

6 lines
778 KiB
JavaScript
Raw Normal View History

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).RevealHighlight=t()}(this,(function(){"use strict";function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(t)}function t(e){Object.freeze(e);var a="function"==typeof e;return Object.getOwnPropertyNames(e).forEach((function(n){!e.hasOwnProperty(n)||null===e[n]||"object"!=typeof e[n]&&"function"!=typeof e[n]||a&&("caller"===n||"callee"===n||"arguments"===n)||Object.isFrozen(e[n])||t(e[n])})),e}function a(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function n(e){var t,a={},n=Array.prototype.slice.call(arguments,1);for(t in e)a[t]=e[t];return n.forEach((function(e){for(t in e)a[t]=e[t]})),a}function i(e){return e.nodeName.toLowerCase()}var r=Object.freeze({__proto__:null,escapeHTML:a,inherit:n,nodeStream:function(e){var t=[];return function e(a,n){for(var r=a.firstChild;r;r=r.nextSibling)3===r.nodeType?n+=r.nodeValue.length:1===r.nodeType&&(t.push({event:"start",offset:n,node:r}),n=e(r,n),i(r).match(/br|hr|img|input/)||t.push({event:"stop",offset:n,node:r}));return n}(e,0),t},mergeStreams:function(e,t,n){var r=0,s="",o=[];function l(){return e.length&&t.length?e[0].offset!==t[0].offset?e[0].offset<t[0].offset?e:t:"start"===t[0].event?e:t:e.length?e:t}function _(e){s+="<"+i(e)+[].map.call(e.attributes,(function(e){return" "+e.nodeName+'="'+a(e.value).replace(/"/g,"&quot;")+'"'})).join("")+">"}function c(e){s+="</"+i(e)+">"}function d(e){("start"===e.event?_:c)(e.node)}for(;e.length||t.length;){var m=l();if(s+=a(n.substring(r,m[0].offset)),r=m[0].offset,m===e){o.reverse().forEach(c);do{d(m.splice(0,1)[0]),m=l()}while(m===e&&m.length&&m[0].offset===r);o.reverse().forEach(_)}else"start"===m[0].event?o.push(m[0].node):o.pop(),d(m.splice(0,1)[0])}return s+a(n.substr(r))}});const s=e=>!!e.kind;class o{constructor(e,t){this.buffer="",this.classPrefix=t.classPrefix,e.walk(this)}addText(e){this.buffer+=a(e)}openNode(e){if(!s(e))return;let t=e.kind;e.sublanguage||(t=`${this.classPrefix}${t}`),this.span(t)}closeNode(e){s(e)&&(this.buffer+="</span>")}span(e){this.buffer+=`<span class="${e}">`}value(){return this.buffer}}class l{constructor(){this.rootNode={children:[]},this.stack=[this.rootNode]}get top(){return this.stack[this.stack.length-1]}get root(){return this.rootNode}add(e){this.top.children.push(e)}openNode(e){let t={kind:e,children:[]};this.add(t),this.stack.push(t)}closeNode(){if(this.stack.length>1)return this.stack.pop()}closeAllNodes(){for(;this.closeNode(););}toJSON(){return JSON.stringify(this.rootNode,null,4)}walk(e){return this.constructor._walk(e,this.rootNode)}static _walk(e,t){return"string"==typeof t?e.addText(t):t.children&&(e.openNode(t),t.children.forEach(t=>this._walk(e,t)),e.closeNode(t)),e}static _collapse(e){e.children&&(e.children.every(e=>"string"==typeof e)?(e.text=e.children.join(""),delete e.children):e.children.forEach(e=>{"string"!=typeof e&&l._collapse(e)}))}}class _ extends l{constructor(e){super(),this.options=e}addKeyword(e,t){""!==e&&(this.openNode(t),this.addText(e),this.closeNode())}addText(e){""!==e&&this.add(e)}addSublanguage(e,t){let a=e.root;a.kind=t,a.sublanguage=!0,this.add(a)}toHTML(){return new o(this,this.options).value()}finalize(){}}function c(e){return e&&e.source||e}const d="(-?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)",m={begin:"\\\\[\\s\\S]",relevance:0},u={className:"string",begin:"'",end:"'",illegal:"\\n",contains:[m]},p={className:"string",begin:'"',end:'"',illegal:"\\n",contains:[m]},E={begin:/\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|they|like|more)\b/},g=function(e,t,a){var i=n({className:"comment",begin:e,end:t,contains:[]},a||{});return i.contains.push(E),i.contains.push({className:"doctag",begin:"(?:TODO|FIXME|NOTE|BUG|XXX):"
/*!
* reveal.js plugin that adds syntax highlight support.
*/
var mn={id:"highlight",HIGHLIGHT_STEP_DELIMITER:"|",HIGHLIGHT_LINE_DELIMITER:",",HIGHLIGHT_LINE_RANGE_DELIMITER:"-",init:function(e){var t=e.getConfig().highlight||{};t.highlightOnLoad="boolean"!=typeof t.highlightOnLoad||t.highlightOnLoad,t.escapeHTML="boolean"!=typeof t.escapeHTML||t.escapeHTML,[].slice.call(e.getRevealElement().querySelectorAll("pre code")).forEach((function(e){e.hasAttribute("data-trim")&&"function"==typeof e.innerHTML.trim&&(e.innerHTML=function(e){function t(e){return e.replace(/^[\s\uFEFF\xA0]+/g,"")}return function(e){var a=function(e){for(var t=e.split("\n"),a=0;a<t.length&&""===t[a].trim();a++)t.splice(a--,1);for(a=t.length-1;a>=0&&""===t[a].trim();a--)t.splice(a,1);return t.join("\n")}(e.innerHTML).split("\n"),n=a.reduce((function(e,a){return a.length>0&&t(a).length>0&&e>a.length-t(a).length?a.length-t(a).length:e}),Number.POSITIVE_INFINITY);return a.map((function(e,t){return e.slice(n)})).join("\n")}(e)}(e)),t.escapeHTML&&!e.hasAttribute("data-noescape")&&(e.innerHTML=e.innerHTML.replace(/</g,"&lt;").replace(/>/g,"&gt;")),e.addEventListener("focusout",(function(e){dn.highlightBlock(e.currentTarget)}),!1),t.highlightOnLoad&&mn.highlightBlock(e)})),e.on("pdf-ready",(function(){[].slice.call(e.getRevealElement().querySelectorAll("pre code[data-line-numbers].current-fragment")).forEach((function(e){mn.scrollHighlightedLineIntoView(e,{},!0)}))}))},highlightBlock:function(e){if(dn.highlightBlock(e),0!==e.innerHTML.trim().length&&e.hasAttribute("data-line-numbers")){dn.lineNumbersBlock(e,{singleLine:!0});var t={currentBlock:e},a=mn.deserializeHighlightSteps(e.getAttribute("data-line-numbers"));if(a.length>1){var n=parseInt(e.getAttribute("data-fragment-index"),10);("number"!=typeof n||isNaN(n))&&(n=null),a.slice(1).forEach((function(a){var i=e.cloneNode(!0);i.setAttribute("data-line-numbers",mn.serializeHighlightSteps([a])),i.classList.add("fragment"),e.parentNode.appendChild(i),mn.highlightLines(i),"number"==typeof n?(i.setAttribute("data-fragment-index",n),n+=1):i.removeAttribute("data-fragment-index"),i.addEventListener("visible",mn.scrollHighlightedLineIntoView.bind(mn,i,t)),i.addEventListener("hidden",mn.scrollHighlightedLineIntoView.bind(mn,i.previousSibling,t))})),e.removeAttribute("data-fragment-index"),e.setAttribute("data-line-numbers",mn.serializeHighlightSteps([a[0]]))}var i="function"==typeof e.closest?e.closest("section:not(.stack)"):null;if(i){i.addEventListener("visible",(function a(){mn.scrollHighlightedLineIntoView(e,t,!0),i.removeEventListener("visible",a)}))}mn.highlightLines(e)}},scrollHighlightedLineIntoView:function(e,t,a){cancelAnimationFrame(t.animationFrameID),t.currentBlock&&(e.scrollTop=t.currentBlock.scrollTop),t.currentBlock=e;var n=this.getHighlightedLineBounds(e),i=e.offsetHeight,r=getComputedStyle(e);i-=parseInt(r.paddingTop)+parseInt(r.paddingBottom);var s=e.scrollTop,o=n.top+(Math.min(n.bottom-n.top,i)-i)/2,l=e.querySelector(".hljs-ln");if(l&&(o+=l.offsetTop-parseInt(r.paddingTop)),o=Math.max(Math.min(o,e.scrollHeight-i),0),!0===a||s===o)e.scrollTop=o;else{if(e.scrollHeight<=i)return;var _=0;!function a(){_=Math.min(_+.02,1),e.scrollTop=s+(o-s)*mn.easeInOutQuart(_),_<1&&(t.animationFrameID=requestAnimationFrame(a))}()}},easeInOutQuart:function(e){return e<.5?8*e*e*e*e:1-8*--e*e*e*e},getHighlightedLineBounds:function(e){var t=e.querySelectorAll(".highlight-line");if(0===t.length)return{top:0,bottom:0};var a=t[0],n=t[t.length-1];return{top:a.offsetTop,bottom:n.offsetTop+n.offsetHeight}},highlightLines:function(e,t){var a=mn.deserializeHighlightSteps(t||e.getAttribute("data-line-numbers"));a.length&&a[0].forEach((function(t){var a=[];"number"==typeof t.end?a=[].slice.call(e.querySelectorAll("table tr:nth-child(n+"+t.start+"):nth-child(-n+"+t.end+")")):"number"==typeof t.start&&(a=[].slice.call(e.querySelectorAll("table tr:nth-child("+t.start+")"))),a.length&&(a.forEach((function(e){e.classList.add("highlight-line")})),e.classList.add("has-highlights"))}))},deserializeHighlightSteps:function(e){return(e=(e=e.replace(/\s/g,"")).split(mn.HIGHLIGHT_STEP_DELIMITER)).map((