prism.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  1. /* PrismJS 1.20.0
  2. https://prismjs.com/download.html#themes=prism-coy&languages=markup+css+clike+javascript+python+visual-basic&plugins=line-numbers+normalize-whitespace */
  3. var _self = (typeof window !== 'undefined')
  4. ? window // if in browser
  5. : (
  6. (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)
  7. ? self // if in worker
  8. : {} // if in node js
  9. );
  10. /**
  11. * Prism: Lightweight, robust, elegant syntax highlighting
  12. * MIT license http://www.opensource.org/licenses/mit-license.php/
  13. * @author Lea Verou http://lea.verou.me
  14. */
  15. var Prism = (function (_self){
  16. // Private helper vars
  17. var lang = /\blang(?:uage)?-([\w-]+)\b/i;
  18. var uniqueId = 0;
  19. var _ = {
  20. manual: _self.Prism && _self.Prism.manual,
  21. disableWorkerMessageHandler: _self.Prism && _self.Prism.disableWorkerMessageHandler,
  22. util: {
  23. encode: function encode(tokens) {
  24. if (tokens instanceof Token) {
  25. return new Token(tokens.type, encode(tokens.content), tokens.alias);
  26. } else if (Array.isArray(tokens)) {
  27. return tokens.map(encode);
  28. } else {
  29. return tokens.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\u00a0/g, ' ');
  30. }
  31. },
  32. type: function (o) {
  33. return Object.prototype.toString.call(o).slice(8, -1);
  34. },
  35. objId: function (obj) {
  36. if (!obj['__id']) {
  37. Object.defineProperty(obj, '__id', { value: ++uniqueId });
  38. }
  39. return obj['__id'];
  40. },
  41. // Deep clone a language definition (e.g. to extend it)
  42. clone: function deepClone(o, visited) {
  43. var clone, id, type = _.util.type(o);
  44. visited = visited || {};
  45. switch (type) {
  46. case 'Object':
  47. id = _.util.objId(o);
  48. if (visited[id]) {
  49. return visited[id];
  50. }
  51. clone = {};
  52. visited[id] = clone;
  53. for (var key in o) {
  54. if (o.hasOwnProperty(key)) {
  55. clone[key] = deepClone(o[key], visited);
  56. }
  57. }
  58. return clone;
  59. case 'Array':
  60. id = _.util.objId(o);
  61. if (visited[id]) {
  62. return visited[id];
  63. }
  64. clone = [];
  65. visited[id] = clone;
  66. o.forEach(function (v, i) {
  67. clone[i] = deepClone(v, visited);
  68. });
  69. return clone;
  70. default:
  71. return o;
  72. }
  73. },
  74. /**
  75. * Returns the Prism language of the given element set by a `language-xxxx` or `lang-xxxx` class.
  76. *
  77. * If no language is set for the element or the element is `null` or `undefined`, `none` will be returned.
  78. *
  79. * @param {Element} element
  80. * @returns {string}
  81. */
  82. getLanguage: function (element) {
  83. while (element && !lang.test(element.className)) {
  84. element = element.parentElement;
  85. }
  86. if (element) {
  87. return (element.className.match(lang) || [, 'none'])[1].toLowerCase();
  88. }
  89. return 'none';
  90. },
  91. /**
  92. * Returns the script element that is currently executing.
  93. *
  94. * This does __not__ work for line script element.
  95. *
  96. * @returns {HTMLScriptElement | null}
  97. */
  98. currentScript: function () {
  99. if (typeof document === 'undefined') {
  100. return null;
  101. }
  102. if ('currentScript' in document) {
  103. return document.currentScript;
  104. }
  105. // IE11 workaround
  106. // we'll get the src of the current script by parsing IE11's error stack trace
  107. // this will not work for inline scripts
  108. try {
  109. throw new Error();
  110. } catch (err) {
  111. // Get file src url from stack. Specifically works with the format of stack traces in IE.
  112. // A stack will look like this:
  113. //
  114. // Error
  115. // at _.util.currentScript (http://localhost/components/prism-core.js:119:5)
  116. // at Global code (http://localhost/components/prism-core.js:606:1)
  117. var src = (/at [^(\r\n]*\((.*):.+:.+\)$/i.exec(err.stack) || [])[1];
  118. if (src) {
  119. var scripts = document.getElementsByTagName('script');
  120. for (var i in scripts) {
  121. if (scripts[i].src == src) {
  122. return scripts[i];
  123. }
  124. }
  125. }
  126. return null;
  127. }
  128. }
  129. },
  130. languages: {
  131. extend: function (id, redef) {
  132. var lang = _.util.clone(_.languages[id]);
  133. for (var key in redef) {
  134. lang[key] = redef[key];
  135. }
  136. return lang;
  137. },
  138. /**
  139. * Insert a token before another token in a language literal
  140. * As this needs to recreate the object (we cannot actually insert before keys in object literals),
  141. * we cannot just provide an object, we need an object and a key.
  142. * @param inside The key (or language id) of the parent
  143. * @param before The key to insert before.
  144. * @param insert Object with the key/value pairs to insert
  145. * @param root The object that contains `inside`. If equal to Prism.languages, it can be omitted.
  146. */
  147. insertBefore: function (inside, before, insert, root) {
  148. root = root || _.languages;
  149. var grammar = root[inside];
  150. var ret = {};
  151. for (var token in grammar) {
  152. if (grammar.hasOwnProperty(token)) {
  153. if (token == before) {
  154. for (var newToken in insert) {
  155. if (insert.hasOwnProperty(newToken)) {
  156. ret[newToken] = insert[newToken];
  157. }
  158. }
  159. }
  160. // Do not insert token which also occur in insert. See #1525
  161. if (!insert.hasOwnProperty(token)) {
  162. ret[token] = grammar[token];
  163. }
  164. }
  165. }
  166. var old = root[inside];
  167. root[inside] = ret;
  168. // Update references in other language definitions
  169. _.languages.DFS(_.languages, function(key, value) {
  170. if (value === old && key != inside) {
  171. this[key] = ret;
  172. }
  173. });
  174. return ret;
  175. },
  176. // Traverse a language definition with Depth First Search
  177. DFS: function DFS(o, callback, type, visited) {
  178. visited = visited || {};
  179. var objId = _.util.objId;
  180. for (var i in o) {
  181. if (o.hasOwnProperty(i)) {
  182. callback.call(o, i, o[i], type || i);
  183. var property = o[i],
  184. propertyType = _.util.type(property);
  185. if (propertyType === 'Object' && !visited[objId(property)]) {
  186. visited[objId(property)] = true;
  187. DFS(property, callback, null, visited);
  188. }
  189. else if (propertyType === 'Array' && !visited[objId(property)]) {
  190. visited[objId(property)] = true;
  191. DFS(property, callback, i, visited);
  192. }
  193. }
  194. }
  195. }
  196. },
  197. plugins: {},
  198. highlightAll: function(async, callback) {
  199. _.highlightAllUnder(document, async, callback);
  200. },
  201. highlightAllUnder: function(container, async, callback) {
  202. var env = {
  203. callback: callback,
  204. container: container,
  205. selector: 'code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code'
  206. };
  207. _.hooks.run('before-highlightall', env);
  208. env.elements = Array.prototype.slice.apply(env.container.querySelectorAll(env.selector));
  209. _.hooks.run('before-all-elements-highlight', env);
  210. for (var i = 0, element; element = env.elements[i++];) {
  211. _.highlightElement(element, async === true, env.callback);
  212. }
  213. },
  214. highlightElement: function(element, async, callback) {
  215. // Find language
  216. var language = _.util.getLanguage(element);
  217. var grammar = _.languages[language];
  218. // Set language on the element, if not present
  219. element.className = element.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
  220. // Set language on the parent, for styling
  221. var parent = element.parentNode;
  222. if (parent && parent.nodeName.toLowerCase() === 'pre') {
  223. parent.className = parent.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
  224. }
  225. var code = element.textContent;
  226. var env = {
  227. element: element,
  228. language: language,
  229. grammar: grammar,
  230. code: code
  231. };
  232. function insertHighlightedCode(highlightedCode) {
  233. env.highlightedCode = highlightedCode;
  234. _.hooks.run('before-insert', env);
  235. env.element.innerHTML = env.highlightedCode;
  236. _.hooks.run('after-highlight', env);
  237. _.hooks.run('complete', env);
  238. callback && callback.call(env.element);
  239. }
  240. _.hooks.run('before-sanity-check', env);
  241. if (!env.code) {
  242. _.hooks.run('complete', env);
  243. callback && callback.call(env.element);
  244. return;
  245. }
  246. _.hooks.run('before-highlight', env);
  247. if (!env.grammar) {
  248. insertHighlightedCode(_.util.encode(env.code));
  249. return;
  250. }
  251. if (async && _self.Worker) {
  252. var worker = new Worker(_.filename);
  253. worker.onmessage = function(evt) {
  254. insertHighlightedCode(evt.data);
  255. };
  256. worker.postMessage(JSON.stringify({
  257. language: env.language,
  258. code: env.code,
  259. immediateClose: true
  260. }));
  261. }
  262. else {
  263. insertHighlightedCode(_.highlight(env.code, env.grammar, env.language));
  264. }
  265. },
  266. highlight: function (text, grammar, language) {
  267. var env = {
  268. code: text,
  269. grammar: grammar,
  270. language: language
  271. };
  272. _.hooks.run('before-tokenize', env);
  273. env.tokens = _.tokenize(env.code, env.grammar);
  274. _.hooks.run('after-tokenize', env);
  275. return Token.stringify(_.util.encode(env.tokens), env.language);
  276. },
  277. tokenize: function(text, grammar) {
  278. var rest = grammar.rest;
  279. if (rest) {
  280. for (var token in rest) {
  281. grammar[token] = rest[token];
  282. }
  283. delete grammar.rest;
  284. }
  285. var tokenList = new LinkedList();
  286. addAfter(tokenList, tokenList.head, text);
  287. matchGrammar(text, tokenList, grammar, tokenList.head, 0);
  288. return toArray(tokenList);
  289. },
  290. hooks: {
  291. all: {},
  292. add: function (name, callback) {
  293. var hooks = _.hooks.all;
  294. hooks[name] = hooks[name] || [];
  295. hooks[name].push(callback);
  296. },
  297. run: function (name, env) {
  298. var callbacks = _.hooks.all[name];
  299. if (!callbacks || !callbacks.length) {
  300. return;
  301. }
  302. for (var i=0, callback; callback = callbacks[i++];) {
  303. callback(env);
  304. }
  305. }
  306. },
  307. Token: Token
  308. };
  309. _self.Prism = _;
  310. function Token(type, content, alias, matchedStr, greedy) {
  311. this.type = type;
  312. this.content = content;
  313. this.alias = alias;
  314. // Copy of the full string this token was created from
  315. this.length = (matchedStr || '').length|0;
  316. this.greedy = !!greedy;
  317. }
  318. Token.stringify = function stringify(o, language) {
  319. if (typeof o == 'string') {
  320. return o;
  321. }
  322. if (Array.isArray(o)) {
  323. var s = '';
  324. o.forEach(function (e) {
  325. s += stringify(e, language);
  326. });
  327. return s;
  328. }
  329. var env = {
  330. type: o.type,
  331. content: stringify(o.content, language),
  332. tag: 'span',
  333. classes: ['token', o.type],
  334. attributes: {},
  335. language: language
  336. };
  337. var aliases = o.alias;
  338. if (aliases) {
  339. if (Array.isArray(aliases)) {
  340. Array.prototype.push.apply(env.classes, aliases);
  341. } else {
  342. env.classes.push(aliases);
  343. }
  344. }
  345. _.hooks.run('wrap', env);
  346. var attributes = '';
  347. for (var name in env.attributes) {
  348. attributes += ' ' + name + '="' + (env.attributes[name] || '').replace(/"/g, '&quot;') + '"';
  349. }
  350. return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + attributes + '>' + env.content + '</' + env.tag + '>';
  351. };
  352. /**
  353. * @param {string} text
  354. * @param {LinkedList<string | Token>} tokenList
  355. * @param {any} grammar
  356. * @param {LinkedListNode<string | Token>} startNode
  357. * @param {number} startPos
  358. * @param {boolean} [oneshot=false]
  359. * @param {string} [target]
  360. */
  361. function matchGrammar(text, tokenList, grammar, startNode, startPos, oneshot, target) {
  362. for (var token in grammar) {
  363. if (!grammar.hasOwnProperty(token) || !grammar[token]) {
  364. continue;
  365. }
  366. var patterns = grammar[token];
  367. patterns = Array.isArray(patterns) ? patterns : [patterns];
  368. for (var j = 0; j < patterns.length; ++j) {
  369. if (target && target == token + ',' + j) {
  370. return;
  371. }
  372. var pattern = patterns[j],
  373. inside = pattern.inside,
  374. lookbehind = !!pattern.lookbehind,
  375. greedy = !!pattern.greedy,
  376. lookbehindLength = 0,
  377. alias = pattern.alias;
  378. if (greedy && !pattern.pattern.global) {
  379. // Without the global flag, lastIndex won't work
  380. var flags = pattern.pattern.toString().match(/[imsuy]*$/)[0];
  381. pattern.pattern = RegExp(pattern.pattern.source, flags + 'g');
  382. }
  383. pattern = pattern.pattern || pattern;
  384. for ( // iterate the token list and keep track of the current token/string position
  385. var currentNode = startNode.next, pos = startPos;
  386. currentNode !== tokenList.tail;
  387. pos += currentNode.value.length, currentNode = currentNode.next
  388. ) {
  389. var str = currentNode.value;
  390. if (tokenList.length > text.length) {
  391. // Something went terribly wrong, ABORT, ABORT!
  392. return;
  393. }
  394. if (str instanceof Token) {
  395. continue;
  396. }
  397. var removeCount = 1; // this is the to parameter of removeBetween
  398. if (greedy && currentNode != tokenList.tail.prev) {
  399. pattern.lastIndex = pos;
  400. var match = pattern.exec(text);
  401. if (!match) {
  402. break;
  403. }
  404. var from = match.index + (lookbehind && match[1] ? match[1].length : 0);
  405. var to = match.index + match[0].length;
  406. var p = pos;
  407. // find the node that contains the match
  408. p += currentNode.value.length;
  409. while (from >= p) {
  410. currentNode = currentNode.next;
  411. p += currentNode.value.length;
  412. }
  413. // adjust pos (and p)
  414. p -= currentNode.value.length;
  415. pos = p;
  416. // the current node is a Token, then the match starts inside another Token, which is invalid
  417. if (currentNode.value instanceof Token) {
  418. continue;
  419. }
  420. // find the last node which is affected by this match
  421. for (
  422. var k = currentNode;
  423. k !== tokenList.tail && (p < to || (typeof k.value === 'string' && !k.prev.value.greedy));
  424. k = k.next
  425. ) {
  426. removeCount++;
  427. p += k.value.length;
  428. }
  429. removeCount--;
  430. // replace with the new match
  431. str = text.slice(pos, p);
  432. match.index -= pos;
  433. } else {
  434. pattern.lastIndex = 0;
  435. var match = pattern.exec(str);
  436. }
  437. if (!match) {
  438. if (oneshot) {
  439. break;
  440. }
  441. continue;
  442. }
  443. if (lookbehind) {
  444. lookbehindLength = match[1] ? match[1].length : 0;
  445. }
  446. var from = match.index + lookbehindLength,
  447. match = match[0].slice(lookbehindLength),
  448. to = from + match.length,
  449. before = str.slice(0, from),
  450. after = str.slice(to);
  451. var removeFrom = currentNode.prev;
  452. if (before) {
  453. removeFrom = addAfter(tokenList, removeFrom, before);
  454. pos += before.length;
  455. }
  456. removeRange(tokenList, removeFrom, removeCount);
  457. var wrapped = new Token(token, inside ? _.tokenize(match, inside) : match, alias, match, greedy);
  458. currentNode = addAfter(tokenList, removeFrom, wrapped);
  459. if (after) {
  460. addAfter(tokenList, currentNode, after);
  461. }
  462. if (removeCount > 1)
  463. matchGrammar(text, tokenList, grammar, currentNode.prev, pos, true, token + ',' + j);
  464. if (oneshot)
  465. break;
  466. }
  467. }
  468. }
  469. }
  470. /**
  471. * @typedef LinkedListNode
  472. * @property {T} value
  473. * @property {LinkedListNode<T> | null} prev The previous node.
  474. * @property {LinkedListNode<T> | null} next The next node.
  475. * @template T
  476. */
  477. /**
  478. * @template T
  479. */
  480. function LinkedList() {
  481. /** @type {LinkedListNode<T>} */
  482. var head = { value: null, prev: null, next: null };
  483. /** @type {LinkedListNode<T>} */
  484. var tail = { value: null, prev: head, next: null };
  485. head.next = tail;
  486. /** @type {LinkedListNode<T>} */
  487. this.head = head;
  488. /** @type {LinkedListNode<T>} */
  489. this.tail = tail;
  490. this.length = 0;
  491. }
  492. /**
  493. * Adds a new node with the given value to the list.
  494. * @param {LinkedList<T>} list
  495. * @param {LinkedListNode<T>} node
  496. * @param {T} value
  497. * @returns {LinkedListNode<T>} The added node.
  498. * @template T
  499. */
  500. function addAfter(list, node, value) {
  501. // assumes that node != list.tail && values.length >= 0
  502. var next = node.next;
  503. var newNode = { value: value, prev: node, next: next };
  504. node.next = newNode;
  505. next.prev = newNode;
  506. list.length++;
  507. return newNode;
  508. }
  509. /**
  510. * Removes `count` nodes after the given node. The given node will not be removed.
  511. * @param {LinkedList<T>} list
  512. * @param {LinkedListNode<T>} node
  513. * @param {number} count
  514. * @template T
  515. */
  516. function removeRange(list, node, count) {
  517. var next = node.next;
  518. for (var i = 0; i < count && next !== list.tail; i++) {
  519. next = next.next;
  520. }
  521. node.next = next;
  522. next.prev = node;
  523. list.length -= i;
  524. }
  525. /**
  526. * @param {LinkedList<T>} list
  527. * @returns {T[]}
  528. * @template T
  529. */
  530. function toArray(list) {
  531. var array = [];
  532. var node = list.head.next;
  533. while (node !== list.tail) {
  534. array.push(node.value);
  535. node = node.next;
  536. }
  537. return array;
  538. }
  539. if (!_self.document) {
  540. if (!_self.addEventListener) {
  541. // in Node.js
  542. return _;
  543. }
  544. if (!_.disableWorkerMessageHandler) {
  545. // In worker
  546. _self.addEventListener('message', function (evt) {
  547. var message = JSON.parse(evt.data),
  548. lang = message.language,
  549. code = message.code,
  550. immediateClose = message.immediateClose;
  551. _self.postMessage(_.highlight(code, _.languages[lang], lang));
  552. if (immediateClose) {
  553. _self.close();
  554. }
  555. }, false);
  556. }
  557. return _;
  558. }
  559. //Get current script and highlight
  560. var script = _.util.currentScript();
  561. if (script) {
  562. _.filename = script.src;
  563. if (script.hasAttribute('data-manual')) {
  564. _.manual = true;
  565. }
  566. }
  567. function highlightAutomaticallyCallback() {
  568. if (!_.manual) {
  569. _.highlightAll();
  570. }
  571. }
  572. if (!_.manual) {
  573. // If the document state is "loading", then we'll use DOMContentLoaded.
  574. // If the document state is "interactive" and the prism.js script is deferred, then we'll also use the
  575. // DOMContentLoaded event because there might be some plugins or languages which have also been deferred and they
  576. // might take longer one animation frame to execute which can create a race condition where only some plugins have
  577. // been loaded when Prism.highlightAll() is executed, depending on how fast resources are loaded.
  578. // See https://github.com/PrismJS/prism/issues/2102
  579. var readyState = document.readyState;
  580. if (readyState === 'loading' || readyState === 'interactive' && script && script.defer) {
  581. document.addEventListener('DOMContentLoaded', highlightAutomaticallyCallback);
  582. } else {
  583. if (window.requestAnimationFrame) {
  584. window.requestAnimationFrame(highlightAutomaticallyCallback);
  585. } else {
  586. window.setTimeout(highlightAutomaticallyCallback, 16);
  587. }
  588. }
  589. }
  590. return _;
  591. })(_self);
  592. if (typeof module !== 'undefined' && module.exports) {
  593. module.exports = Prism;
  594. }
  595. // hack for components to work correctly in node.js
  596. if (typeof global !== 'undefined') {
  597. global.Prism = Prism;
  598. }
  599. ;
  600. Prism.languages.markup = {
  601. 'comment': /<!--[\s\S]*?-->/,
  602. 'prolog': /<\?[\s\S]+?\?>/,
  603. 'doctype': {
  604. // https://www.w3.org/TR/xml/#NT-doctypedecl
  605. pattern: /<!DOCTYPE(?:[^>"'[\]]|"[^"]*"|'[^']*')+(?:\[(?:[^<"'\]]|"[^"]*"|'[^']*'|<(?!!--)|<!--(?:[^-]|-(?!->))*-->)*\]\s*)?>/i,
  606. greedy: true,
  607. inside: {
  608. 'internal-subset': {
  609. pattern: /(\[)[\s\S]+(?=\]>$)/,
  610. lookbehind: true,
  611. greedy: true,
  612. inside: null // see below
  613. },
  614. 'string': {
  615. pattern: /"[^"]*"|'[^']*'/,
  616. greedy: true
  617. },
  618. 'punctuation': /^<!|>$|[[\]]/,
  619. 'doctype-tag': /^DOCTYPE/,
  620. 'name': /[^\s<>'"]+/
  621. }
  622. },
  623. 'cdata': /<!\[CDATA\[[\s\S]*?]]>/i,
  624. 'tag': {
  625. pattern: /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/,
  626. greedy: true,
  627. inside: {
  628. 'tag': {
  629. pattern: /^<\/?[^\s>\/]+/,
  630. inside: {
  631. 'punctuation': /^<\/?/,
  632. 'namespace': /^[^\s>\/:]+:/
  633. }
  634. },
  635. 'attr-value': {
  636. pattern: /=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+)/,
  637. inside: {
  638. 'punctuation': [
  639. {
  640. pattern: /^=/,
  641. alias: 'attr-equals'
  642. },
  643. /"|'/
  644. ]
  645. }
  646. },
  647. 'punctuation': /\/?>/,
  648. 'attr-name': {
  649. pattern: /[^\s>\/]+/,
  650. inside: {
  651. 'namespace': /^[^\s>\/:]+:/
  652. }
  653. }
  654. }
  655. },
  656. 'entity': [
  657. {
  658. pattern: /&[\da-z]{1,8};/i,
  659. alias: 'named-entity'
  660. },
  661. /&#x?[\da-f]{1,8};/i
  662. ]
  663. };
  664. Prism.languages.markup['tag'].inside['attr-value'].inside['entity'] =
  665. Prism.languages.markup['entity'];
  666. Prism.languages.markup['doctype'].inside['internal-subset'].inside = Prism.languages.markup;
  667. // Plugin to make entity title show the real entity, idea by Roman Komarov
  668. Prism.hooks.add('wrap', function (env) {
  669. if (env.type === 'entity') {
  670. env.attributes['title'] = env.content.replace(/&amp;/, '&');
  671. }
  672. });
  673. Object.defineProperty(Prism.languages.markup.tag, 'addInlined', {
  674. /**
  675. * Adds an inlined language to markup.
  676. *
  677. * An example of an inlined language is CSS with `<style>` tags.
  678. *
  679. * @param {string} tagName The name of the tag that contains the inlined language. This name will be treated as
  680. * case insensitive.
  681. * @param {string} lang The language key.
  682. * @example
  683. * addInlined('style', 'css');
  684. */
  685. value: function addInlined(tagName, lang) {
  686. var includedCdataInside = {};
  687. includedCdataInside['language-' + lang] = {
  688. pattern: /(^<!\[CDATA\[)[\s\S]+?(?=\]\]>$)/i,
  689. lookbehind: true,
  690. inside: Prism.languages[lang]
  691. };
  692. includedCdataInside['cdata'] = /^<!\[CDATA\[|\]\]>$/i;
  693. var inside = {
  694. 'included-cdata': {
  695. pattern: /<!\[CDATA\[[\s\S]*?\]\]>/i,
  696. inside: includedCdataInside
  697. }
  698. };
  699. inside['language-' + lang] = {
  700. pattern: /[\s\S]+/,
  701. inside: Prism.languages[lang]
  702. };
  703. var def = {};
  704. def[tagName] = {
  705. pattern: RegExp(/(<__[\s\S]*?>)(?:<!\[CDATA\[(?:[^\]]|\](?!\]>))*\]\]>|(?!<!\[CDATA\[)[\s\S])*?(?=<\/__>)/.source.replace(/__/g, function () { return tagName; }), 'i'),
  706. lookbehind: true,
  707. greedy: true,
  708. inside: inside
  709. };
  710. Prism.languages.insertBefore('markup', 'cdata', def);
  711. }
  712. });
  713. Prism.languages.html = Prism.languages.markup;
  714. Prism.languages.mathml = Prism.languages.markup;
  715. Prism.languages.svg = Prism.languages.markup;
  716. Prism.languages.xml = Prism.languages.extend('markup', {});
  717. Prism.languages.ssml = Prism.languages.xml;
  718. Prism.languages.atom = Prism.languages.xml;
  719. Prism.languages.rss = Prism.languages.xml;
  720. (function (Prism) {
  721. var string = /("|')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/;
  722. Prism.languages.css = {
  723. 'comment': /\/\*[\s\S]*?\*\//,
  724. 'atrule': {
  725. pattern: /@[\w-]+[\s\S]*?(?:;|(?=\s*\{))/,
  726. inside: {
  727. 'rule': /^@[\w-]+/,
  728. 'selector-function-argument': {
  729. pattern: /(\bselector\s*\((?!\s*\))\s*)(?:[^()]|\((?:[^()]|\([^()]*\))*\))+?(?=\s*\))/,
  730. lookbehind: true,
  731. alias: 'selector'
  732. }
  733. // See rest below
  734. }
  735. },
  736. 'url': {
  737. pattern: RegExp('url\\((?:' + string.source + '|[^\n\r()]*)\\)', 'i'),
  738. greedy: true,
  739. inside: {
  740. 'function': /^url/i,
  741. 'punctuation': /^\(|\)$/
  742. }
  743. },
  744. 'selector': RegExp('[^{}\\s](?:[^{};"\']|' + string.source + ')*?(?=\\s*\\{)'),
  745. 'string': {
  746. pattern: string,
  747. greedy: true
  748. },
  749. 'property': /[-_a-z\xA0-\uFFFF][-\w\xA0-\uFFFF]*(?=\s*:)/i,
  750. 'important': /!important\b/i,
  751. 'function': /[-a-z0-9]+(?=\()/i,
  752. 'punctuation': /[(){};:,]/
  753. };
  754. Prism.languages.css['atrule'].inside.rest = Prism.languages.css;
  755. var markup = Prism.languages.markup;
  756. if (markup) {
  757. markup.tag.addInlined('style', 'css');
  758. Prism.languages.insertBefore('inside', 'attr-value', {
  759. 'style-attr': {
  760. pattern: /\s*style=("|')(?:\\[\s\S]|(?!\1)[^\\])*\1/i,
  761. inside: {
  762. 'attr-name': {
  763. pattern: /^\s*style/i,
  764. inside: markup.tag.inside
  765. },
  766. 'punctuation': /^\s*=\s*['"]|['"]\s*$/,
  767. 'attr-value': {
  768. pattern: /.+/i,
  769. inside: Prism.languages.css
  770. }
  771. },
  772. alias: 'language-css'
  773. }
  774. }, markup.tag);
  775. }
  776. }(Prism));
  777. Prism.languages.clike = {
  778. 'comment': [
  779. {
  780. pattern: /(^|[^\\])\/\*[\s\S]*?(?:\*\/|$)/,
  781. lookbehind: true
  782. },
  783. {
  784. pattern: /(^|[^\\:])\/\/.*/,
  785. lookbehind: true,
  786. greedy: true
  787. }
  788. ],
  789. 'string': {
  790. pattern: /(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
  791. greedy: true
  792. },
  793. 'class-name': {
  794. pattern: /(\b(?:class|interface|extends|implements|trait|instanceof|new)\s+|\bcatch\s+\()[\w.\\]+/i,
  795. lookbehind: true,
  796. inside: {
  797. 'punctuation': /[.\\]/
  798. }
  799. },
  800. 'keyword': /\b(?:if|else|while|do|for|return|in|instanceof|function|new|try|throw|catch|finally|null|break|continue)\b/,
  801. 'boolean': /\b(?:true|false)\b/,
  802. 'function': /\w+(?=\()/,
  803. 'number': /\b0x[\da-f]+\b|(?:\b\d+\.?\d*|\B\.\d+)(?:e[+-]?\d+)?/i,
  804. 'operator': /[<>]=?|[!=]=?=?|--?|\+\+?|&&?|\|\|?|[?*/~^%]/,
  805. 'punctuation': /[{}[\];(),.:]/
  806. };
  807. Prism.languages.javascript = Prism.languages.extend('clike', {
  808. 'class-name': [
  809. Prism.languages.clike['class-name'],
  810. {
  811. pattern: /(^|[^$\w\xA0-\uFFFF])[_$A-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\.(?:prototype|constructor))/,
  812. lookbehind: true
  813. }
  814. ],
  815. 'keyword': [
  816. {
  817. pattern: /((?:^|})\s*)(?:catch|finally)\b/,
  818. lookbehind: true
  819. },
  820. {
  821. pattern: /(^|[^.]|\.\.\.\s*)\b(?:as|async(?=\s*(?:function\b|\(|[$\w\xA0-\uFFFF]|$))|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|(?:get|set)(?=\s*[\[$\w\xA0-\uFFFF])|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)\b/,
  822. lookbehind: true
  823. },
  824. ],
  825. 'number': /\b(?:(?:0[xX](?:[\dA-Fa-f](?:_[\dA-Fa-f])?)+|0[bB](?:[01](?:_[01])?)+|0[oO](?:[0-7](?:_[0-7])?)+)n?|(?:\d(?:_\d)?)+n|NaN|Infinity)\b|(?:\b(?:\d(?:_\d)?)+\.?(?:\d(?:_\d)?)*|\B\.(?:\d(?:_\d)?)+)(?:[Ee][+-]?(?:\d(?:_\d)?)+)?/,
  826. // Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
  827. 'function': /#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
  828. 'operator': /--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]/
  829. });
  830. Prism.languages.javascript['class-name'][0].pattern = /(\b(?:class|interface|extends|implements|instanceof|new)\s+)[\w.\\]+/;
  831. Prism.languages.insertBefore('javascript', 'keyword', {
  832. 'regex': {
  833. pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(?:\[(?:[^\]\\\r\n]|\\.)*]|\\.|[^/\\\[\r\n])+\/[gimyus]{0,6}(?=(?:\s|\/\*(?:[^*]|\*(?!\/))*\*\/)*(?:$|[\r\n,.;:})\]]|\/\/))/,
  834. lookbehind: true,
  835. greedy: true
  836. },
  837. // This must be declared before keyword because we use "function" inside the look-forward
  838. 'function-variable': {
  839. pattern: /#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\((?:[^()]|\([^()]*\))*\)|[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/,
  840. alias: 'function'
  841. },
  842. 'parameter': [
  843. {
  844. pattern: /(function(?:\s+[_$A-Za-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)?\s*\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\))/,
  845. lookbehind: true,
  846. inside: Prism.languages.javascript
  847. },
  848. {
  849. pattern: /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=>)/i,
  850. inside: Prism.languages.javascript
  851. },
  852. {
  853. pattern: /(\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\)\s*=>)/,
  854. lookbehind: true,
  855. inside: Prism.languages.javascript
  856. },
  857. {
  858. pattern: /((?:\b|\s|^)(?!(?:as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)(?![$\w\xA0-\uFFFF]))(?:[_$A-Za-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)\(\s*|\]\s*\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\)\s*\{)/,
  859. lookbehind: true,
  860. inside: Prism.languages.javascript
  861. }
  862. ],
  863. 'constant': /\b[A-Z](?:[A-Z_]|\dx?)*\b/
  864. });
  865. Prism.languages.insertBefore('javascript', 'string', {
  866. 'template-string': {
  867. pattern: /`(?:\\[\s\S]|\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})+}|(?!\${)[^\\`])*`/,
  868. greedy: true,
  869. inside: {
  870. 'template-punctuation': {
  871. pattern: /^`|`$/,
  872. alias: 'string'
  873. },
  874. 'interpolation': {
  875. pattern: /((?:^|[^\\])(?:\\{2})*)\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})+}/,
  876. lookbehind: true,
  877. inside: {
  878. 'interpolation-punctuation': {
  879. pattern: /^\${|}$/,
  880. alias: 'punctuation'
  881. },
  882. rest: Prism.languages.javascript
  883. }
  884. },
  885. 'string': /[\s\S]+/
  886. }
  887. }
  888. });
  889. if (Prism.languages.markup) {
  890. Prism.languages.markup.tag.addInlined('script', 'javascript');
  891. }
  892. Prism.languages.js = Prism.languages.javascript;
  893. Prism.languages.python = {
  894. 'comment': {
  895. pattern: /(^|[^\\])#.*/,
  896. lookbehind: true
  897. },
  898. 'string-interpolation': {
  899. pattern: /(?:f|rf|fr)(?:("""|''')[\s\S]*?\1|("|')(?:\\.|(?!\2)[^\\\r\n])*\2)/i,
  900. greedy: true,
  901. inside: {
  902. 'interpolation': {
  903. // "{" <expression> <optional "!s", "!r", or "!a"> <optional ":" format specifier> "}"
  904. pattern: /((?:^|[^{])(?:{{)*){(?!{)(?:[^{}]|{(?!{)(?:[^{}]|{(?!{)(?:[^{}])+})+})+}/,
  905. lookbehind: true,
  906. inside: {
  907. 'format-spec': {
  908. pattern: /(:)[^:(){}]+(?=}$)/,
  909. lookbehind: true
  910. },
  911. 'conversion-option': {
  912. pattern: /![sra](?=[:}]$)/,
  913. alias: 'punctuation'
  914. },
  915. rest: null
  916. }
  917. },
  918. 'string': /[\s\S]+/
  919. }
  920. },
  921. 'triple-quoted-string': {
  922. pattern: /(?:[rub]|rb|br)?("""|''')[\s\S]*?\1/i,
  923. greedy: true,
  924. alias: 'string'
  925. },
  926. 'string': {
  927. pattern: /(?:[rub]|rb|br)?("|')(?:\\.|(?!\1)[^\\\r\n])*\1/i,
  928. greedy: true
  929. },
  930. 'function': {
  931. pattern: /((?:^|\s)def[ \t]+)[a-zA-Z_]\w*(?=\s*\()/g,
  932. lookbehind: true
  933. },
  934. 'class-name': {
  935. pattern: /(\bclass\s+)\w+/i,
  936. lookbehind: true
  937. },
  938. 'decorator': {
  939. pattern: /(^\s*)@\w+(?:\.\w+)*/im,
  940. lookbehind: true,
  941. alias: ['annotation', 'punctuation'],
  942. inside: {
  943. 'punctuation': /\./
  944. }
  945. },
  946. 'keyword': /\b(?:and|as|assert|async|await|break|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|nonlocal|not|or|pass|print|raise|return|try|while|with|yield)\b/,
  947. 'builtin': /\b(?:__import__|abs|all|any|apply|ascii|basestring|bin|bool|buffer|bytearray|bytes|callable|chr|classmethod|cmp|coerce|compile|complex|delattr|dict|dir|divmod|enumerate|eval|execfile|file|filter|float|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|int|intern|isinstance|issubclass|iter|len|list|locals|long|map|max|memoryview|min|next|object|oct|open|ord|pow|property|range|raw_input|reduce|reload|repr|reversed|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|unichr|unicode|vars|xrange|zip)\b/,
  948. 'boolean': /\b(?:True|False|None)\b/,
  949. 'number': /(?:\b(?=\d)|\B(?=\.))(?:0[bo])?(?:(?:\d|0x[\da-f])[\da-f]*\.?\d*|\.\d+)(?:e[+-]?\d+)?j?\b/i,
  950. 'operator': /[-+%=]=?|!=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~]/,
  951. 'punctuation': /[{}[\];(),.:]/
  952. };
  953. Prism.languages.python['string-interpolation'].inside['interpolation'].inside.rest = Prism.languages.python;
  954. Prism.languages.py = Prism.languages.python;
  955. Prism.languages['visual-basic'] = {
  956. 'comment': {
  957. pattern: /(?:['‘’]|REM\b)(?:[^\r\n_]|_(?:\r\n?|\n)?)*/i,
  958. inside: {
  959. 'keyword': /^REM/i
  960. }
  961. },
  962. 'directive': {
  963. pattern: /#(?:Const|Else|ElseIf|End|ExternalChecksum|ExternalSource|If|Region)(?:[^\S\r\n]_[^\S\r\n]*(?:\r\n?|\n)|.)+/i,
  964. alias: 'comment',
  965. greedy: true
  966. },
  967. 'string': {
  968. pattern: /\$?["“”](?:["“”]{2}|[^"“”])*["“”]C?/i,
  969. greedy: true
  970. },
  971. 'date': {
  972. pattern: /#[^\S\r\n]*(?:\d+([/-])\d+\1\d+(?:[^\S\r\n]+(?:\d+[^\S\r\n]*(?:AM|PM)|\d+:\d+(?::\d+)?(?:[^\S\r\n]*(?:AM|PM))?))?|(?:\d+[^\S\r\n]*(?:AM|PM)|\d+:\d+(?::\d+)?(?:[^\S\r\n]*(?:AM|PM))?))[^\S\r\n]*#/i,
  973. alias: 'builtin'
  974. },
  975. 'number': /(?:(?:\b\d+(?:\.\d+)?|\.\d+)(?:E[+-]?\d+)?|&[HO][\dA-F]+)(?:U?[ILS]|[FRD])?/i,
  976. 'boolean': /\b(?:True|False|Nothing)\b/i,
  977. 'keyword': /\b(?:AddHandler|AddressOf|Alias|And(?:Also)?|As|Boolean|ByRef|Byte|ByVal|Call|Case|Catch|C(?:Bool|Byte|Char|Date|Dbl|Dec|Int|Lng|Obj|SByte|Short|Sng|Str|Type|UInt|ULng|UShort)|Char|Class|Const|Continue|Currency|Date|Decimal|Declare|Default|Delegate|Dim|DirectCast|Do|Double|Each|Else(?:If)?|End(?:If)?|Enum|Erase|Error|Event|Exit|Finally|For|Friend|Function|Get(?:Type|XMLNamespace)?|Global|GoSub|GoTo|Handles|If|Implements|Imports|In|Inherits|Integer|Interface|Is|IsNot|Let|Lib|Like|Long|Loop|Me|Mod|Module|Must(?:Inherit|Override)|My(?:Base|Class)|Namespace|Narrowing|New|Next|Not(?:Inheritable|Overridable)?|Object|Of|On|Operator|Option(?:al)?|Or(?:Else)?|Out|Overloads|Overridable|Overrides|ParamArray|Partial|Private|Property|Protected|Public|RaiseEvent|ReadOnly|ReDim|RemoveHandler|Resume|Return|SByte|Select|Set|Shadows|Shared|short|Single|Static|Step|Stop|String|Structure|Sub|SyncLock|Then|Throw|To|Try|TryCast|Type|TypeOf|U(?:Integer|Long|Short)|Using|Variant|Wend|When|While|Widening|With(?:Events)?|WriteOnly|Xor)\b/i,
  978. 'operator': [
  979. /[+\-*/\\^<=>&#@$%!]/,
  980. {
  981. pattern: /([^\S\r\n])_(?=[^\S\r\n]*[\r\n])/,
  982. lookbehind: true
  983. }
  984. ],
  985. 'punctuation': /[{}().,:?]/
  986. };
  987. Prism.languages.vb = Prism.languages['visual-basic'];
  988. (function () {
  989. if (typeof self === 'undefined' || !self.Prism || !self.document) {
  990. return;
  991. }
  992. /**
  993. * Plugin name which is used as a class name for <pre> which is activating the plugin
  994. * @type {String}
  995. */
  996. var PLUGIN_NAME = 'line-numbers';
  997. /**
  998. * Regular expression used for determining line breaks
  999. * @type {RegExp}
  1000. */
  1001. var NEW_LINE_EXP = /\n(?!$)/g;
  1002. /**
  1003. * Resizes line numbers spans according to height of line of code
  1004. * @param {Element} element <pre> element
  1005. */
  1006. var _resizeElement = function (element) {
  1007. var codeStyles = getStyles(element);
  1008. var whiteSpace = codeStyles['white-space'];
  1009. if (whiteSpace === 'pre-wrap' || whiteSpace === 'pre-line') {
  1010. var codeElement = element.querySelector('code');
  1011. var lineNumbersWrapper = element.querySelector('.line-numbers-rows');
  1012. if (!codeElement || !lineNumbersWrapper) {
  1013. return;
  1014. }
  1015. var lineNumberSizer = element.querySelector('.line-numbers-sizer');
  1016. var codeLines = codeElement.textContent.split(NEW_LINE_EXP);
  1017. if (!lineNumberSizer) {
  1018. lineNumberSizer = document.createElement('span');
  1019. lineNumberSizer.className = 'line-numbers-sizer';
  1020. codeElement.appendChild(lineNumberSizer);
  1021. }
  1022. lineNumberSizer.style.display = 'block';
  1023. codeLines.forEach(function (line, lineNumber) {
  1024. lineNumberSizer.textContent = line || '\n';
  1025. var lineSize = lineNumberSizer.getBoundingClientRect().height;
  1026. lineNumbersWrapper.children[lineNumber].style.height = lineSize + 'px';
  1027. });
  1028. lineNumberSizer.textContent = '';
  1029. lineNumberSizer.style.display = 'none';
  1030. }
  1031. };
  1032. /**
  1033. * Returns style declarations for the element
  1034. * @param {Element} element
  1035. */
  1036. var getStyles = function (element) {
  1037. if (!element) {
  1038. return null;
  1039. }
  1040. return window.getComputedStyle ? getComputedStyle(element) : (element.currentStyle || null);
  1041. };
  1042. window.addEventListener('resize', function () {
  1043. Array.prototype.forEach.call(document.querySelectorAll('pre.' + PLUGIN_NAME), _resizeElement);
  1044. });
  1045. Prism.hooks.add('complete', function (env) {
  1046. if (!env.code) {
  1047. return;
  1048. }
  1049. var code = env.element;
  1050. var pre = code.parentNode;
  1051. // works only for <code> wrapped inside <pre> (not inline)
  1052. if (!pre || !/pre/i.test(pre.nodeName)) {
  1053. return;
  1054. }
  1055. // Abort if line numbers already exists
  1056. if (code.querySelector('.line-numbers-rows')) {
  1057. return;
  1058. }
  1059. var addLineNumbers = false;
  1060. var lineNumbersRegex = /(?:^|\s)line-numbers(?:\s|$)/;
  1061. for (var element = code; element; element = element.parentNode) {
  1062. if (lineNumbersRegex.test(element.className)) {
  1063. addLineNumbers = true;
  1064. break;
  1065. }
  1066. }
  1067. // only add line numbers if <code> or one of its ancestors has the `line-numbers` class
  1068. if (!addLineNumbers) {
  1069. return;
  1070. }
  1071. // Remove the class 'line-numbers' from the <code>
  1072. code.className = code.className.replace(lineNumbersRegex, ' ');
  1073. // Add the class 'line-numbers' to the <pre>
  1074. if (!lineNumbersRegex.test(pre.className)) {
  1075. pre.className += ' line-numbers';
  1076. }
  1077. var match = env.code.match(NEW_LINE_EXP);
  1078. var linesNum = match ? match.length + 1 : 1;
  1079. var lineNumbersWrapper;
  1080. var lines = new Array(linesNum + 1).join('<span></span>');
  1081. lineNumbersWrapper = document.createElement('span');
  1082. lineNumbersWrapper.setAttribute('aria-hidden', 'true');
  1083. lineNumbersWrapper.className = 'line-numbers-rows';
  1084. lineNumbersWrapper.innerHTML = lines;
  1085. if (pre.hasAttribute('data-start')) {
  1086. pre.style.counterReset = 'linenumber ' + (parseInt(pre.getAttribute('data-start'), 10) - 1);
  1087. }
  1088. env.element.appendChild(lineNumbersWrapper);
  1089. _resizeElement(pre);
  1090. Prism.hooks.run('line-numbers', env);
  1091. });
  1092. Prism.hooks.add('line-numbers', function (env) {
  1093. env.plugins = env.plugins || {};
  1094. env.plugins.lineNumbers = true;
  1095. });
  1096. /**
  1097. * Global exports
  1098. */
  1099. Prism.plugins.lineNumbers = {
  1100. /**
  1101. * Returns the node of the given line number in the given element.
  1102. * @param {Element} element A `<pre>` element with line numbers.
  1103. * @param {Number} number
  1104. * @returns {Element | undefined}
  1105. */
  1106. getLine: function (element, number) {
  1107. if (element.tagName !== 'PRE' || !element.classList.contains(PLUGIN_NAME)) {
  1108. return;
  1109. }
  1110. var lineNumberRows = element.querySelector('.line-numbers-rows');
  1111. var lineNumberStart = parseInt(element.getAttribute('data-start'), 10) || 1;
  1112. var lineNumberEnd = lineNumberStart + (lineNumberRows.children.length - 1);
  1113. if (number < lineNumberStart) {
  1114. number = lineNumberStart;
  1115. }
  1116. if (number > lineNumberEnd) {
  1117. number = lineNumberEnd;
  1118. }
  1119. var lineIndex = number - lineNumberStart;
  1120. return lineNumberRows.children[lineIndex];
  1121. },
  1122. /**
  1123. * Resizes the line numbers of the given element.
  1124. *
  1125. * This function will not add line numbers. It will only resize existing ones.
  1126. * @param {Element} element A `<pre>` element with line numbers.
  1127. * @returns {void}
  1128. */
  1129. resize: function (element) {
  1130. _resizeElement(element);
  1131. }
  1132. };
  1133. }());
  1134. (function() {
  1135. var assign = Object.assign || function (obj1, obj2) {
  1136. for (var name in obj2) {
  1137. if (obj2.hasOwnProperty(name))
  1138. obj1[name] = obj2[name];
  1139. }
  1140. return obj1;
  1141. }
  1142. function NormalizeWhitespace(defaults) {
  1143. this.defaults = assign({}, defaults);
  1144. }
  1145. function toCamelCase(value) {
  1146. return value.replace(/-(\w)/g, function(match, firstChar) {
  1147. return firstChar.toUpperCase();
  1148. });
  1149. }
  1150. function tabLen(str) {
  1151. var res = 0;
  1152. for (var i = 0; i < str.length; ++i) {
  1153. if (str.charCodeAt(i) == '\t'.charCodeAt(0))
  1154. res += 3;
  1155. }
  1156. return str.length + res;
  1157. }
  1158. NormalizeWhitespace.prototype = {
  1159. setDefaults: function (defaults) {
  1160. this.defaults = assign(this.defaults, defaults);
  1161. },
  1162. normalize: function (input, settings) {
  1163. settings = assign(this.defaults, settings);
  1164. for (var name in settings) {
  1165. var methodName = toCamelCase(name);
  1166. if (name !== "normalize" && methodName !== 'setDefaults' &&
  1167. settings[name] && this[methodName]) {
  1168. input = this[methodName].call(this, input, settings[name]);
  1169. }
  1170. }
  1171. return input;
  1172. },
  1173. /*
  1174. * Normalization methods
  1175. */
  1176. leftTrim: function (input) {
  1177. return input.replace(/^\s+/, '');
  1178. },
  1179. rightTrim: function (input) {
  1180. return input.replace(/\s+$/, '');
  1181. },
  1182. tabsToSpaces: function (input, spaces) {
  1183. spaces = spaces|0 || 4;
  1184. return input.replace(/\t/g, new Array(++spaces).join(' '));
  1185. },
  1186. spacesToTabs: function (input, spaces) {
  1187. spaces = spaces|0 || 4;
  1188. return input.replace(RegExp(' {' + spaces + '}', 'g'), '\t');
  1189. },
  1190. removeTrailing: function (input) {
  1191. return input.replace(/\s*?$/gm, '');
  1192. },
  1193. // Support for deprecated plugin remove-initial-line-feed
  1194. removeInitialLineFeed: function (input) {
  1195. return input.replace(/^(?:\r?\n|\r)/, '');
  1196. },
  1197. removeIndent: function (input) {
  1198. var indents = input.match(/^[^\S\n\r]*(?=\S)/gm);
  1199. if (!indents || !indents[0].length)
  1200. return input;
  1201. indents.sort(function(a, b){return a.length - b.length; });
  1202. if (!indents[0].length)
  1203. return input;
  1204. return input.replace(RegExp('^' + indents[0], 'gm'), '');
  1205. },
  1206. indent: function (input, tabs) {
  1207. return input.replace(/^[^\S\n\r]*(?=\S)/gm, new Array(++tabs).join('\t') + '$&');
  1208. },
  1209. breakLines: function (input, characters) {
  1210. characters = (characters === true) ? 80 : characters|0 || 80;
  1211. var lines = input.split('\n');
  1212. for (var i = 0; i < lines.length; ++i) {
  1213. if (tabLen(lines[i]) <= characters)
  1214. continue;
  1215. var line = lines[i].split(/(\s+)/g),
  1216. len = 0;
  1217. for (var j = 0; j < line.length; ++j) {
  1218. var tl = tabLen(line[j]);
  1219. len += tl;
  1220. if (len > characters) {
  1221. line[j] = '\n' + line[j];
  1222. len = tl;
  1223. }
  1224. }
  1225. lines[i] = line.join('');
  1226. }
  1227. return lines.join('\n');
  1228. }
  1229. };
  1230. // Support node modules
  1231. if (typeof module !== 'undefined' && module.exports) {
  1232. module.exports = NormalizeWhitespace;
  1233. }
  1234. // Exit if prism is not loaded
  1235. if (typeof Prism === 'undefined') {
  1236. return;
  1237. }
  1238. Prism.plugins.NormalizeWhitespace = new NormalizeWhitespace({
  1239. 'remove-trailing': true,
  1240. 'remove-indent': true,
  1241. 'left-trim': true,
  1242. 'right-trim': true,
  1243. /*'break-lines': 80,
  1244. 'indent': 2,*/
  1245. 'remove-initial-line-feed': true,
  1246. /*'tabs-to-spaces': 4,
  1247. 'spaces-to-tabs': 4*/
  1248. });
  1249. Prism.hooks.add('before-sanity-check', function (env) {
  1250. var Normalizer = Prism.plugins.NormalizeWhitespace;
  1251. // Check settings
  1252. if (env.settings && env.settings['whitespace-normalization'] === false) {
  1253. return;
  1254. }
  1255. // Simple mode if there is no env.element
  1256. if ((!env.element || !env.element.parentNode) && env.code) {
  1257. env.code = Normalizer.normalize(env.code, env.settings);
  1258. return;
  1259. }
  1260. // Normal mode
  1261. var pre = env.element.parentNode;
  1262. var clsReg = /(?:^|\s)no-whitespace-normalization(?:\s|$)/;
  1263. if (!env.code || !pre || pre.nodeName.toLowerCase() !== 'pre' ||
  1264. clsReg.test(pre.className) || clsReg.test(env.element.className))
  1265. return;
  1266. var children = pre.childNodes,
  1267. before = '',
  1268. after = '',
  1269. codeFound = false;
  1270. // Move surrounding whitespace from the <pre> tag into the <code> tag
  1271. for (var i = 0; i < children.length; ++i) {
  1272. var node = children[i];
  1273. if (node == env.element) {
  1274. codeFound = true;
  1275. } else if (node.nodeName === "#text") {
  1276. if (codeFound) {
  1277. after += node.nodeValue;
  1278. } else {
  1279. before += node.nodeValue;
  1280. }
  1281. pre.removeChild(node);
  1282. --i;
  1283. }
  1284. }
  1285. if (!env.element.children.length || !Prism.plugins.KeepMarkup) {
  1286. env.code = before + env.code + after;
  1287. env.code = Normalizer.normalize(env.code, env.settings);
  1288. } else {
  1289. // Preserve markup for keep-markup plugin
  1290. var html = before + env.element.innerHTML + after;
  1291. env.element.innerHTML = Normalizer.normalize(html, env.settings);
  1292. env.code = env.element.textContent;
  1293. }
  1294. });
  1295. }());