plugin.js 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.0.1 (2019-02-21)
  8. */
  9. (function () {
  10. var codesample = (function (domGlobals) {
  11. 'use strict';
  12. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  13. var global$1 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
  14. var window = {};
  15. var global$2 = window;
  16. var _self = typeof window !== 'undefined' ? window : typeof WorkerGlobalScope !== 'undefined' && domGlobals.self instanceof WorkerGlobalScope ? domGlobals.self : {};
  17. var Prism = function () {
  18. var lang = /\blang(?:uage)?-(?!\*)(\w+)\b/i;
  19. var _ = _self.Prism = {
  20. util: {
  21. encode: function (tokens) {
  22. if (tokens instanceof Token) {
  23. return new Token(tokens.type, _.util.encode(tokens.content), tokens.alias);
  24. } else if (_.util.type(tokens) === 'Array') {
  25. return tokens.map(_.util.encode);
  26. } else {
  27. return tokens.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\u00a0/g, ' ');
  28. }
  29. },
  30. type: function (o) {
  31. return Object.prototype.toString.call(o).match(/\[object (\w+)\]/)[1];
  32. },
  33. clone: function (o) {
  34. var type = _.util.type(o);
  35. switch (type) {
  36. case 'Object':
  37. var clone = {};
  38. for (var key in o) {
  39. if (o.hasOwnProperty(key)) {
  40. clone[key] = _.util.clone(o[key]);
  41. }
  42. }
  43. return clone;
  44. case 'Array':
  45. return o.map && o.map(function (v) {
  46. return _.util.clone(v);
  47. });
  48. }
  49. return o;
  50. }
  51. },
  52. languages: {
  53. extend: function (id, redef) {
  54. var lang = _.util.clone(_.languages[id]);
  55. for (var key in redef) {
  56. lang[key] = redef[key];
  57. }
  58. return lang;
  59. },
  60. insertBefore: function (inside, before, insert, root) {
  61. root = root || _.languages;
  62. var grammar = root[inside];
  63. if (arguments.length === 2) {
  64. insert = arguments[1];
  65. for (var newToken in insert) {
  66. if (insert.hasOwnProperty(newToken)) {
  67. grammar[newToken] = insert[newToken];
  68. }
  69. }
  70. return grammar;
  71. }
  72. var ret = {};
  73. for (var token in grammar) {
  74. if (grammar.hasOwnProperty(token)) {
  75. if (token === before) {
  76. for (var newToken in insert) {
  77. if (insert.hasOwnProperty(newToken)) {
  78. ret[newToken] = insert[newToken];
  79. }
  80. }
  81. }
  82. ret[token] = grammar[token];
  83. }
  84. }
  85. _.languages.DFS(_.languages, function (key, value) {
  86. if (value === root[inside] && key !== inside) {
  87. this[key] = ret;
  88. }
  89. });
  90. return root[inside] = ret;
  91. },
  92. DFS: function (o, callback, type) {
  93. for (var i in o) {
  94. if (o.hasOwnProperty(i)) {
  95. callback.call(o, i, o[i], type || i);
  96. if (_.util.type(o[i]) === 'Object') {
  97. _.languages.DFS(o[i], callback);
  98. } else if (_.util.type(o[i]) === 'Array') {
  99. _.languages.DFS(o[i], callback, i);
  100. }
  101. }
  102. }
  103. }
  104. },
  105. plugins: {},
  106. highlightAll: function (async, callback) {
  107. var elements = domGlobals.document.querySelectorAll('code[class*="language-"], [class*="language-"] code, code[class*="lang-"], [class*="lang-"] code');
  108. for (var i = 0, element = void 0; element = elements[i++];) {
  109. _.highlightElement(element, async === true, callback);
  110. }
  111. },
  112. highlightElement: function (element, async, callback) {
  113. var language, grammar, parent = element;
  114. while (parent && !lang.test(parent.className)) {
  115. parent = parent.parentNode;
  116. }
  117. if (parent) {
  118. language = (parent.className.match(lang) || [
  119. ,
  120. ''
  121. ])[1];
  122. grammar = _.languages[language];
  123. }
  124. element.className = element.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
  125. parent = element.parentNode;
  126. if (/pre/i.test(parent.nodeName)) {
  127. parent.className = parent.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
  128. }
  129. var code = element.textContent;
  130. var env = {
  131. element: element,
  132. language: language,
  133. grammar: grammar,
  134. code: code
  135. };
  136. if (!code || !grammar) {
  137. _.hooks.run('complete', env);
  138. return;
  139. }
  140. _.hooks.run('before-highlight', env);
  141. if (async && _self.Worker) {
  142. var worker = new domGlobals.Worker(_.filename);
  143. worker.onmessage = function (evt) {
  144. env.highlightedCode = evt.data;
  145. _.hooks.run('before-insert', env);
  146. env.element.innerHTML = env.highlightedCode;
  147. if (callback) {
  148. callback.call(env.element);
  149. }
  150. _.hooks.run('after-highlight', env);
  151. _.hooks.run('complete', env);
  152. };
  153. worker.postMessage(JSON.stringify({
  154. language: env.language,
  155. code: env.code,
  156. immediateClose: true
  157. }));
  158. } else {
  159. env.highlightedCode = _.highlight(env.code, env.grammar, env.language);
  160. _.hooks.run('before-insert', env);
  161. env.element.innerHTML = env.highlightedCode;
  162. if (callback) {
  163. callback.call(element);
  164. }
  165. _.hooks.run('after-highlight', env);
  166. _.hooks.run('complete', env);
  167. }
  168. },
  169. highlight: function (text, grammar, language) {
  170. var tokens = _.tokenize(text, grammar);
  171. return Token.stringify(_.util.encode(tokens), language);
  172. },
  173. tokenize: function (text, grammar, language) {
  174. var Token = _.Token;
  175. var strarr = [text];
  176. var rest = grammar.rest;
  177. if (rest) {
  178. for (var token in rest) {
  179. grammar[token] = rest[token];
  180. }
  181. delete grammar.rest;
  182. }
  183. tokenloop:
  184. for (var token in grammar) {
  185. if (!grammar.hasOwnProperty(token) || !grammar[token]) {
  186. continue;
  187. }
  188. var patterns = grammar[token];
  189. patterns = _.util.type(patterns) === 'Array' ? patterns : [patterns];
  190. for (var j = 0; j < patterns.length; ++j) {
  191. var pattern = patterns[j];
  192. var inside = pattern.inside;
  193. var lookbehind = !!pattern.lookbehind;
  194. var lookbehindLength = 0;
  195. var alias = pattern.alias;
  196. pattern = pattern.pattern || pattern;
  197. for (var i = 0; i < strarr.length; i++) {
  198. var str = strarr[i];
  199. if (strarr.length > text.length) {
  200. break tokenloop;
  201. }
  202. if (str instanceof Token) {
  203. continue;
  204. }
  205. pattern.lastIndex = 0;
  206. var match = pattern.exec(str);
  207. if (match) {
  208. if (lookbehind) {
  209. lookbehindLength = match[1].length;
  210. }
  211. var from = match.index - 1 + lookbehindLength;
  212. match = match[0].slice(lookbehindLength);
  213. var len = match.length, to = from + len, before = str.slice(0, from + 1), after = str.slice(to + 1);
  214. var args = [
  215. i,
  216. 1
  217. ];
  218. if (before) {
  219. args.push(before);
  220. }
  221. var wrapped = new Token(token, inside ? _.tokenize(match, inside) : match, alias);
  222. args.push(wrapped);
  223. if (after) {
  224. args.push(after);
  225. }
  226. Array.prototype.splice.apply(strarr, args);
  227. }
  228. }
  229. }
  230. }
  231. return strarr;
  232. },
  233. hooks: {
  234. all: {},
  235. add: function (name, callback) {
  236. var hooks = _.hooks.all;
  237. hooks[name] = hooks[name] || [];
  238. hooks[name].push(callback);
  239. },
  240. run: function (name, env) {
  241. var callbacks = _.hooks.all[name];
  242. if (!callbacks || !callbacks.length) {
  243. return;
  244. }
  245. for (var i = 0, callback = void 0; callback = callbacks[i++];) {
  246. callback(env);
  247. }
  248. }
  249. }
  250. };
  251. var Token = _.Token = function (type, content, alias) {
  252. this.type = type;
  253. this.content = content;
  254. this.alias = alias;
  255. };
  256. Token.stringify = function (o, language, parent) {
  257. if (typeof o === 'string') {
  258. return o;
  259. }
  260. if (_.util.type(o) === 'Array') {
  261. return o.map(function (element) {
  262. return Token.stringify(element, language, o);
  263. }).join('');
  264. }
  265. var env = {
  266. type: o.type,
  267. content: Token.stringify(o.content, language, parent),
  268. tag: 'span',
  269. classes: [
  270. 'token',
  271. o.type
  272. ],
  273. attributes: {},
  274. language: language,
  275. parent: parent
  276. };
  277. if (env.type === 'comment') {
  278. env.attributes.spellcheck = 'true';
  279. }
  280. if (o.alias) {
  281. var aliases = _.util.type(o.alias) === 'Array' ? o.alias : [o.alias];
  282. Array.prototype.push.apply(env.classes, aliases);
  283. }
  284. _.hooks.run('wrap', env);
  285. var attributes = '';
  286. for (var name in env.attributes) {
  287. attributes += (attributes ? ' ' : '') + name + '="' + (env.attributes[name] || '') + '"';
  288. }
  289. return '<' + env.tag + ' class="' + env.classes.join(' ') + '" ' + attributes + '>' + env.content + '</' + env.tag + '>';
  290. };
  291. if (!_self.document) {
  292. if (!_self.addEventListener) {
  293. return _self.Prism;
  294. }
  295. _self.addEventListener('message', function (evt) {
  296. var message = JSON.parse(evt.data), lang = message.language, code = message.code, immediateClose = message.immediateClose;
  297. _self.postMessage(_.highlight(code, _.languages[lang], lang));
  298. if (immediateClose) {
  299. _self.close();
  300. }
  301. }, false);
  302. return _self.Prism;
  303. }
  304. }();
  305. if (typeof global$2 !== 'undefined') {
  306. global$2.Prism = Prism;
  307. }
  308. Prism.languages.markup = {
  309. comment: /<!--[\w\W]*?-->/,
  310. prolog: /<\?[\w\W]+?\?>/,
  311. doctype: /<!DOCTYPE[\w\W]+?>/,
  312. cdata: /<!\[CDATA\[[\w\W]*?]]>/i,
  313. tag: {
  314. pattern: /<\/?[^\s>\/=.]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\\1|\\?(?!\1)[\w\W])*\1|[^\s'">=]+))?)*\s*\/?>/i,
  315. inside: {
  316. 'tag': {
  317. pattern: /^<\/?[^\s>\/]+/i,
  318. inside: {
  319. punctuation: /^<\/?/,
  320. namespace: /^[^\s>\/:]+:/
  321. }
  322. },
  323. 'attr-value': {
  324. pattern: /=(?:('|")[\w\W]*?(\1)|[^\s>]+)/i,
  325. inside: { punctuation: /[=>"']/ }
  326. },
  327. 'punctuation': /\/?>/,
  328. 'attr-name': {
  329. pattern: /[^\s>\/]+/,
  330. inside: { namespace: /^[^\s>\/:]+:/ }
  331. }
  332. }
  333. },
  334. entity: /&#?[\da-z]{1,8};/i
  335. };
  336. Prism.hooks.add('wrap', function (env) {
  337. if (env.type === 'entity') {
  338. env.attributes.title = env.content.replace(/&amp;/, '&');
  339. }
  340. });
  341. Prism.languages.xml = Prism.languages.markup;
  342. Prism.languages.html = Prism.languages.markup;
  343. Prism.languages.mathml = Prism.languages.markup;
  344. Prism.languages.svg = Prism.languages.markup;
  345. Prism.languages.css = {
  346. comment: /\/\*[\w\W]*?\*\//,
  347. atrule: {
  348. pattern: /@[\w-]+?.*?(;|(?=\s*\{))/i,
  349. inside: { rule: /@[\w-]+/ }
  350. },
  351. url: /url\((?:(["'])(\\(?:\r\n|[\w\W])|(?!\1)[^\\\r\n])*\1|.*?)\)/i,
  352. selector: /[^\{\}\s][^\{\};]*?(?=\s*\{)/,
  353. string: /("|')(\\(?:\r\n|[\w\W])|(?!\1)[^\\\r\n])*\1/,
  354. property: /(\b|\B)[\w-]+(?=\s*:)/i,
  355. important: /\B!important\b/i,
  356. function: /[-a-z0-9]+(?=\()/i,
  357. punctuation: /[(){};:]/
  358. };
  359. Prism.languages.css.atrule.inside.rest = Prism.util.clone(Prism.languages.css);
  360. if (Prism.languages.markup) {
  361. Prism.languages.insertBefore('markup', 'tag', {
  362. style: {
  363. pattern: /<style[\w\W]*?>[\w\W]*?<\/style>/i,
  364. inside: {
  365. tag: {
  366. pattern: /<style[\w\W]*?>|<\/style>/i,
  367. inside: Prism.languages.markup.tag.inside
  368. },
  369. rest: Prism.languages.css
  370. },
  371. alias: 'language-css'
  372. }
  373. });
  374. Prism.languages.insertBefore('inside', 'attr-value', {
  375. 'style-attr': {
  376. pattern: /\s*style=("|').*?\1/i,
  377. inside: {
  378. 'attr-name': {
  379. pattern: /^\s*style/i,
  380. inside: Prism.languages.markup.tag.inside
  381. },
  382. 'punctuation': /^\s*=\s*['"]|['"]\s*$/,
  383. 'attr-value': {
  384. pattern: /.+/i,
  385. inside: Prism.languages.css
  386. }
  387. },
  388. alias: 'language-css'
  389. }
  390. }, Prism.languages.markup.tag);
  391. }
  392. Prism.languages.clike = {
  393. 'comment': [
  394. {
  395. pattern: /(^|[^\\])\/\*[\w\W]*?\*\//,
  396. lookbehind: true
  397. },
  398. {
  399. pattern: /(^|[^\\:])\/\/.*/,
  400. lookbehind: true
  401. }
  402. ],
  403. 'string': /(["'])(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
  404. 'class-name': {
  405. pattern: /((?:\b(?:class|interface|extends|implements|trait|instanceof|new)\s+)|(?:catch\s+\())[a-z0-9_\.\\]+/i,
  406. lookbehind: true,
  407. inside: { punctuation: /(\.|\\)/ }
  408. },
  409. 'keyword': /\b(if|else|while|do|for|return|in|instanceof|function|new|try|throw|catch|finally|null|break|continue)\b/,
  410. 'boolean': /\b(true|false)\b/,
  411. 'function': /[a-z0-9_]+(?=\()/i,
  412. 'number': /\b-?(?:0x[\da-f]+|\d*\.?\d+(?:e[+-]?\d+)?)\b/i,
  413. 'operator': /--?|\+\+?|!=?=?|<=?|>=?|==?=?|&&?|\|\|?|\?|\*|\/|~|\^|%/,
  414. 'punctuation': /[{}[\];(),.:]/
  415. };
  416. Prism.languages.javascript = Prism.languages.extend('clike', {
  417. keyword: /\b(as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|false|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|true|try|typeof|var|void|while|with|yield)\b/,
  418. number: /\b-?(0x[\dA-Fa-f]+|0b[01]+|0o[0-7]+|\d*\.?\d+([Ee][+-]?\d+)?|NaN|Infinity)\b/,
  419. function: /[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*(?=\()/i
  420. });
  421. Prism.languages.insertBefore('javascript', 'keyword', {
  422. regex: {
  423. pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\\\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})]))/,
  424. lookbehind: true
  425. }
  426. });
  427. Prism.languages.insertBefore('javascript', 'class-name', {
  428. 'template-string': {
  429. pattern: /`(?:\\`|\\?[^`])*`/,
  430. inside: {
  431. interpolation: {
  432. pattern: /\$\{[^}]+\}/,
  433. inside: {
  434. 'interpolation-punctuation': {
  435. pattern: /^\$\{|\}$/,
  436. alias: 'punctuation'
  437. },
  438. 'rest': Prism.languages.javascript
  439. }
  440. },
  441. string: /[\s\S]+/
  442. }
  443. }
  444. });
  445. if (Prism.languages.markup) {
  446. Prism.languages.insertBefore('markup', 'tag', {
  447. script: {
  448. pattern: /<script[\w\W]*?>[\w\W]*?<\/script>/i,
  449. inside: {
  450. tag: {
  451. pattern: /<script[\w\W]*?>|<\/script>/i,
  452. inside: Prism.languages.markup.tag.inside
  453. },
  454. rest: Prism.languages.javascript
  455. },
  456. alias: 'language-javascript'
  457. }
  458. });
  459. }
  460. Prism.languages.js = Prism.languages.javascript;
  461. Prism.languages.c = Prism.languages.extend('clike', {
  462. keyword: /\b(asm|typeof|inline|auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while)\b/,
  463. operator: /\-[>-]?|\+\+?|!=?|<<?=?|>>?=?|==?|&&?|\|?\||[~^%?*\/]/,
  464. number: /\b-?(?:0x[\da-f]+|\d*\.?\d+(?:e[+-]?\d+)?)[ful]*\b/i
  465. });
  466. Prism.languages.insertBefore('c', 'string', {
  467. macro: {
  468. pattern: /(^\s*)#\s*[a-z]+([^\r\n\\]|\\.|\\(?:\r\n?|\n))*/im,
  469. lookbehind: true,
  470. alias: 'property',
  471. inside: {
  472. string: {
  473. pattern: /(#\s*include\s*)(<.+?>|("|')(\\?.)+?\3)/,
  474. lookbehind: true
  475. }
  476. }
  477. }
  478. });
  479. delete Prism.languages.c['class-name'];
  480. delete Prism.languages.c.boolean;
  481. Prism.languages.csharp = Prism.languages.extend('clike', {
  482. keyword: /\b(abstract|as|async|await|base|bool|break|byte|case|catch|char|checked|class|const|continue|decimal|default|delegate|do|double|else|enum|event|explicit|extern|false|finally|fixed|float|for|foreach|goto|if|implicit|in|int|interface|internal|is|lock|long|namespace|new|null|object|operator|out|override|params|private|protected|public|readonly|ref|return|sbyte|sealed|short|sizeof|stackalloc|static|string|struct|switch|this|throw|true|try|typeof|uint|ulong|unchecked|unsafe|ushort|using|virtual|void|volatile|while|add|alias|ascending|async|await|descending|dynamic|from|get|global|group|into|join|let|orderby|partial|remove|select|set|value|var|where|yield)\b/,
  483. string: [
  484. /@("|')(\1\1|\\\1|\\?(?!\1)[\s\S])*\1/,
  485. /("|')(\\?.)*?\1/
  486. ],
  487. number: /\b-?(0x[\da-f]+|\d*\.?\d+)\b/i
  488. });
  489. Prism.languages.insertBefore('csharp', 'keyword', {
  490. preprocessor: {
  491. pattern: /(^\s*)#.*/m,
  492. lookbehind: true
  493. }
  494. });
  495. Prism.languages.cpp = Prism.languages.extend('c', {
  496. keyword: /\b(alignas|alignof|asm|auto|bool|break|case|catch|char|char16_t|char32_t|class|compl|const|constexpr|const_cast|continue|decltype|default|delete|do|double|dynamic_cast|else|enum|explicit|export|extern|float|for|friend|goto|if|inline|int|long|mutable|namespace|new|noexcept|nullptr|operator|private|protected|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_assert|static_cast|struct|switch|template|this|thread_local|throw|try|typedef|typeid|typename|union|unsigned|using|virtual|void|volatile|wchar_t|while)\b/,
  497. boolean: /\b(true|false)\b/,
  498. operator: /[-+]{1,2}|!=?|<{1,2}=?|>{1,2}=?|\->|:{1,2}|={1,2}|\^|~|%|&{1,2}|\|?\||\?|\*|\/|\b(and|and_eq|bitand|bitor|not|not_eq|or|or_eq|xor|xor_eq)\b/
  499. });
  500. Prism.languages.insertBefore('cpp', 'keyword', {
  501. 'class-name': {
  502. pattern: /(class\s+)[a-z0-9_]+/i,
  503. lookbehind: true
  504. }
  505. });
  506. Prism.languages.java = Prism.languages.extend('clike', {
  507. keyword: /\b(abstract|continue|for|new|switch|assert|default|goto|package|synchronized|boolean|do|if|private|this|break|double|implements|protected|throw|byte|else|import|public|throws|case|enum|instanceof|return|transient|catch|extends|int|short|try|char|final|interface|static|void|class|finally|long|strictfp|volatile|const|float|native|super|while)\b/,
  508. number: /\b0b[01]+\b|\b0x[\da-f]*\.?[\da-fp\-]+\b|\b\d*\.?\d+(?:e[+-]?\d+)?[df]?\b/i,
  509. operator: {
  510. pattern: /(^|[^.])(?:\+[+=]?|-[-=]?|!=?|<<?=?|>>?>?=?|==?|&[&=]?|\|[|=]?|\*=?|\/=?|%=?|\^=?|[?:~])/m,
  511. lookbehind: true
  512. }
  513. });
  514. Prism.languages.php = Prism.languages.extend('clike', {
  515. keyword: /\b(and|or|xor|array|as|break|case|cfunction|class|const|continue|declare|default|die|do|else|elseif|enddeclare|endfor|endforeach|endif|endswitch|endwhile|extends|for|foreach|function|include|include_once|global|if|new|return|static|switch|use|require|require_once|var|while|abstract|interface|public|implements|private|protected|parent|throw|null|echo|print|trait|namespace|final|yield|goto|instanceof|finally|try|catch)\b/i,
  516. constant: /\b[A-Z0-9_]{2,}\b/,
  517. comment: {
  518. pattern: /(^|[^\\])(?:\/\*[\w\W]*?\*\/|\/\/.*)/,
  519. lookbehind: true
  520. }
  521. });
  522. Prism.languages.insertBefore('php', 'class-name', {
  523. 'shell-comment': {
  524. pattern: /(^|[^\\])#.*/,
  525. lookbehind: true,
  526. alias: 'comment'
  527. }
  528. });
  529. Prism.languages.insertBefore('php', 'keyword', {
  530. delimiter: /\?>|<\?(?:php)?/i,
  531. variable: /\$\w+\b/i,
  532. package: {
  533. pattern: /(\\|namespace\s+|use\s+)[\w\\]+/,
  534. lookbehind: true,
  535. inside: { punctuation: /\\/ }
  536. }
  537. });
  538. Prism.languages.insertBefore('php', 'operator', {
  539. property: {
  540. pattern: /(->)[\w]+/,
  541. lookbehind: true
  542. }
  543. });
  544. if (Prism.languages.markup) {
  545. Prism.hooks.add('before-highlight', function (env) {
  546. if (env.language !== 'php') {
  547. return;
  548. }
  549. env.tokenStack = [];
  550. env.backupCode = env.code;
  551. env.code = env.code.replace(/(?:<\?php|<\?)[\w\W]*?(?:\?>)/ig, function (match) {
  552. env.tokenStack.push(match);
  553. return '{{{PHP' + env.tokenStack.length + '}}}';
  554. });
  555. });
  556. Prism.hooks.add('before-insert', function (env) {
  557. if (env.language === 'php') {
  558. env.code = env.backupCode;
  559. delete env.backupCode;
  560. }
  561. });
  562. Prism.hooks.add('after-highlight', function (env) {
  563. if (env.language !== 'php') {
  564. return;
  565. }
  566. for (var i = 0, t = void 0; t = env.tokenStack[i]; i++) {
  567. env.highlightedCode = env.highlightedCode.replace('{{{PHP' + (i + 1) + '}}}', Prism.highlight(t, env.grammar, 'php').replace(/\$/g, '$$$$'));
  568. }
  569. env.element.innerHTML = env.highlightedCode;
  570. });
  571. Prism.hooks.add('wrap', function (env) {
  572. if (env.language === 'php' && env.type === 'markup') {
  573. env.content = env.content.replace(/(\{\{\{PHP[0-9]+\}\}\})/g, '<span class="token php">$1</span>');
  574. }
  575. });
  576. Prism.languages.insertBefore('php', 'comment', {
  577. markup: {
  578. pattern: /<[^?]\/?(.*?)>/,
  579. inside: Prism.languages.markup
  580. },
  581. php: /\{\{\{PHP[0-9]+\}\}\}/
  582. });
  583. }
  584. Prism.languages.python = {
  585. 'comment': {
  586. pattern: /(^|[^\\])#.*/,
  587. lookbehind: true
  588. },
  589. 'string': /"""[\s\S]+?"""|'''[\s\S]+?'''|("|')(?:\\?.)*?\1/,
  590. 'function': {
  591. pattern: /((?:^|\s)def[ \t]+)[a-zA-Z_][a-zA-Z0-9_]*(?=\()/g,
  592. lookbehind: true
  593. },
  594. 'class-name': {
  595. pattern: /(\bclass\s+)[a-z0-9_]+/i,
  596. lookbehind: true
  597. },
  598. 'keyword': /\b(?:as|assert|async|await|break|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|pass|print|raise|return|try|while|with|yield)\b/,
  599. 'boolean': /\b(?:True|False)\b/,
  600. 'number': /\b-?(?:0[bo])?(?:(?:\d|0x[\da-f])[\da-f]*\.?\d*|\.\d+)(?:e[+-]?\d+)?j?\b/i,
  601. 'operator': /[-+%=]=?|!=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~]|\b(?:or|and|not)\b/,
  602. 'punctuation': /[{}[\];(),.:]/
  603. };
  604. (function (Prism) {
  605. Prism.languages.ruby = Prism.languages.extend('clike', {
  606. comment: /#(?!\{[^\r\n]*?\}).*/,
  607. keyword: /\b(alias|and|BEGIN|begin|break|case|class|def|define_method|defined|do|each|else|elsif|END|end|ensure|false|for|if|in|module|new|next|nil|not|or|raise|redo|require|rescue|retry|return|self|super|then|throw|true|undef|unless|until|when|while|yield)\b/
  608. });
  609. var interpolation = {
  610. pattern: /#\{[^}]+\}/,
  611. inside: {
  612. delimiter: {
  613. pattern: /^#\{|\}$/,
  614. alias: 'tag'
  615. },
  616. rest: Prism.util.clone(Prism.languages.ruby)
  617. }
  618. };
  619. Prism.languages.insertBefore('ruby', 'keyword', {
  620. regex: [
  621. {
  622. pattern: /%r([^a-zA-Z0-9\s\{\(\[<])(?:[^\\]|\\[\s\S])*?\1[gim]{0,3}/,
  623. inside: { interpolation: interpolation }
  624. },
  625. {
  626. pattern: /%r\((?:[^()\\]|\\[\s\S])*\)[gim]{0,3}/,
  627. inside: { interpolation: interpolation }
  628. },
  629. {
  630. pattern: /%r\{(?:[^#{}\\]|#(?:\{[^}]+\})?|\\[\s\S])*\}[gim]{0,3}/,
  631. inside: { interpolation: interpolation }
  632. },
  633. {
  634. pattern: /%r\[(?:[^\[\]\\]|\\[\s\S])*\][gim]{0,3}/,
  635. inside: { interpolation: interpolation }
  636. },
  637. {
  638. pattern: /%r<(?:[^<>\\]|\\[\s\S])*>[gim]{0,3}/,
  639. inside: { interpolation: interpolation }
  640. },
  641. {
  642. pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\r\n])+\/[gim]{0,3}(?=\s*($|[\r\n,.;})]))/,
  643. lookbehind: true
  644. }
  645. ],
  646. variable: /[@$]+[a-zA-Z_][a-zA-Z_0-9]*(?:[?!]|\b)/,
  647. symbol: /:[a-zA-Z_][a-zA-Z_0-9]*(?:[?!]|\b)/
  648. });
  649. Prism.languages.insertBefore('ruby', 'number', {
  650. builtin: /\b(Array|Bignum|Binding|Class|Continuation|Dir|Exception|FalseClass|File|Stat|File|Fixnum|Fload|Hash|Integer|IO|MatchData|Method|Module|NilClass|Numeric|Object|Proc|Range|Regexp|String|Struct|TMS|Symbol|ThreadGroup|Thread|Time|TrueClass)\b/,
  651. constant: /\b[A-Z][a-zA-Z_0-9]*(?:[?!]|\b)/
  652. });
  653. Prism.languages.ruby.string = [
  654. {
  655. pattern: /%[qQiIwWxs]?([^a-zA-Z0-9\s\{\(\[<])(?:[^\\]|\\[\s\S])*?\1/,
  656. inside: { interpolation: interpolation }
  657. },
  658. {
  659. pattern: /%[qQiIwWxs]?\((?:[^()\\]|\\[\s\S])*\)/,
  660. inside: { interpolation: interpolation }
  661. },
  662. {
  663. pattern: /%[qQiIwWxs]?\{(?:[^#{}\\]|#(?:\{[^}]+\})?|\\[\s\S])*\}/,
  664. inside: { interpolation: interpolation }
  665. },
  666. {
  667. pattern: /%[qQiIwWxs]?\[(?:[^\[\]\\]|\\[\s\S])*\]/,
  668. inside: { interpolation: interpolation }
  669. },
  670. {
  671. pattern: /%[qQiIwWxs]?<(?:[^<>\\]|\\[\s\S])*>/,
  672. inside: { interpolation: interpolation }
  673. },
  674. {
  675. pattern: /("|')(#\{[^}]+\}|\\(?:\r?\n|\r)|\\?.)*?\1/,
  676. inside: { interpolation: interpolation }
  677. }
  678. ];
  679. }(Prism));
  680. function isCodeSample(elm) {
  681. return elm && elm.nodeName === 'PRE' && elm.className.indexOf('language-') !== -1;
  682. }
  683. function trimArg(predicateFn) {
  684. return function (arg1, arg2) {
  685. return predicateFn(arg2);
  686. };
  687. }
  688. var Utils = {
  689. isCodeSample: isCodeSample,
  690. trimArg: trimArg
  691. };
  692. var constant = function (value) {
  693. return function () {
  694. return value;
  695. };
  696. };
  697. var never = constant(false);
  698. var always = constant(true);
  699. var never$1 = never;
  700. var always$1 = always;
  701. var none = function () {
  702. return NONE;
  703. };
  704. var NONE = function () {
  705. var eq = function (o) {
  706. return o.isNone();
  707. };
  708. var call = function (thunk) {
  709. return thunk();
  710. };
  711. var id = function (n) {
  712. return n;
  713. };
  714. var noop = function () {
  715. };
  716. var nul = function () {
  717. return null;
  718. };
  719. var undef = function () {
  720. return undefined;
  721. };
  722. var me = {
  723. fold: function (n, s) {
  724. return n();
  725. },
  726. is: never$1,
  727. isSome: never$1,
  728. isNone: always$1,
  729. getOr: id,
  730. getOrThunk: call,
  731. getOrDie: function (msg) {
  732. throw new Error(msg || 'error: getOrDie called on none.');
  733. },
  734. getOrNull: nul,
  735. getOrUndefined: undef,
  736. or: id,
  737. orThunk: call,
  738. map: none,
  739. ap: none,
  740. each: noop,
  741. bind: none,
  742. flatten: none,
  743. exists: never$1,
  744. forall: always$1,
  745. filter: none,
  746. equals: eq,
  747. equals_: eq,
  748. toArray: function () {
  749. return [];
  750. },
  751. toString: constant('none()')
  752. };
  753. if (Object.freeze)
  754. Object.freeze(me);
  755. return me;
  756. }();
  757. var some = function (a) {
  758. var constant_a = function () {
  759. return a;
  760. };
  761. var self = function () {
  762. return me;
  763. };
  764. var map = function (f) {
  765. return some(f(a));
  766. };
  767. var bind = function (f) {
  768. return f(a);
  769. };
  770. var me = {
  771. fold: function (n, s) {
  772. return s(a);
  773. },
  774. is: function (v) {
  775. return a === v;
  776. },
  777. isSome: always$1,
  778. isNone: never$1,
  779. getOr: constant_a,
  780. getOrThunk: constant_a,
  781. getOrDie: constant_a,
  782. getOrNull: constant_a,
  783. getOrUndefined: constant_a,
  784. or: self,
  785. orThunk: self,
  786. map: map,
  787. ap: function (optfab) {
  788. return optfab.fold(none, function (fab) {
  789. return some(fab(a));
  790. });
  791. },
  792. each: function (f) {
  793. f(a);
  794. },
  795. bind: bind,
  796. flatten: constant_a,
  797. exists: bind,
  798. forall: bind,
  799. filter: function (f) {
  800. return f(a) ? me : NONE;
  801. },
  802. equals: function (o) {
  803. return o.is(a);
  804. },
  805. equals_: function (o, elementEq) {
  806. return o.fold(never$1, function (b) {
  807. return elementEq(a, b);
  808. });
  809. },
  810. toArray: function () {
  811. return [a];
  812. },
  813. toString: function () {
  814. return 'some(' + a + ')';
  815. }
  816. };
  817. return me;
  818. };
  819. var from = function (value) {
  820. return value === null || value === undefined ? NONE : some(value);
  821. };
  822. var Option = {
  823. some: some,
  824. none: none,
  825. from: from
  826. };
  827. var getSelectedCodeSample = function (editor) {
  828. var node = editor.selection ? editor.selection.getNode() : null;
  829. if (Utils.isCodeSample(node)) {
  830. return Option.some(node);
  831. }
  832. return Option.none();
  833. };
  834. var insertCodeSample = function (editor, language, code) {
  835. editor.undoManager.transact(function () {
  836. var node = getSelectedCodeSample(editor);
  837. code = global$1.DOM.encode(code);
  838. return node.fold(function () {
  839. editor.insertContent('<pre id="__new" class="language-' + language + '">' + code + '</pre>');
  840. editor.selection.select(editor.$('#__new').removeAttr('id')[0]);
  841. }, function (n) {
  842. editor.dom.setAttrib(n, 'class', 'language-' + language);
  843. n.innerHTML = code;
  844. Prism.highlightElement(n);
  845. editor.selection.select(n);
  846. });
  847. });
  848. };
  849. var getCurrentCode = function (editor) {
  850. var node = getSelectedCodeSample(editor);
  851. return node.fold(function () {
  852. return '';
  853. }, function (n) {
  854. return n.textContent;
  855. });
  856. };
  857. var CodeSample = {
  858. getSelectedCodeSample: getSelectedCodeSample,
  859. insertCodeSample: insertCodeSample,
  860. getCurrentCode: getCurrentCode
  861. };
  862. var getLanguages = function (editor) {
  863. return editor.settings.codesample_languages;
  864. };
  865. var Settings = { getLanguages: getLanguages };
  866. var getLanguages$1 = function (editor) {
  867. var defaultLanguages = [
  868. {
  869. text: 'HTML/XML',
  870. value: 'markup'
  871. },
  872. {
  873. text: 'JavaScript',
  874. value: 'javascript'
  875. },
  876. {
  877. text: 'CSS',
  878. value: 'css'
  879. },
  880. {
  881. text: 'PHP',
  882. value: 'php'
  883. },
  884. {
  885. text: 'Ruby',
  886. value: 'ruby'
  887. },
  888. {
  889. text: 'Python',
  890. value: 'python'
  891. },
  892. {
  893. text: 'Java',
  894. value: 'java'
  895. },
  896. {
  897. text: 'C',
  898. value: 'c'
  899. },
  900. {
  901. text: 'C#',
  902. value: 'csharp'
  903. },
  904. {
  905. text: 'C++',
  906. value: 'cpp'
  907. }
  908. ];
  909. var customLanguages = Settings.getLanguages(editor);
  910. return customLanguages ? customLanguages : defaultLanguages;
  911. };
  912. var getCurrentLanguage = function (editor, fallback) {
  913. var node = CodeSample.getSelectedCodeSample(editor);
  914. return node.fold(function () {
  915. return fallback;
  916. }, function (n) {
  917. var matches = n.className.match(/language-(\w+)/);
  918. return matches ? matches[1] : fallback;
  919. });
  920. };
  921. var Languages = {
  922. getLanguages: getLanguages$1,
  923. getCurrentLanguage: getCurrentLanguage
  924. };
  925. var typeOf = function (x) {
  926. if (x === null)
  927. return 'null';
  928. var t = typeof x;
  929. if (t === 'object' && Array.prototype.isPrototypeOf(x))
  930. return 'array';
  931. if (t === 'object' && String.prototype.isPrototypeOf(x))
  932. return 'string';
  933. return t;
  934. };
  935. var isType = function (type) {
  936. return function (value) {
  937. return typeOf(value) === type;
  938. };
  939. };
  940. var isFunction = isType('function');
  941. var slice = Array.prototype.slice;
  942. var head = function (xs) {
  943. return xs.length === 0 ? Option.none() : Option.some(xs[0]);
  944. };
  945. var from$1 = isFunction(Array.from) ? Array.from : function (x) {
  946. return slice.call(x);
  947. };
  948. var open = function (editor) {
  949. var languages = Languages.getLanguages(editor);
  950. var defaultLanguage = head(languages).fold(function () {
  951. return '';
  952. }, function (l) {
  953. return l.value;
  954. });
  955. var currentLanguage = Languages.getCurrentLanguage(editor, defaultLanguage);
  956. var currentCode = CodeSample.getCurrentCode(editor);
  957. editor.windowManager.open({
  958. title: 'Insert/Edit Code Sample',
  959. size: 'large',
  960. body: {
  961. type: 'panel',
  962. items: [
  963. {
  964. type: 'selectbox',
  965. name: 'language',
  966. label: 'Language',
  967. items: languages
  968. },
  969. {
  970. type: 'textarea',
  971. name: 'code',
  972. label: 'Code view'
  973. }
  974. ]
  975. },
  976. buttons: [
  977. {
  978. type: 'cancel',
  979. name: 'cancel',
  980. text: 'Cancel'
  981. },
  982. {
  983. type: 'submit',
  984. name: 'save',
  985. text: 'Save',
  986. primary: true
  987. }
  988. ],
  989. initialData: {
  990. language: currentLanguage,
  991. code: currentCode
  992. },
  993. onSubmit: function (api) {
  994. var data = api.getData();
  995. CodeSample.insertCodeSample(editor, data.language, data.code);
  996. api.close();
  997. }
  998. });
  999. };
  1000. var Dialog = { open: open };
  1001. var register = function (editor) {
  1002. editor.addCommand('codesample', function () {
  1003. var node = editor.selection.getNode();
  1004. if (editor.selection.isCollapsed() || Utils.isCodeSample(node)) {
  1005. Dialog.open(editor);
  1006. } else {
  1007. editor.formatter.toggle('code');
  1008. }
  1009. });
  1010. };
  1011. var Commands = { register: register };
  1012. var setup = function (editor) {
  1013. var $ = editor.$;
  1014. editor.on('PreProcess', function (e) {
  1015. $('pre[contenteditable=false]', e.node).filter(Utils.trimArg(Utils.isCodeSample)).each(function (idx, elm) {
  1016. var $elm = $(elm), code = elm.textContent;
  1017. $elm.attr('class', $.trim($elm.attr('class')));
  1018. $elm.removeAttr('contentEditable');
  1019. $elm.empty().append($('<code></code>').each(function () {
  1020. this.textContent = code;
  1021. }));
  1022. });
  1023. });
  1024. editor.on('SetContent', function () {
  1025. var unprocessedCodeSamples = $('pre').filter(Utils.trimArg(Utils.isCodeSample)).filter(function (idx, elm) {
  1026. return elm.contentEditable !== 'false';
  1027. });
  1028. if (unprocessedCodeSamples.length) {
  1029. editor.undoManager.transact(function () {
  1030. unprocessedCodeSamples.each(function (idx, elm) {
  1031. $(elm).find('br').each(function (idx, elm) {
  1032. elm.parentNode.replaceChild(editor.getDoc().createTextNode('\n'), elm);
  1033. });
  1034. elm.contentEditable = false;
  1035. elm.innerHTML = editor.dom.encode(elm.textContent);
  1036. Prism.highlightElement(elm);
  1037. elm.className = $.trim(elm.className);
  1038. });
  1039. });
  1040. }
  1041. });
  1042. };
  1043. var FilterContent = { setup: setup };
  1044. var isCodeSampleSelection = function (editor) {
  1045. var node = editor.selection.getStart();
  1046. return editor.dom.is(node, 'pre.language-markup');
  1047. };
  1048. var register$1 = function (editor) {
  1049. editor.ui.registry.addToggleButton('codesample', {
  1050. icon: 'code-sample',
  1051. tooltip: 'Insert/edit code sample',
  1052. onAction: function () {
  1053. return Dialog.open(editor);
  1054. },
  1055. onSetup: function (api) {
  1056. var nodeChangeHandler = function () {
  1057. api.setActive(isCodeSampleSelection(editor));
  1058. };
  1059. editor.on('nodeChange', nodeChangeHandler);
  1060. return function () {
  1061. return editor.off('nodeChange', nodeChangeHandler);
  1062. };
  1063. }
  1064. });
  1065. editor.ui.registry.addMenuItem('codesample', {
  1066. text: 'Code sample...',
  1067. icon: 'code-sample',
  1068. onAction: function () {
  1069. return Dialog.open(editor);
  1070. }
  1071. });
  1072. };
  1073. var Buttons = { register: register$1 };
  1074. global.add('codesample', function (editor, pluginUrl) {
  1075. FilterContent.setup(editor);
  1076. Buttons.register(editor);
  1077. Commands.register(editor);
  1078. editor.on('dblclick', function (ev) {
  1079. if (Utils.isCodeSample(ev.target)) {
  1080. Dialog.open(editor);
  1081. }
  1082. });
  1083. });
  1084. function Plugin () {
  1085. }
  1086. return Plugin;
  1087. }(window));
  1088. })();