md2html.py 3.3 KB

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