PrepareHTML.full.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /** Converts <>&" to their HTML escape sequences */
  2. function escapeHTMLEntities(str) {
  3. return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
  4. }
  5. /** Restores the original source string's '<' and '>' as entered in
  6. the document, before the browser processed it as HTML. There is no
  7. way in an HTML document to distinguish an entity that was entered
  8. as an entity.*/
  9. function unescapeHTMLEntities(str) {
  10. // Process &amp; last so that we don't recursively unescape
  11. // escaped escape sequences.
  12. return str.
  13. replace(/&lt;/g, '<').
  14. replace(/&gt;/g, '>').
  15. replace(/&quot;/g, '"').
  16. replace(/&#39;/g, "'").
  17. replace(/&ndash;/g, '--').
  18. replace(/&mdash;/g, '---').
  19. replace(/&amp;/g, '&');
  20. }
  21. /**
  22. \param node A node from an HTML DOM
  23. \return A String that is a very good reconstruction of what the
  24. original source looked like before the browser tried to correct
  25. it to legal HTML.
  26. */
  27. function nodeToMarkdeepSource(node, leaveEscapes) {
  28. var source = node.innerHTML;
  29. // Markdown uses <[email protected]> e-mail syntax, which HTML parsing
  30. // will try to close by inserting the matching close tags at the end of the
  31. // document. Remove anything that looks like that and comes *after*
  32. // the first fallback style.
  33. source = source.replace(/(?:<style class="fallback">[\s\S]*?<\/style>[\s\S]*)<\/\S+@\S+\.\S+?>/gim, '');
  34. // Remove artificially inserted close tags
  35. source = source.replace(/<\/h?ttps?:.*>/gi, '');
  36. // Now try to fix the URLs themselves, which will be
  37. // transformed like this: <http: casual-effects.com="" markdeep="">
  38. source = source.replace(/<(https?): (.*?)>/gi, function (match, protocol, list) {
  39. // Remove any quotes--they wouldn't have been legal in the URL anyway
  40. var s = '<' + protocol + '://' + list.replace(/=""\s/g, '/');
  41. if (s.substring(s.length - 3) === '=""') {
  42. s = s.substring(0, s.length - 3);
  43. }
  44. // Remove any lingering quotes (since they
  45. // wouldn't have been legal in the URL)
  46. s = s.replace(/"/g, '');
  47. return s + '>';
  48. });
  49. // Remove the "fallback" style tags
  50. source = source.replace(/<style class=["']fallback["']>.*?<\/style>/gmi, '');
  51. source = unescapeHTMLEntities(source);
  52. return source;
  53. }
  54. // $ (FULL_DOCUMENT_HEAD) is replaced by the contents of the <head> found in
  55. // PreviewBlogPage.htm. This document head will overwrite whatever Markdeep does to
  56. // the head at the very end.
  57. FullDocumentHead='\
  58. \r\n\
  59. <meta http-equiv="content-type" content="text/html; charset=UTF-8">\r\n\
  60. <meta charset="UTF-8">\r\n\
  61. \r\n\
  62. <!-- Markdeep styles -->\r\n\
  63. <style>body{counter-reset: h1 h2 h3 h4 h5 h6}.md code,pre{font-family:Menlo,Consolas,monospace;font-size:15.018802542857143px;line-height:140%}.md div.title{font-size:26px;font-weight:800;line-height:120%;text-align:center}.md div.afterTitles{height:10px}.md div.subtitle{text-align:center}.md .image{display:inline-block}.md div.imagecaption,.md div.tablecaption,.md div.listingcaption{margin:0.2em 5px 10px 5px;text-align: justify;font-style:italic}.md div.imagecaption{margin-bottom:0}.md img{max-width:100%;page-break-inside:avoid}li{text-align:left};.md div.tilde{margin:20px 0 -10px;text-align:center}.md blockquote.fancyquote{margin:25px 0 25px;text-align:left;line-height:160%}.md blockquote.fancyquote::before{content:"“";color:#DDD;font-family:Times New Roman;font-size:45px;line-height:0;margin-right:6px;vertical-align:-0.3em}.md span.fancyquote{font-size:118%;color:#777;font-style:italic}.md span.fancyquote::after{content:"”";font-style:normal;color:#DDD;font-family:Times New Roman;font-size:45px;line-height:0;margin-left:6px;vertical-align:-0.3em}.md blockquote.fancyquote .author{width:100%;margin-top:10px;display:inline-block;text-align:right}.md small{font-size:60%}.md div.title,contents,.md .tocHeader,h1,h2,h3,h4,h5,h6,.md .shortTOC,.md .mediumTOC,.nonumberh1,.nonumberh2,.nonumberh3,.nonumberh4,.nonumberh5,.nonumberh6{font-family:Verdana,Helvetica,Arial,sans-serif;margin:13.4px 0 13.4px;padding:15px 0 3px;border-top:none;clear:both}.md svg.diagram{display:block;font-family:Menlo,Consolas,monospace;font-size:15.018802542857143px;text-align:center;stroke-linecap:round;stroke-width:2px;page-break-inside:avoid;stroke:#000;fill:#000}.md svg.diagram .opendot{fill:#FFF}.md svg.diagram text{stroke:none}.md h1,.tocHeader,.nonumberh1{border-bottom:3px solid;font-size:20px;font-weight:bold;}h1,.nonumberh1{counter-reset: h2 h3 h4 h5 h6}h2,.nonumberh2{counter-reset: h3 h4 h5 h6;border-bottom:2px solid #999;color:#555;font-size:18px;}h3,h4,h5,h6,.nonumberh3,.nonumberh4,.nonumberh5,.nonumberh6{font-family:Helvetica,Arial,sans-serif;color:#555;font-size:16px;}h3{counter-reset:h4 h5 h6}h4{counter-reset:h5 h6}h5{counter-reset:h6}.md table{border-collapse:collapse;line-height:140%;page-break-inside:avoid}.md table.table{margin:auto}.md table.calendar{width:100%;margin:auto;font-size:11px;font-family:Helvetica,Arial,sans-serif}.md table.calendar th{font-size:16px}.md .today{background:#ECF8FA}.md .calendar .parenthesized{color:#999;font-style:italic}.md div.tablecaption{text-align:center}.md table.table th{color:#FFF;background-color:#AAA;border:1px solid #888;padding:8px 15px 8px 15px}.md table.table td{padding:5px 15px 5px 15px;border:1px solid #888}.md table.table tr:nth-child(even){background:#EEE}.md pre.tilde{border-top: 1px solid #CCC;border-bottom: 1px solid #CCC;padding: 5px 0 5px 20px;margin:0 0 30px 0;background:#FCFCFC;page-break-inside:avoid}.md a:link, .md a:visited{color:#38A;text-decoration:none}.md a:link:hover{text-decoration:underline}.md dt{font-weight:700}dl>.md dd{padding:0 0 18px}.md dl>table{margin:35px 0 30px}.md code{white-space:pre;page-break-inside:avoid}.md .endnote{font-size:13px;line-height:15px;padding-left:10px;text-indent:-10px}.md .bib{padding-left:80px;text-indent:-80px;text-align:left}.markdeepFooter{font-size:9px;text-align:right;padding-top:80px;color:#999}.md .mediumTOC{float:right;font-size:12px;line-height:15px;border-left:1px solid #CCC;padding-left:15px;margin:15px 0px 15px 25px}.md .mediumTOC .level1{font-weight:600}.md .longTOC .level1{font-weight:600;display:block;padding-top:12px;margin:0 0 -20px}.md .shortTOC{text-align:center;font-weight:bold;margin-top:15px;font-size:14px}</style>\r\n\
  64. \r\n\
  65. <!-- hljs styles -->\r\n\
  66. <style>.hljs{display:block;overflow-x:auto;padding:0.5em;background:#fff;color:#000;-webkit-text-size-adjust:none}.hljs-comment{color:#006a00}.hljs-keyword{color:#02E}.hljs-literal,.nginx .hljs-title{color:#aa0d91}.method,.hljs-list .hljs-title,.hljs-tag .hljs-title,.setting .hljs-value,.hljs-winutils,.tex .hljs-command,.http .hljs-title,.hljs-request,.hljs-status,.hljs-name{color:#008}.hljs-envvar,.tex .hljs-special{color:#660}.hljs-string{color:#c41a16}.hljs-tag .hljs-value,.hljs-cdata,.hljs-filter .hljs-argument,.hljs-attr_selector,.apache .hljs-cbracket,.hljs-date,.hljs-regexp{color:#080}.hljs-sub .hljs-identifier,.hljs-pi,.hljs-tag,.hljs-tag .hljs-keyword,.hljs-decorator,.ini .hljs-title,.hljs-shebang,.hljs-prompt,.hljs-hexcolor,.hljs-rule .hljs-value,.hljs-symbol,.hljs-symbol .hljs-string,.hljs-number,.css .hljs-function,.hljs-function .hljs-title,.coffeescript .hljs-attribute{color:#A0C}.hljs-function .hljs-title{font-weight:bold;color:#000}.hljs-class .hljs-title,.smalltalk .hljs-class,.hljs-type,.hljs-typename,.hljs-tag .hljs-attribute,.hljs-doctype,.hljs-class .hljs-id,.hljs-built_in,.setting,.hljs-params,.clojure .hljs-attribute{color:#5c2699}.hljs-variable{color:#3f6e74}.css .hljs-tag,.hljs-rule .hljs-property,.hljs-pseudo,.hljs-subst{color:#000}.css .hljs-class,.css .hljs-id{color:#9b703f}.hljs-value .hljs-important{color:#ff7700;font-weight:bold}.hljs-rule .hljs-keyword{color:#c5af75}.hljs-annotation,.apache .hljs-sqbracket,.nginx .hljs-built_in{color:#9b859d}.hljs-preprocessor,.hljs-preprocessor *,.hljs-pragma{color:#643820}.tex .hljs-formula{background-color:#eee;font-style:italic}.diff .hljs-header,.hljs-chunk{color:#808080;font-weight:bold}.diff .hljs-change{background-color:#bccff9}.hljs-addition{background-color:#baeeba}.hljs-deletion{background-color:#ffc8bd}.hljs-comment .hljs-doctag{font-weight:bold}.method .hljs-id{color:#000}</style>\r\n\
  67. \
  68. ';
  69. // This code is placed at the beginning of the body before the Markdeep code.
  70. // $ (DOCUMENT_BODY_PREFIX) is everything in the body of PreviewBlogPage.htm up to
  71. // $ (ARTICLE_HTML_CODE).
  72. DocumentBodyPrefix='\
  73. \r\n\
  74. \
  75. <!-- MARKDEEP_BEGIN -->\
  76. <pre class="markdeep">\
  77. ';
  78. // This code is placed at the end of the body after the Markdeep code.
  79. // $ (DOCUMENT_BODY_SUFFIX) is everything in the body of PreviewBlogPage.htm after
  80. // $ (ARTICLE_HTML_CODE).
  81. DocumentBodySuffix='\
  82. </pre>\
  83. <!-- MARKDEEP_END -->\
  84. <div>Document &lt;body&gt; code:<br/>\
  85. <textarea cols="40" rows="10" id="BodyDisplayBox"></textarea></div>\
  86. \r\n\
  87. \
  88. ';
  89. // Get the full Markdeep code from the .md.html file without the script invocation
  90. MarkdeepCode=nodeToMarkdeepSource(document.body);
  91. MarkdeepCode=MarkdeepCode.slice(0,MarkdeepCode.lastIndexOf("<script"));
  92. // Bring it into a form where it can be pasted into an HTML document
  93. SanitizedMarkdeepCode=escapeHTMLEntities(MarkdeepCode);
  94. // Surround it by the prefix and suffix code and set that as body code
  95. document.body.innerHTML=DocumentBodyPrefix+SanitizedMarkdeepCode+DocumentBodySuffix;
  96. // Setting head attributes
  97. // Setting body attributes