md2html.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import re
  5. from PyQt5.QtCore import QFileSystemWatcher
  6. import json
  7. from bs4 import BeautifulSoup
  8. import pypandoc
  9. class Compiler():
  10. def __init__(self,core):
  11. self.core = core
  12. self.initWatching()
  13. self.compileContents()
  14. def initWatching(self):
  15. self.refreshPaths()
  16. self.fs_watcher = QFileSystemWatcher(self.paths)
  17. # self.fs_watcher.directoryChanged.connect(self.directory_changed)
  18. self.fs_watcher.fileChanged.connect(self.onMdFileChanged)
  19. def onMdFileChanged(self):
  20. print("onMdFileChanged")
  21. try:
  22. self.compileContents()
  23. except Exception as e:
  24. print("Error compiling MD files", e)
  25. pass
  26. def refreshPaths(self):
  27. jsonfilepath = os.path.join(self.core.cwd,'.config/summary.json')
  28. sum_json = open(jsonfilepath).read()
  29. self.sum = json.loads(sum_json)
  30. self.paths = [os.path.join(self.core.cwd,'contents')]
  31. for item in self.sum:
  32. self.paths.append(os.path.join(self.core.cwd,'contents',item['file']))
  33. def reload(self):
  34. self.fs_watcher.removePaths(self.paths)
  35. self.refreshPaths()
  36. self.fs_watcher.addPaths(self.paths)
  37. self.compileContents()
  38. def compileContents(self):
  39. print('Compiling md')
  40. # create main html dom from template
  41. template_f = open(os.path.join(self.core.appcwd,"templates/main.tpl.html"), "r")
  42. template_html = template_f.read()
  43. template_dom = BeautifulSoup(template_html, 'html.parser')
  44. # get story div
  45. story_dom = template_dom.find('div', {"id":"flow-main"})
  46. pi = 0
  47. for p in self.sum:
  48. # print(toc[p])
  49. pagename = p['title']
  50. pageid = re.sub('[^a-z0-9]+', '-', pagename.lower())
  51. print(pageid)
  52. # files
  53. in_f = os.path.join(self.core.cwd, "contents", p['file'])
  54. if not os.path.isfile(in_f):
  55. print("Source path is not a file, can't generate html : "+in_f)
  56. continue
  57. # print('in_f : '+in_f)
  58. pdoc_args = ['--mathjax',
  59. '--smart']
  60. pdoc_filters = []
  61. output = pypandoc.convert_file(in_f,
  62. to='html5',
  63. format='markdown+header_attributes+link_attributes+bracketed_spans',
  64. extra_args=pdoc_args,
  65. filters=pdoc_filters)
  66. output_dom = BeautifulSoup(output, 'html.parser')
  67. # append html story page to template_dom
  68. story_page = BeautifulSoup(
  69. '<div class="story-page story-page-'+str(pi)+'" id="'+pageid+'"></div>',
  70. 'html.parser'
  71. )
  72. story_page.div.append(output_dom)
  73. story_dom.append(story_page)
  74. pi = pi+1
  75. # create main html file from filled template html dom
  76. book_html_f = os.path.join(self.core.cwd,'index.html')
  77. with open(book_html_f, 'w') as fp:
  78. fp.write(template_dom.prettify())
  79. #