From: Tim Düsterhus Date: Tue, 20 Nov 2018 20:20:13 +0000 (+0100) Subject: Add WoltLabSuite/Core/Prism X-Git-Tag: 5.2.0_Alpha_1~420^2~5^2~19 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=cf10f7b78b4148f4582d47e0fcc9cee4aa1aaa82;p=GitHub%2FWoltLab%2FWCF.git Add WoltLabSuite/Core/Prism see #2752 --- diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Prism.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Prism.js new file mode 100644 index 0000000000..76195f8929 --- /dev/null +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Prism.js @@ -0,0 +1,71 @@ +/** + * Augments the Prism syntax highlighter with additional functions. + * + * @author Tim Duesterhus + * @copyright 2001-2018 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLabSuite/Core/Prism + */ + +window.Prism = window.Prism || {} +window.Prism.manual = true + +define(['prism/prism'], function () { + Prism.highlightSeparateLines = function (text, language) { + var frag = new DocumentFragment(), + lineNo = 1, + it, node, line, grammar, container; + + function newLine() { + var line = elCreate('span'); + elData(line, 'number', lineNo++); + frag.appendChild(line); + + return line; + } + + function highlightStringIntoContainer() { + var container = elCreate('div'); + container.innerHTML = Prism.highlight(text, grammar, language); + return container; + } + + grammar = Prism.languages[language]; + if (!grammar) { + throw new Error('Invalid language ' + language + ' given.'); + } + + container = highlightStringIntoContainer(); + it = document.createNodeIterator(container, NodeFilter.SHOW_TEXT); + + line = newLine(lineNo); + while (node = it.nextNode()) { + node.data.split(/\r?\n/).forEach(function (codeLine, index) { + var current, parent; + + // We are behind a newline, insert \n and create new container. + if (index >= 1) { + line.appendChild(document.createTextNode("\n")); + line = newLine(lineNo); + } + + current = document.createTextNode(codeLine); + + // Copy hierarchy (to preserve CSS classes). + parent = node.parentNode + while (parent !== container) { + var clone = parent.cloneNode(false); + clone.appendChild(current); + current = clone; + parent = parent.parentNode; + } + + line.appendChild(current); + }); + } + + return frag; + }; + + return Prism; +}) diff --git a/wcfsetup/install/files/js/require.config.js b/wcfsetup/install/files/js/require.config.js index e9e544b71e..1986a37789 100644 --- a/wcfsetup/install/files/js/require.config.js +++ b/wcfsetup/install/files/js/require.config.js @@ -3,7 +3,8 @@ requirejs.config({ paths: { enquire: '3rdParty/enquire', favico: '3rdParty/favico', - 'perfect-scrollbar': '3rdParty/perfect-scrollbar' + 'perfect-scrollbar': '3rdParty/perfect-scrollbar', + prism: '3rdParty/prism', }, shim: { enquire: { exports: 'enquire' },