coloration.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. document.cookie = 'colorwithjs=1;path=/;max-age=' + (60*60*24*365);
  2. (function(){
  3. var line = 1;
  4. var ml;
  5. var firstTime = true;
  6. window.theCode = [];
  7. window.theCode2 = [];
  8. var currentTags = [];
  9. window.addCode = function (code) {
  10. if (firstTime) {
  11. ml = {};
  12. var tmp = window.marked_lines;
  13. if (tmp) {
  14. for (var i=0; i < tmp.length; i++) {
  15. ml[tmp[i]] = true;
  16. }
  17. }
  18. code = "\n" + code;
  19. }
  20. theCode.push (code);
  21. code = code.replace (
  22. /<([ACVSI])/g,
  23. function (str,letter) {
  24. return "<span class='" + letter.toLowerCase() + "'>";
  25. } );
  26. code = code.replace (
  27. /<([DML])([^>]+)>/g,
  28. function (str, letter, contents) {
  29. switch (letter) {
  30. case "D":
  31. return "<a class=\"d\" href=\"" + ident_cgi + "?i=" + contents + "\">" +contents + "</a>";
  32. case "M":
  33. return "<a href=\"mailto:" + contents + "\">&lt;" + contents + "&gt;</a>";
  34. case "L":
  35. return "<a href=\"" + contents + "\">" + contents + "</a>";
  36. }
  37. return
  38. } );
  39. code = code.replace(
  40. /<(\/?)([^\s>]+)(\s+[^>]+)?>|(>)/g,
  41. function (str, isEndTag, tagName, attributesStr, gt) {
  42. if (!isEndTag && tagName) {
  43. currentTags.push (tagName);
  44. } else {
  45. var string = arguments [arguments.length - 1];
  46. var offset = arguments [arguments.length - 2];
  47. var startTag = currentTags.pop();
  48. if (gt) {
  49. //throw new Error( gt + " found alone (offset: " + offset + ")\n\n" + string.slice( offset, 150 ) );
  50. str = "</" + startTag + ">";
  51. } else if (attributesStr) {
  52. throw new Error ("Incorrect end tag:\n" + str);
  53. } else if (startTag != tagName) {
  54. throw new Error ("Mismatching tags: <" + startTag + "> ... </" + tagName + ">");
  55. }
  56. }
  57. return str;
  58. } );
  59. code = code.replace (
  60. /\n(<div class="m">|<\/div>|<style>[^<]*<\/style>){0,}/g,
  61. function (str) {
  62. var l = line++;
  63. var d = ("" + l).length - 1;
  64. if (l in ml) {
  65. d += " m";
  66. }
  67. return str + "<a class='l d" + d + "' name=" + l + " href=\"#" + l + "\">" + l + "</a> ";
  68. } );
  69. if (firstTime) {
  70. firstTime = false;
  71. code = code.slice (1);
  72. }
  73. theCode2.push (code);
  74. document.write (code);
  75. };
  76. })();