prism-previewers.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. (function () {
  2. if (typeof Prism === 'undefined' || typeof document === 'undefined' || !Function.prototype.bind) {
  3. return;
  4. }
  5. var previewers = {
  6. // gradient must be defined before color and angle
  7. 'gradient': {
  8. create: (function () {
  9. // Stores already processed gradients so that we don't
  10. // make the conversion every time the previewer is shown
  11. var cache = {};
  12. /**
  13. * Returns a W3C-valid linear gradient
  14. *
  15. * @param {string} prefix Vendor prefix if any ("-moz-", "-webkit-", etc.)
  16. * @param {string} func Gradient function name ("linear-gradient")
  17. * @param {string[]} values Array of the gradient function parameters (["0deg", "red 0%", "blue 100%"])
  18. */
  19. var convertToW3CLinearGradient = function (prefix, func, values) {
  20. // Default value for angle
  21. var angle = '180deg';
  22. if (/^(?:-?(?:\d+(?:\.\d+)?|\.\d+)(?:deg|rad)|to\b|top|right|bottom|left)/.test(values[0])) {
  23. angle = values.shift();
  24. if (angle.indexOf('to ') < 0) {
  25. // Angle uses old keywords
  26. // W3C syntax uses "to" + opposite keywords
  27. if (angle.indexOf('top') >= 0) {
  28. if (angle.indexOf('left') >= 0) {
  29. angle = 'to bottom right';
  30. } else if (angle.indexOf('right') >= 0) {
  31. angle = 'to bottom left';
  32. } else {
  33. angle = 'to bottom';
  34. }
  35. } else if (angle.indexOf('bottom') >= 0) {
  36. if (angle.indexOf('left') >= 0) {
  37. angle = 'to top right';
  38. } else if (angle.indexOf('right') >= 0) {
  39. angle = 'to top left';
  40. } else {
  41. angle = 'to top';
  42. }
  43. } else if (angle.indexOf('left') >= 0) {
  44. angle = 'to right';
  45. } else if (angle.indexOf('right') >= 0) {
  46. angle = 'to left';
  47. } else if (prefix) {
  48. // Angle is shifted by 90deg in prefixed gradients
  49. if (angle.indexOf('deg') >= 0) {
  50. angle = (90 - parseFloat(angle)) + 'deg';
  51. } else if (angle.indexOf('rad') >= 0) {
  52. angle = (Math.PI / 2 - parseFloat(angle)) + 'rad';
  53. }
  54. }
  55. }
  56. }
  57. return func + '(' + angle + ',' + values.join(',') + ')';
  58. };
  59. /**
  60. * Returns a W3C-valid radial gradient
  61. *
  62. * @param {string} prefix Vendor prefix if any ("-moz-", "-webkit-", etc.)
  63. * @param {string} func Gradient function name ("linear-gradient")
  64. * @param {string[]} values Array of the gradient function parameters (["0deg", "red 0%", "blue 100%"])
  65. */
  66. var convertToW3CRadialGradient = function (prefix, func, values) {
  67. if (values[0].indexOf('at') < 0) {
  68. // Looks like old syntax
  69. // Default values
  70. var position = 'center';
  71. var shape = 'ellipse';
  72. var size = 'farthest-corner';
  73. if (/\b(?:bottom|center|left|right|top)\b|^\d+/.test(values[0])) {
  74. // Found a position
  75. // Remove angle value, if any
  76. position = values.shift().replace(/\s*-?\d+(?:deg|rad)\s*/, '');
  77. }
  78. if (/\b(?:circle|closest|contain|cover|ellipse|farthest)\b/.test(values[0])) {
  79. // Found a shape and/or size
  80. var shapeSizeParts = values.shift().split(/\s+/);
  81. if (shapeSizeParts[0] && (shapeSizeParts[0] === 'circle' || shapeSizeParts[0] === 'ellipse')) {
  82. shape = shapeSizeParts.shift();
  83. }
  84. if (shapeSizeParts[0]) {
  85. size = shapeSizeParts.shift();
  86. }
  87. // Old keywords are converted to their synonyms
  88. if (size === 'cover') {
  89. size = 'farthest-corner';
  90. } else if (size === 'contain') {
  91. size = 'clothest-side';
  92. }
  93. }
  94. return func + '(' + shape + ' ' + size + ' at ' + position + ',' + values.join(',') + ')';
  95. }
  96. return func + '(' + values.join(',') + ')';
  97. };
  98. /**
  99. * Converts a gradient to a W3C-valid one
  100. * Does not support old webkit syntax (-webkit-gradient(linear...) and -webkit-gradient(radial...))
  101. *
  102. * @param {string} gradient The CSS gradient
  103. */
  104. var convertToW3CGradient = function (gradient) {
  105. if (cache[gradient]) {
  106. return cache[gradient];
  107. }
  108. var parts = gradient.match(/^(\b|\B-[a-z]{1,10}-)((?:repeating-)?(?:linear|radial)-gradient)/);
  109. // "", "-moz-", etc.
  110. var prefix = parts && parts[1];
  111. // "linear-gradient", "radial-gradient", etc.
  112. var func = parts && parts[2];
  113. var values = gradient.replace(/^(?:\b|\B-[a-z]{1,10}-)(?:repeating-)?(?:linear|radial)-gradient\(|\)$/g, '').split(/\s*,\s*/);
  114. if (func.indexOf('linear') >= 0) {
  115. return cache[gradient] = convertToW3CLinearGradient(prefix, func, values);
  116. } else if (func.indexOf('radial') >= 0) {
  117. return cache[gradient] = convertToW3CRadialGradient(prefix, func, values);
  118. }
  119. return cache[gradient] = func + '(' + values.join(',') + ')';
  120. };
  121. return function () {
  122. new Prism.plugins.Previewer('gradient', function (value) {
  123. this.firstChild.style.backgroundImage = '';
  124. this.firstChild.style.backgroundImage = convertToW3CGradient(value);
  125. return !!this.firstChild.style.backgroundImage;
  126. }, '*', function () {
  127. this._elt.innerHTML = '<div></div>';
  128. });
  129. };
  130. }()),
  131. tokens: {
  132. 'gradient': {
  133. pattern: /(?:\b|\B-[a-z]{1,10}-)(?:repeating-)?(?:linear|radial)-gradient\((?:(?:hsl|rgb)a?\(.+?\)|[^\)])+\)/gi,
  134. inside: {
  135. 'function': /[\w-]+(?=\()/,
  136. 'punctuation': /[(),]/
  137. }
  138. }
  139. },
  140. languages: {
  141. 'css': true,
  142. 'less': true,
  143. 'sass': [
  144. {
  145. lang: 'sass',
  146. before: 'punctuation',
  147. inside: 'inside',
  148. root: Prism.languages.sass && Prism.languages.sass['variable-line']
  149. },
  150. {
  151. lang: 'sass',
  152. before: 'punctuation',
  153. inside: 'inside',
  154. root: Prism.languages.sass && Prism.languages.sass['property-line']
  155. }
  156. ],
  157. 'scss': true,
  158. 'stylus': [
  159. {
  160. lang: 'stylus',
  161. before: 'func',
  162. inside: 'rest',
  163. root: Prism.languages.stylus && Prism.languages.stylus['property-declaration'].inside
  164. },
  165. {
  166. lang: 'stylus',
  167. before: 'func',
  168. inside: 'rest',
  169. root: Prism.languages.stylus && Prism.languages.stylus['variable-declaration'].inside
  170. }
  171. ]
  172. }
  173. },
  174. 'angle': {
  175. create: function () {
  176. new Prism.plugins.Previewer('angle', function (value) {
  177. var num = parseFloat(value);
  178. var unit = value.match(/[a-z]+$/i);
  179. var max; var percentage;
  180. if (!num || !unit) {
  181. return false;
  182. }
  183. unit = unit[0];
  184. switch (unit) {
  185. case 'deg':
  186. max = 360;
  187. break;
  188. case 'grad':
  189. max = 400;
  190. break;
  191. case 'rad':
  192. max = 2 * Math.PI;
  193. break;
  194. case 'turn':
  195. max = 1;
  196. }
  197. percentage = 100 * num / max;
  198. percentage %= 100;
  199. this[(num < 0 ? 'set' : 'remove') + 'Attribute']('data-negative', '');
  200. this.querySelector('circle').style.strokeDasharray = Math.abs(percentage) + ',500';
  201. return true;
  202. }, '*', function () {
  203. this._elt.innerHTML = '<svg viewBox="0 0 64 64">' +
  204. '<circle r="16" cy="32" cx="32"></circle>' +
  205. '</svg>';
  206. });
  207. },
  208. tokens: {
  209. 'angle': /(?:\b|\B-|(?=\B\.))(?:\d+(?:\.\d+)?|\.\d+)(?:deg|g?rad|turn)\b/i
  210. },
  211. languages: {
  212. 'css': true,
  213. 'less': true,
  214. 'markup': {
  215. lang: 'markup',
  216. before: 'punctuation',
  217. inside: 'inside',
  218. root: Prism.languages.markup && Prism.languages.markup['tag'].inside['attr-value']
  219. },
  220. 'sass': [
  221. {
  222. lang: 'sass',
  223. inside: 'inside',
  224. root: Prism.languages.sass && Prism.languages.sass['property-line']
  225. },
  226. {
  227. lang: 'sass',
  228. before: 'operator',
  229. inside: 'inside',
  230. root: Prism.languages.sass && Prism.languages.sass['variable-line']
  231. }
  232. ],
  233. 'scss': true,
  234. 'stylus': [
  235. {
  236. lang: 'stylus',
  237. before: 'func',
  238. inside: 'rest',
  239. root: Prism.languages.stylus && Prism.languages.stylus['property-declaration'].inside
  240. },
  241. {
  242. lang: 'stylus',
  243. before: 'func',
  244. inside: 'rest',
  245. root: Prism.languages.stylus && Prism.languages.stylus['variable-declaration'].inside
  246. }
  247. ]
  248. }
  249. },
  250. 'color': {
  251. create: function () {
  252. new Prism.plugins.Previewer('color', function (value) {
  253. this.style.backgroundColor = '';
  254. this.style.backgroundColor = value;
  255. return !!this.style.backgroundColor;
  256. });
  257. },
  258. tokens: {
  259. 'color': [Prism.languages.css['hexcode']].concat(Prism.languages.css['color'])
  260. },
  261. languages: {
  262. // CSS extras is required, so css and scss are not necessary
  263. 'css': false,
  264. 'less': true,
  265. 'markup': {
  266. lang: 'markup',
  267. before: 'punctuation',
  268. inside: 'inside',
  269. root: Prism.languages.markup && Prism.languages.markup['tag'].inside['attr-value']
  270. },
  271. 'sass': [
  272. {
  273. lang: 'sass',
  274. before: 'punctuation',
  275. inside: 'inside',
  276. root: Prism.languages.sass && Prism.languages.sass['variable-line']
  277. },
  278. {
  279. lang: 'sass',
  280. inside: 'inside',
  281. root: Prism.languages.sass && Prism.languages.sass['property-line']
  282. }
  283. ],
  284. 'scss': false,
  285. 'stylus': [
  286. {
  287. lang: 'stylus',
  288. before: 'hexcode',
  289. inside: 'rest',
  290. root: Prism.languages.stylus && Prism.languages.stylus['property-declaration'].inside
  291. },
  292. {
  293. lang: 'stylus',
  294. before: 'hexcode',
  295. inside: 'rest',
  296. root: Prism.languages.stylus && Prism.languages.stylus['variable-declaration'].inside
  297. }
  298. ]
  299. }
  300. },
  301. 'easing': {
  302. create: function () {
  303. new Prism.plugins.Previewer('easing', function (value) {
  304. value = {
  305. 'linear': '0,0,1,1',
  306. 'ease': '.25,.1,.25,1',
  307. 'ease-in': '.42,0,1,1',
  308. 'ease-out': '0,0,.58,1',
  309. 'ease-in-out': '.42,0,.58,1'
  310. }[value] || value;
  311. var p = value.match(/-?(?:\d+(?:\.\d+)?|\.\d+)/g);
  312. if (p.length === 4) {
  313. p = p.map(function (p, i) { return (i % 2 ? 1 - p : p) * 100; });
  314. this.querySelector('path').setAttribute('d', 'M0,100 C' + p[0] + ',' + p[1] + ', ' + p[2] + ',' + p[3] + ', 100,0');
  315. var lines = this.querySelectorAll('line');
  316. lines[0].setAttribute('x2', p[0]);
  317. lines[0].setAttribute('y2', p[1]);
  318. lines[1].setAttribute('x2', p[2]);
  319. lines[1].setAttribute('y2', p[3]);
  320. return true;
  321. }
  322. return false;
  323. }, '*', function () {
  324. this._elt.innerHTML = '<svg viewBox="-20 -20 140 140" width="100" height="100">' +
  325. '<defs>' +
  326. '<marker id="prism-previewer-easing-marker" viewBox="0 0 4 4" refX="2" refY="2" markerUnits="strokeWidth">' +
  327. '<circle cx="2" cy="2" r="1.5" />' +
  328. '</marker>' +
  329. '</defs>' +
  330. '<path d="M0,100 C20,50, 40,30, 100,0" />' +
  331. '<line x1="0" y1="100" x2="20" y2="50" marker-start="url(#prism-previewer-easing-marker)" marker-end="url(#prism-previewer-easing-marker)" />' +
  332. '<line x1="100" y1="0" x2="40" y2="30" marker-start="url(#prism-previewer-easing-marker)" marker-end="url(#prism-previewer-easing-marker)" />' +
  333. '</svg>';
  334. });
  335. },
  336. tokens: {
  337. 'easing': {
  338. pattern: /\bcubic-bezier\((?:-?(?:\d+(?:\.\d+)?|\.\d+),\s*){3}-?(?:\d+(?:\.\d+)?|\.\d+)\)\B|\b(?:ease(?:-in)?(?:-out)?|linear)(?=\s|[;}]|$)/i,
  339. inside: {
  340. 'function': /[\w-]+(?=\()/,
  341. 'punctuation': /[(),]/
  342. }
  343. }
  344. },
  345. languages: {
  346. 'css': true,
  347. 'less': true,
  348. 'sass': [
  349. {
  350. lang: 'sass',
  351. inside: 'inside',
  352. before: 'punctuation',
  353. root: Prism.languages.sass && Prism.languages.sass['variable-line']
  354. },
  355. {
  356. lang: 'sass',
  357. inside: 'inside',
  358. root: Prism.languages.sass && Prism.languages.sass['property-line']
  359. }
  360. ],
  361. 'scss': true,
  362. 'stylus': [
  363. {
  364. lang: 'stylus',
  365. before: 'hexcode',
  366. inside: 'rest',
  367. root: Prism.languages.stylus && Prism.languages.stylus['property-declaration'].inside
  368. },
  369. {
  370. lang: 'stylus',
  371. before: 'hexcode',
  372. inside: 'rest',
  373. root: Prism.languages.stylus && Prism.languages.stylus['variable-declaration'].inside
  374. }
  375. ]
  376. }
  377. },
  378. 'time': {
  379. create: function () {
  380. new Prism.plugins.Previewer('time', function (value) {
  381. var num = parseFloat(value);
  382. var unit = value.match(/[a-z]+$/i);
  383. if (!num || !unit) {
  384. return false;
  385. }
  386. unit = unit[0];
  387. this.querySelector('circle').style.animationDuration = 2 * num + unit;
  388. return true;
  389. }, '*', function () {
  390. this._elt.innerHTML = '<svg viewBox="0 0 64 64">' +
  391. '<circle r="16" cy="32" cx="32"></circle>' +
  392. '</svg>';
  393. });
  394. },
  395. tokens: {
  396. 'time': /(?:\b|\B-|(?=\B\.))(?:\d+(?:\.\d+)?|\.\d+)m?s\b/i
  397. },
  398. languages: {
  399. 'css': true,
  400. 'less': true,
  401. 'markup': {
  402. lang: 'markup',
  403. before: 'punctuation',
  404. inside: 'inside',
  405. root: Prism.languages.markup && Prism.languages.markup['tag'].inside['attr-value']
  406. },
  407. 'sass': [
  408. {
  409. lang: 'sass',
  410. inside: 'inside',
  411. root: Prism.languages.sass && Prism.languages.sass['property-line']
  412. },
  413. {
  414. lang: 'sass',
  415. before: 'operator',
  416. inside: 'inside',
  417. root: Prism.languages.sass && Prism.languages.sass['variable-line']
  418. }
  419. ],
  420. 'scss': true,
  421. 'stylus': [
  422. {
  423. lang: 'stylus',
  424. before: 'hexcode',
  425. inside: 'rest',
  426. root: Prism.languages.stylus && Prism.languages.stylus['property-declaration'].inside
  427. },
  428. {
  429. lang: 'stylus',
  430. before: 'hexcode',
  431. inside: 'rest',
  432. root: Prism.languages.stylus && Prism.languages.stylus['variable-declaration'].inside
  433. }
  434. ]
  435. }
  436. }
  437. };
  438. /**
  439. * Returns the absolute X, Y offsets for an element
  440. *
  441. * @param {HTMLElement} element
  442. * @returns {{top: number, right: number, bottom: number, left: number, width: number, height: number}}
  443. */
  444. var getOffset = function (element) {
  445. var elementBounds = element.getBoundingClientRect();
  446. var left = elementBounds.left;
  447. var top = elementBounds.top;
  448. var documentBounds = document.documentElement.getBoundingClientRect();
  449. left -= documentBounds.left;
  450. top -= documentBounds.top;
  451. return {
  452. top: top,
  453. right: innerWidth - left - elementBounds.width,
  454. bottom: innerHeight - top - elementBounds.height,
  455. left: left,
  456. width: elementBounds.width,
  457. height: elementBounds.height
  458. };
  459. };
  460. var TOKEN_CLASS = 'token';
  461. var ACTIVE_CLASS = 'active';
  462. var FLIPPED_CLASS = 'flipped';
  463. /**
  464. * Previewer constructor
  465. *
  466. * @param {string} type Unique previewer type
  467. * @param {Function} updater Function that will be called on mouseover.
  468. * @param {string[]|string} [supportedLanguages] Aliases of the languages this previewer must be enabled for. Defaults to "*", all languages.
  469. * @param {Function} [initializer] Function that will be called on initialization.
  470. * @class
  471. */
  472. var Previewer = function (type, updater, supportedLanguages, initializer) {
  473. this._elt = null;
  474. this._type = type;
  475. this._token = null;
  476. this.updater = updater;
  477. this._mouseout = this.mouseout.bind(this);
  478. this.initializer = initializer;
  479. var self = this;
  480. if (!supportedLanguages) {
  481. supportedLanguages = ['*'];
  482. }
  483. if (!Array.isArray(supportedLanguages)) {
  484. supportedLanguages = [supportedLanguages];
  485. }
  486. supportedLanguages.forEach(function (lang) {
  487. if (typeof lang !== 'string') {
  488. lang = lang.lang;
  489. }
  490. if (!Previewer.byLanguages[lang]) {
  491. Previewer.byLanguages[lang] = [];
  492. }
  493. if (Previewer.byLanguages[lang].indexOf(self) < 0) {
  494. Previewer.byLanguages[lang].push(self);
  495. }
  496. });
  497. Previewer.byType[type] = this;
  498. };
  499. /**
  500. * Creates the HTML element for the previewer.
  501. */
  502. Previewer.prototype.init = function () {
  503. if (this._elt) {
  504. return;
  505. }
  506. this._elt = document.createElement('div');
  507. this._elt.className = 'prism-previewer prism-previewer-' + this._type;
  508. document.body.appendChild(this._elt);
  509. if (this.initializer) {
  510. this.initializer();
  511. }
  512. };
  513. /**
  514. * @param {Element} token
  515. * @returns {boolean}
  516. */
  517. Previewer.prototype.isDisabled = function (token) {
  518. do {
  519. if (token.hasAttribute && token.hasAttribute('data-previewers')) {
  520. var previewers = token.getAttribute('data-previewers');
  521. return (previewers || '').split(/\s+/).indexOf(this._type) === -1;
  522. }
  523. } while ((token = token.parentNode));
  524. return false;
  525. };
  526. /**
  527. * Checks the class name of each hovered element
  528. *
  529. * @param {Element} token
  530. */
  531. Previewer.prototype.check = function (token) {
  532. if (token.classList.contains(TOKEN_CLASS) && this.isDisabled(token)) {
  533. return;
  534. }
  535. do {
  536. if (token.classList && token.classList.contains(TOKEN_CLASS) && token.classList.contains(this._type)) {
  537. break;
  538. }
  539. } while ((token = token.parentNode));
  540. if (token && token !== this._token) {
  541. this._token = token;
  542. this.show();
  543. }
  544. };
  545. /**
  546. * Called on mouseout
  547. */
  548. Previewer.prototype.mouseout = function () {
  549. this._token.removeEventListener('mouseout', this._mouseout, false);
  550. this._token = null;
  551. this.hide();
  552. };
  553. /**
  554. * Shows the previewer positioned properly for the current token.
  555. */
  556. Previewer.prototype.show = function () {
  557. if (!this._elt) {
  558. this.init();
  559. }
  560. if (!this._token) {
  561. return;
  562. }
  563. if (this.updater.call(this._elt, this._token.textContent)) {
  564. this._token.addEventListener('mouseout', this._mouseout, false);
  565. var offset = getOffset(this._token);
  566. this._elt.classList.add(ACTIVE_CLASS);
  567. if (offset.top - this._elt.offsetHeight > 0) {
  568. this._elt.classList.remove(FLIPPED_CLASS);
  569. this._elt.style.top = offset.top + 'px';
  570. this._elt.style.bottom = '';
  571. } else {
  572. this._elt.classList.add(FLIPPED_CLASS);
  573. this._elt.style.bottom = offset.bottom + 'px';
  574. this._elt.style.top = '';
  575. }
  576. this._elt.style.left = offset.left + Math.min(200, offset.width / 2) + 'px';
  577. } else {
  578. this.hide();
  579. }
  580. };
  581. /**
  582. * Hides the previewer.
  583. */
  584. Previewer.prototype.hide = function () {
  585. this._elt.classList.remove(ACTIVE_CLASS);
  586. };
  587. /**
  588. * Map of all registered previewers by language
  589. *
  590. * @type {{}}
  591. */
  592. Previewer.byLanguages = {};
  593. /**
  594. * Map of all registered previewers by type
  595. *
  596. * @type {{}}
  597. */
  598. Previewer.byType = {};
  599. /**
  600. * Initializes the mouseover event on the code block.
  601. *
  602. * @param {HTMLElement} elt The code block (env.element)
  603. * @param {string} lang The language (env.language)
  604. */
  605. Previewer.initEvents = function (elt, lang) {
  606. var previewers = [];
  607. if (Previewer.byLanguages[lang]) {
  608. previewers = previewers.concat(Previewer.byLanguages[lang]);
  609. }
  610. if (Previewer.byLanguages['*']) {
  611. previewers = previewers.concat(Previewer.byLanguages['*']);
  612. }
  613. elt.addEventListener('mouseover', function (e) {
  614. var target = e.target;
  615. previewers.forEach(function (previewer) {
  616. previewer.check(target);
  617. });
  618. }, false);
  619. };
  620. Prism.plugins.Previewer = Previewer;
  621. Prism.hooks.add('before-highlight', function (env) {
  622. for (var previewer in previewers) {
  623. var languages = previewers[previewer].languages;
  624. if (env.language && languages[env.language] && !languages[env.language].initialized) {
  625. var lang = languages[env.language];
  626. if (!Array.isArray(lang)) {
  627. lang = [lang];
  628. }
  629. lang.forEach(function (lang) {
  630. var before; var inside; var root; var skip;
  631. if (lang === true) {
  632. before = 'important';
  633. inside = env.language;
  634. lang = env.language;
  635. } else {
  636. before = lang.before || 'important';
  637. inside = lang.inside || lang.lang;
  638. root = lang.root || Prism.languages;
  639. skip = lang.skip;
  640. lang = env.language;
  641. }
  642. if (!skip && Prism.languages[lang]) {
  643. Prism.languages.insertBefore(inside, before, previewers[previewer].tokens, root);
  644. env.grammar = Prism.languages[lang];
  645. languages[env.language] = { initialized: true };
  646. }
  647. });
  648. }
  649. }
  650. });
  651. // Initialize the previewers only when needed
  652. Prism.hooks.add('after-highlight', function (env) {
  653. if (Previewer.byLanguages['*'] || Previewer.byLanguages[env.language]) {
  654. Previewer.initEvents(env.element, env.language);
  655. }
  656. });
  657. for (var previewer in previewers) {
  658. previewers[previewer].create();
  659. }
  660. }());