/SeriesMgr/helptxt/compile.py

https://github.com/esitarski/CrossMgr
Python | 121 lines | 101 code | 16 blank | 4 comment | 28 complexity | 3907c229867bb5509abdf2a0e25bfcfa MD5 | raw file
  1. import markdown
  2. import glob
  3. import os
  4. import re
  5. import io
  6. import base64
  7. import zipfile
  8. import shutil
  9. import datetime
  10. from io import StringIO
  11. from contextlib import contextmanager
  12. HtmlDocFolder = 'SeriesMgrHtmlDoc'
  13. @contextmanager
  14. def working_directory(directory):
  15. original_directory = os.getcwd()
  16. try:
  17. os.chdir(directory)
  18. yield directory
  19. finally:
  20. os.chdir(original_directory)
  21. def fileOlderThan( srcFile, transFile ):
  22. try:
  23. return os.path.getmtime(srcFile) <= os.path.getmtime(transFile)
  24. except:
  25. return False
  26. reImage = re.compile( r'src="\.\/images\/([^"]+)"' )
  27. def InlineImages( html ):
  28. while 1:
  29. match = reImage.search( html )
  30. if not match:
  31. break
  32. fname = match.group(1)
  33. with io.open(os.path.join('images',fname), 'rb') as f:
  34. b64 = base64.b64encode( f.read() )
  35. sReplace = 'src="data:image/{};base64,{}'.format(
  36. os.path.splitext(fname)[1][1:],
  37. b64,
  38. )
  39. html = html.replace( match.group(0), sReplace )
  40. return html
  41. def CompileHelp( dir = '.' ):
  42. with working_directory( dir ):
  43. # Check if any of the help files need rebuilding.
  44. doNothing = True
  45. for fname in glob.glob("./*.txt"):
  46. fbase = os.path.splitext(os.path.basename(fname))[0]
  47. fhtml = os.path.join( '..', HtmlDocFolder, fbase + '.html' )
  48. if not fileOlderThan(fhtml, fname):
  49. doNothing = False
  50. break
  51. if doNothing:
  52. print( 'Nothing to do.' )
  53. return
  54. md = markdown.Markdown(
  55. extensions=['toc', 'tables', 'sane_lists'],
  56. #safe_mode='escape',
  57. output_format='html5'
  58. )
  59. with io.open('markdown.css', 'r') as f:
  60. style = f.read()
  61. with io.open('prolog.html', 'r') as f:
  62. prolog = f.read()
  63. prolog = prolog.replace( '<<<style>>>', style, 1 )
  64. del style
  65. with io.open('epilog.html', 'r') as f:
  66. epilog = f.read().replace('YYYY','{}'.format(datetime.datetime.now().year))
  67. contentDiv = '<div class="content">'
  68. with io.open('Links.md', 'r') as f:
  69. links = f.read()
  70. for fname in glob.glob("./*.txt"):
  71. print( fname, '...' )
  72. with io.open(fname, 'r') as f:
  73. input = StringIO()
  74. input.write( links )
  75. input.write( f.read() )
  76. htmlSave = html = md.convert( input.getvalue() )
  77. input.close()
  78. html = html.replace( '</div>', '</div>' + '\n' + contentDiv, 1 )
  79. if htmlSave == html:
  80. html = contentDiv + '\n' + html
  81. html += '\n</div>\n'
  82. html = InlineImages( html )
  83. with io.open( os.path.splitext(fname)[0] + '.html', 'w' ) as f:
  84. f.write( prolog )
  85. f.write( html )
  86. f.write( epilog )
  87. md.reset()
  88. # Put all the html files into a zipfile.
  89. ZipFileName = 'SeriesMgrDocHtml.zip'
  90. zf = zipfile.ZipFile( ZipFileName, 'w' )
  91. for fname in glob.glob("./*.html"):
  92. if not ('prolog' in fname or 'epilog' in fname):
  93. zf.write( fname )
  94. zf.close()
  95. # Copy all the files into the htmldoc directory.
  96. htmldocdir = os.path.join('..', HtmlDocFolder)
  97. if not os.path.exists( htmldocdir ):
  98. os.mkdir( htmldocdir )
  99. for fname in glob.glob( os.path.join(htmldocdir, '*.html') ):
  100. os.remove( fname )
  101. for fname in glob.glob("./*.html"):
  102. if not ('prolog' in fname or 'epilog' in fname):
  103. shutil.move( fname, htmldocdir )
  104. if __name__ == '__main__':
  105. CompileHelp()