diff options
author | Patrick McDermott <pj@pehjota.net> | 2017-10-31 04:09:44 (EDT) |
---|---|---|
committer | Patrick McDermott <pj@pehjota.net> | 2017-10-31 04:09:44 (EDT) |
commit | 490c2d20475e8b16136422d3d426b84abb828a23 (patch) | |
tree | 8a96f7932bf3fe969a0dcfc665c8e18f798f44eb /data/resources/prism/plugins | |
parent | 3f57a98661ac2fc4c73eab89e1b1590e3c547dba (diff) | |
download | marquee-490c2d20475e8b16136422d3d426b84abb828a23.zip marquee-490c2d20475e8b16136422d3d426b84abb828a23.tar.gz marquee-490c2d20475e8b16136422d3d426b84abb828a23.tar.bz2 |
data/resources/prism/: Import from upstream, HEAD of branch gh-pages
Diffstat (limited to 'data/resources/prism/plugins')
3 files changed, 253 insertions, 0 deletions
diff --git a/data/resources/prism/plugins/data-uri-highlight/prism-data-uri-highlight.js b/data/resources/prism/plugins/data-uri-highlight/prism-data-uri-highlight.js new file mode 100644 index 0000000..7ff8d1f --- /dev/null +++ b/data/resources/prism/plugins/data-uri-highlight/prism-data-uri-highlight.js @@ -0,0 +1,98 @@ +(function () { + + if ( + typeof self !== 'undefined' && !self.Prism || + typeof global !== 'undefined' && !global.Prism + ) { + return; + } + + var autoLinkerProcess = function (grammar) { + if (Prism.plugins.autolinker) { + Prism.plugins.autolinker.processGrammar(grammar); + } + return grammar; + }; + var dataURI = { + pattern: /(.)\bdata:[^\/]+\/[^,]+,(?:(?!\1)[\s\S]|\\\1)+(?=\1)/, + lookbehind: true, + inside: { + 'language-css': { + pattern: /(data:[^\/]+\/(?:[^+,]+\+)?css,)[\s\S]+/, + lookbehind: true + }, + 'language-javascript': { + pattern: /(data:[^\/]+\/(?:[^+,]+\+)?javascript,)[\s\S]+/, + lookbehind: true + }, + 'language-json': { + pattern: /(data:[^\/]+\/(?:[^+,]+\+)?json,)[\s\S]+/, + lookbehind: true + }, + 'language-markup': { + pattern: /(data:[^\/]+\/(?:[^+,]+\+)?(?:html|xml),)[\s\S]+/, + lookbehind: true + } + } + }; + + // Tokens that may contain URLs + var candidates = ['url', 'attr-value', 'string']; + + Prism.plugins.dataURIHighlight = { + processGrammar: function (grammar) { + // Abort if grammar has already been processed + if (!grammar || grammar['data-uri']) { + return; + } + + Prism.languages.DFS(grammar, function (key, def, type) { + if (candidates.indexOf(type) > -1 && Prism.util.type(def) !== 'Array') { + if (!def.pattern) { + def = this[key] = { + pattern: def + }; + } + + def.inside = def.inside || {}; + + if (type == 'attr-value') { + Prism.languages.insertBefore('inside', def.inside['url-link'] ? 'url-link' : 'punctuation', { + 'data-uri': dataURI + }, def); + } + else { + if (def.inside['url-link']) { + Prism.languages.insertBefore('inside', 'url-link', { + 'data-uri': dataURI + }, def); + } else { + def.inside['data-uri'] = dataURI; + } + } + } + }); + grammar['data-uri'] = dataURI; + } + }; + + Prism.hooks.add('before-highlight', function (env) { + // Prepare the needed grammars for this code block + if (dataURI.pattern.test(env.code)) { + for (var p in dataURI.inside) { + if (dataURI.inside.hasOwnProperty(p)) { + if (!dataURI.inside[p].inside && dataURI.inside[p].pattern.test(env.code)) { + var lang = p.match(/^language-(.+)/)[1]; + if (Prism.languages[lang]) { + dataURI.inside[p].inside = { + rest: autoLinkerProcess(Prism.languages[lang]) + }; + } + } + } + } + } + + Prism.plugins.dataURIHighlight.processGrammar(env.grammar); + }); +}());
\ No newline at end of file diff --git a/data/resources/prism/plugins/line-numbers/prism-line-numbers.css b/data/resources/prism/plugins/line-numbers/prism-line-numbers.css new file mode 100644 index 0000000..d5cbfd3 --- /dev/null +++ b/data/resources/prism/plugins/line-numbers/prism-line-numbers.css @@ -0,0 +1,41 @@ +pre.line-numbers { + position: relative; + padding-left: 3.8em; + counter-reset: linenumber; +} + +pre.line-numbers > code { + position: relative; + white-space: inherit; +} + +.line-numbers .line-numbers-rows { + position: absolute; + pointer-events: none; + top: 0; + font-size: 100%; + left: -3.8em; + width: 3em; /* works for line-numbers below 1000 lines */ + letter-spacing: -1px; + border-right: 1px solid #999; + + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; + +} + + .line-numbers-rows > span { + pointer-events: none; + display: block; + counter-increment: linenumber; + } + + .line-numbers-rows > span:before { + content: counter(linenumber); + color: #999; + display: block; + padding-right: 0.8em; + text-align: right; + }
\ No newline at end of file diff --git a/data/resources/prism/plugins/line-numbers/prism-line-numbers.js b/data/resources/prism/plugins/line-numbers/prism-line-numbers.js new file mode 100644 index 0000000..dea8e3a --- /dev/null +++ b/data/resources/prism/plugins/line-numbers/prism-line-numbers.js @@ -0,0 +1,114 @@ +(function () { + + if (typeof self === 'undefined' || !self.Prism || !self.document) { + return; + } + + /** + * Class name for <pre> which is activating the plugin + * @type {String} + */ + var PLUGIN_CLASS = 'line-numbers'; + + /** + * Resizes line numbers spans according to height of line of code + * @param {Element} element <pre> element + */ + var _resizeElement = function (element) { + var codeStyles = getStyles(element); + var whiteSpace = codeStyles['white-space']; + + if (whiteSpace === 'pre-wrap' || whiteSpace === 'pre-line') { + var codeElement = element.querySelector('code'); + var lineNumbersWrapper = element.querySelector('.line-numbers-rows'); + var lineNumberSizer = element.querySelector('.line-numbers-sizer'); + var codeLines = element.textContent.split('\n'); + + if (!lineNumberSizer) { + lineNumberSizer = document.createElement('span'); + lineNumberSizer.className = 'line-numbers-sizer'; + + codeElement.appendChild(lineNumberSizer); + } + + lineNumberSizer.style.display = 'block'; + + codeLines.forEach(function (line, lineNumber) { + lineNumberSizer.textContent = line || '\n'; + var lineSize = lineNumberSizer.getBoundingClientRect().height; + lineNumbersWrapper.children[lineNumber].style.height = lineSize + 'px'; + }); + + lineNumberSizer.textContent = ''; + lineNumberSizer.style.display = 'none'; + } + }; + + /** + * Returns style declarations for the element + * @param {Element} element + */ + var getStyles = function (element) { + if (!element) { + return null; + } + + return window.getComputedStyle ? getComputedStyle(element) : (element.currentStyle || null); + }; + + window.addEventListener('resize', function () { + Array.prototype.forEach.call(document.querySelectorAll('pre.' + PLUGIN_CLASS), _resizeElement); + }); + + Prism.hooks.add('complete', function (env) { + if (!env.code) { + return; + } + + // works only for <code> wrapped inside <pre> (not inline) + var pre = env.element.parentNode; + var clsReg = /\s*\bline-numbers\b\s*/; + if ( + !pre || !/pre/i.test(pre.nodeName) || + // Abort only if nor the <pre> nor the <code> have the class + (!clsReg.test(pre.className) && !clsReg.test(env.element.className)) + ) { + return; + } + + if (env.element.querySelector(".line-numbers-rows")) { + // Abort if line numbers already exists + return; + } + + if (clsReg.test(env.element.className)) { + // Remove the class "line-numbers" from the <code> + env.element.className = env.element.className.replace(clsReg, ' '); + } + if (!clsReg.test(pre.className)) { + // Add the class "line-numbers" to the <pre> + pre.className += ' line-numbers'; + } + + var match = env.code.match(/\n(?!$)/g); + var linesNum = match ? match.length + 1 : 1; + var lineNumbersWrapper; + + var lines = new Array(linesNum + 1); + lines = lines.join('<span></span>'); + + lineNumbersWrapper = document.createElement('span'); + lineNumbersWrapper.setAttribute('aria-hidden', 'true'); + lineNumbersWrapper.className = 'line-numbers-rows'; + lineNumbersWrapper.innerHTML = lines; + + if (pre.hasAttribute('data-start')) { + pre.style.counterReset = 'linenumber ' + (parseInt(pre.getAttribute('data-start'), 10) - 1); + } + + env.element.appendChild(lineNumbersWrapper); + + _resizeElement(pre); + }); + +}());
\ No newline at end of file |