build.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # @Author: Bachir Soussi Chiadmi <bach>
  4. # @Date: 27-03-2017
  5. # @Email: bachir@figureslibres.io
  6. # @Last modified by: bach
  7. # @Last modified time: 21-04-2017
  8. # @License: GPL-V3
  9. import sys, os
  10. import shutil
  11. import markdown
  12. # import mistune
  13. from bs4 import BeautifulSoup
  14. import pypandoc
  15. import json
  16. import re
  17. _BOOKS_SRC = 'book-src'
  18. _BUILD_d = "build"
  19. _TOC = []
  20. # CUR_PATH = os.path.dirname(os.path.abspath(__file__))
  21. def main():
  22. print("Building books")
  23. if not os.path.isdir(_BUILD_d):
  24. os.mkdir(_BUILD_d)
  25. # loop through books sources
  26. for book in os.listdir(_BOOKS_SRC):
  27. if os.path.isdir(os.path.join(_BOOKS_SRC, book)):
  28. # print(book)
  29. parse_book(book)
  30. with open(_BUILD_d+'/toc.json', 'w') as fp:
  31. json.dump(_TOC, fp, ensure_ascii=False, indent="\t")
  32. def parse_book(book):
  33. book_name = book.replace('.git', '')
  34. print("- - -")
  35. print(book_name)
  36. print("- - -")
  37. # table of content (ordered list of markdown files)
  38. sum_p = os.path.join(_BOOKS_SRC, book, "SUMMARY.md")
  39. if not os.path.isfile(sum_p):
  40. print("No summary file, can't generate html")
  41. return
  42. sum_f = open(sum_p)
  43. sum_str = sum_f.read()
  44. # print(sum_str)
  45. # convert md to html
  46. sum_html = markdown.markdown(sum_str)
  47. # print(sum_html)
  48. # create dom from html string (as it will be parsable)
  49. sum_dom = BeautifulSoup(sum_html, 'html.parser')
  50. # print(sum_dom)
  51. # parse html dom to get file list in the right order
  52. toc = parse_summary(sum_dom.ul, {})
  53. # print(toc)
  54. # generate final html build for html2print
  55. generate_html(book, toc, book_name)
  56. def parse_summary(ul, toc):
  57. i=0
  58. for li in ul.find_all('li',recursive=False):
  59. # print('li')
  60. for a in li.find_all('a',recursive=False):
  61. # print(a.get_text(strip=True))
  62. # print(a['href'])
  63. href = a['href']
  64. href = re.sub(r'^/', '', href)
  65. toc[i] = {
  66. 'label':a.get_text(strip=True),
  67. 'file':href
  68. }
  69. i = i+1
  70. return toc
  71. def generate_html(book, toc, book_name):
  72. # build directory destination
  73. book_build_d = os.path.join(_BUILD_d,book_name)
  74. if os.path.isdir(book_build_d):
  75. shutil.rmtree(book_build_d, ignore_errors=True)
  76. os.mkdir(book_build_d)
  77. #
  78. # create main html dom from template
  79. template_f = open("templates/main.tpl.html", "r")
  80. template_html = template_f.read()
  81. template_dom = BeautifulSoup(template_html, 'html.parser')
  82. # replace title
  83. template_dom.html.head.title.contents[0].replaceWith(book_name)
  84. # get story div
  85. story_dom = template_dom.find('div', {"id":"my-story"})
  86. #
  87. # loop through pages to convert them to html and add it to main html file
  88. book_build_d_pages = os.path.join(book_build_d,'pages')
  89. os.mkdir(book_build_d_pages)
  90. for p in toc:
  91. # print(toc[p]['file'])
  92. # files
  93. in_f = os.path.join(_BOOKS_SRC, book, toc[p]['file'])
  94. if not os.path.isfile(in_f):
  95. print("Source path is not a file, can't generate html : "+in_f)
  96. continue
  97. # print('in_f : '+in_f)
  98. # out_f = os.path.join(book_build_d_pages, toc[p]['file'].replace('/', '-').replace('.md', '.html'))
  99. # print('out_f : '+out_f)
  100. pdoc_args = ['--mathjax',
  101. '--smart']
  102. output = pypandoc.convert_file(in_f,
  103. to='html5',
  104. format='md',
  105. extra_args=pdoc_args)
  106. # filters=filters,
  107. # outputfile=out_f)
  108. # append html story page to template_dom
  109. story_page = BeautifulSoup('<div class="story-page"></div>')
  110. story_page.div.append(BeautifulSoup(output))
  111. story_dom.append(story_page)
  112. # create main html file from filled template html dom
  113. book_html_f = os.path.join(book_build_d,book_name+'.html')
  114. with open(book_html_f, 'w') as fp:
  115. fp.write(template_dom.prettify())
  116. book_toc = {
  117. 'label':book_name,
  118. 'file':book_html_f
  119. }
  120. global _TOC
  121. _TOC.append(book_toc)
  122. if __name__ == "__main__":
  123. main()