BuildMarkdeepUtility.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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=[
  17. ("$(FULL_DOCUMENT_HEAD)",FullDocumentHead),
  18. ("$(DOCUMENT_BODY_PREFIX)",DocumentBodyPrefix),
  19. ("$(DOCUMENT_BODY_SUFFIX)",DocumentBodySuffix)
  20. ];
  21. for Macro,Replacement in ReplacementList:
  22. FullPrepareHTMLCode=FullPrepareHTMLCode.replace(Macro,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(1)+"\",\""+Match.group(2)+"\");\r\n";
  28. open("PrepareHTML.full.js","wb").write(FullPrepareHTMLCode.encode("utf-8"));
  29. # Concatenate all the scripts together
  30. SourceFileList=[
  31. "PrepareHTML.full.js",
  32. "SetMarkdeepMode.js",
  33. "markdeep.min.js",
  34. "DisplayMarkdeepOutput.js",
  35. "InvokeMathJax.js"
  36. ];
  37. OutputCode="\r\n\r\n".join(["// "+SourceFile+"\r\n\r\n"+open(SourceFile,"rb").read().decode("utf-8") for SourceFile in SourceFileList]);
  38. OutputFile=open("MarkdeepUtility.js","wb");
  39. OutputFile.write(OutputCode.encode("utf-8"));
  40. OutputFile.close();
  41. print("Done.");