BuildMarkdeepUtility.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import re
  2. if (__name__ == "__main__"):
  3. # Assemble the script which embeds the Markdeep page into the preview blog
  4. PreviewBlogPage = open("PreviewBlogPage.htm", "rb").read().decode("utf-8")
  5. HeadMatch = re.search("<head(.*?)>(.*?)</head>", PreviewBlogPage, re.DOTALL)
  6. HeadAttributes = HeadMatch.group(1)
  7. FullDocumentHead = HeadMatch.group(2)
  8. BodyMatch = re.search("<body(.*?)>(.*?)</body>", PreviewBlogPage, re.DOTALL)
  9. BodyAttributes = BodyMatch.group(1)
  10. FullPreviewBody = BodyMatch.group(2)
  11. ArticleHTMLCodeMacro = "$(ARTICLE_HTML_CODE)"
  12. iArticleHTMLCodeMacro = FullPreviewBody.find(ArticleHTMLCodeMacro)
  13. DocumentBodyPrefix = FullPreviewBody[0:iArticleHTMLCodeMacro]
  14. DocumentBodySuffix = FullPreviewBody[iArticleHTMLCodeMacro + len(ArticleHTMLCodeMacro):]
  15. FullPrepareHTMLCode = open("PrepareHTML.js", "rb").read().decode("utf-8")
  16. ReplacementList = [("$(FULL_DOCUMENT_HEAD)", FullDocumentHead),
  17. ("$(DOCUMENT_BODY_PREFIX)", DocumentBodyPrefix),
  18. ("$(DOCUMENT_BODY_SUFFIX)", DocumentBodySuffix)]
  19. for Macro, Replacement in ReplacementList:
  20. FullPrepareHTMLCode = FullPrepareHTMLCode.replace(
  21. Macro,
  22. Replacement.replace("\r\n", "\\r\\n\\\r\n").replace("'", "\\'"))
  23. # Generate code which sets body and head attributes appropriately
  24. for Element, AttributeCode in [("head", HeadAttributes), ("body", BodyAttributes)]:
  25. FullPrepareHTMLCode += "\r\n// Setting " + Element + " attributes\r\n"
  26. for Match in re.finditer("(\\w+)=\\\"(.*?)\\\"", AttributeCode):
  27. FullPrepareHTMLCode += "document." + Element + ".setAttribute(\"" + Match.group(
  28. 1) + "\",\"" + Match.group(2) + "\");\r\n"
  29. open("PrepareHTML.full.js", "wb").write(FullPrepareHTMLCode.encode("utf-8"))
  30. # Concatenate all the scripts together
  31. SourceFileList = [
  32. "PrepareHTML.full.js", "SetMarkdeepMode.js", "markdeep.min.js", "DisplayMarkdeepOutput.js",
  33. "InvokeMathJax.js"
  34. ]
  35. OutputCode = "\r\n\r\n".join([
  36. "// " + SourceFile + "\r\n\r\n" + open(SourceFile, "rb").read().decode("utf-8")
  37. for SourceFile in SourceFileList
  38. ])
  39. OutputFile = open("MarkdeepUtility.js", "wb")
  40. OutputFile.write(OutputCode.encode("utf-8"))
  41. OutputFile.close()
  42. print("Done.")