Advertisement
12Me21

reg

May 24th, 2017
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. function Highlighter (languageDefinition) {
  2. for (var i = 0; i < languageDefinition.syntax.length; i++) {
  3. if (!languageDefinition.syntax[i].regex.global)
  4. throw "Highlighter regex must have global flag set";
  5. if (languageDefinition.syntax[i].keywords) {
  6. languageDefinition.syntax[i].regex=new RegExp(languageDefinition.syntax[i].replace(/([\.\[\]^$()+*?{}|\\])/ig,"\\$&").replace(/\s+/ig,"|"))
  7. }
  8. }new RegExp(regex.source,"gm"+(regex.global?"i":""))
  9. this.syntax = languageDefinition.syntax;
  10. }
  11.  
  12. //Escape < > & for setting innerHTML
  13. Highlighter.escapeHTML = function (text) {
  14. return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
  15. }
  16.  
  17. //Actual highlighter
  18. Highlighter.prototype.highlight = function (code) {
  19.  
  20. //find potential things to highlight
  21. var highlightList = [];
  22. for (var i = 0; i < this.syntax.length; i++) {
  23. var className = this.syntax[i].className;
  24. var regex = this.syntax[i].regex;
  25. var match;
  26. while (match = regex.exec(code))
  27. highlightList.push({
  28. start: match.index,
  29. end: match.index + match[0].length,
  30. className: className,
  31. index: highlightList.length
  32. });
  33. }
  34. highlightList = highlightList.sort(function (a,b) {
  35. return a.start - b.start || b.end - a.end || a.index - b.index;
  36. });
  37.  
  38. //insert highlighting
  39. var output = "";
  40. var pos = 0;
  41. for (var i = 0; i < highlightList.length; i++) {
  42. var highlight = highlightList[i];
  43. if (highlight.start >= pos) { //only highlight if it's past the end of the previous keyword
  44. if (highlight.className)
  45. output += Highlighter.escapeHTML(code.substring(pos, highlight.start)) +
  46. "<span class=\"" + highlight.className + "\">" +
  47. Highlighter.escapeHTML(code.substring(highlight.start, highlight.end)) +
  48. "</span>";
  49. else
  50. output += Highlighter.escapeHTML(code.substring(pos, highlight.end));
  51. pos = highlight.end;
  52. }
  53. }
  54. output += Highlighter.escapeHTML(code.substring(pos));
  55.  
  56. return output;
  57. }
  58.  
  59. //Highlight an element
  60. Highlighter.prototype.highlightElement = function (codeElements) {
  61. codeElements.innerHTML = this.highlight(codeElements.textContent);
  62. }
  63.  
  64. //Highlight a list of elements.
  65. Highlighter.prototype.highlightElements = function (codeElements) {
  66. for (var i = 0; i < codeElements.length; i++)
  67. this.highlightElement(codeElements[i])
  68. }
  69.  
  70. //Highlight a list of elements asynchronosly using web workers.
  71. if (window.Worker) {
  72. Highlighter.prototype.workerHighlightElements = function (codeElements) {
  73. //Get a list of code to highlight
  74. var textContents=new Array(codeElements.length)
  75. for(var i=0;i<codeElements.length;i++)
  76. textContents[i]=codeElements[i].textContent;
  77. //Create worker
  78. var worker = new Worker("rxhighlightworker.js");
  79. worker.onmessage = function (event) {
  80. for(var i=0;i<codeElements.length;i++)
  81. codeElements[i].innerHTML = event.data[i];
  82. worker.terminate();
  83. };
  84. worker.postMessage({code:textContents,syntax:this.syntax});
  85. }
  86. } else {
  87. //fallback for old browsers
  88. Highlighter.prototype.workerHighlightElements=Highlighter.prototype.highlightElements;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement