build.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. # CUR_PATH = os.path.dirname(os.path.abspath(__file__))
  20. print("Building book")
  21. def main():
  22. # clean build directory
  23. if os.path.isdir(_BUILD_d):
  24. shutil.rmtree(_BUILD_d, ignore_errors=True)
  25. os.mkdir(_BUILD_d)
  26. parse_book(_BOOKS_SRC)
  27. def parse_book(book):
  28. # book_name = book.replace('.git', '')
  29. # print("- - -")
  30. print("Parse book")
  31. # print("- - -")
  32. # table of content (ordered list of markdown files)
  33. sum_p = os.path.join(_BOOKS_SRC, "SUMMARY.md")
  34. if not os.path.isfile(sum_p):
  35. print("No summary file, can't generate html")
  36. return
  37. sum_f = open(sum_p)
  38. sum_str = sum_f.read()
  39. # print(sum_str)
  40. # convert md to html
  41. sum_html = markdown.markdown(sum_str)
  42. # print(sum_html)
  43. # create dom from html string (as it will be parsable)
  44. sum_dom = BeautifulSoup(sum_html, 'html.parser')
  45. # print(sum_dom)
  46. # parse html dom to get file list in the right order
  47. toc = parse_summary(sum_dom.ul, {})
  48. # print(toc)
  49. # generate final html build for html2print
  50. generate_html(book, toc)
  51. def parse_summary(ul, toc):
  52. print("Parse summary")
  53. i=0
  54. for li in ul.find_all('li',recursive=False):
  55. # print('li')
  56. for a in li.find_all('a',recursive=False):
  57. # print(a.get_text(strip=True))
  58. # print(a['href'])
  59. href = a['href']
  60. href = re.sub(r'^/', '', href)
  61. toc[i] = {
  62. 'label':a.get_text(strip=True),
  63. 'file':href
  64. }
  65. i = i+1
  66. return toc
  67. def generate_html(book, toc):
  68. print("Generate html")
  69. #
  70. # create main html dom from template
  71. template_f = open("templates/main.tpl.html", "r")
  72. template_html = template_f.read()
  73. template_dom = BeautifulSoup(template_html, 'html.parser')
  74. # replace title
  75. # template_dom.html.head.title.contents[0].replaceWith(book_name)
  76. # get story div
  77. story_dom = template_dom.find('div', {"id":"my-story"})
  78. #
  79. # loop through pages to convert them to html and add it to main html file
  80. # book_build_d_pages = os.path.join(_BUILD_d,'pages')
  81. # os.mkdir(book_build_d_pages)
  82. for p in toc:
  83. print(toc[p]['file'])
  84. pagename = toc[p]['file'].replace('.md', '')
  85. # files
  86. in_f = os.path.join(_BOOKS_SRC, toc[p]['file'])
  87. if not os.path.isfile(in_f):
  88. print("Source path is not a file, can't generate html : "+in_f)
  89. continue
  90. # print('in_f : '+in_f)
  91. # out_f = os.path.join(book_build_d_pages, toc[p]['file'].replace('/', '-').replace('.md', '.html'))
  92. # print('out_f : '+out_f)
  93. pdoc_args = ['--mathjax',
  94. '--smart']
  95. output = pypandoc.convert_file(in_f,
  96. to='html5',
  97. format='md',
  98. extra_args=pdoc_args)
  99. # filters=filters,
  100. # outputfile=out_f)
  101. # append html story page to template_dom
  102. story_page = BeautifulSoup('<div class="story-page" id="'+pagename+'"></div>')
  103. story_page.div.append(BeautifulSoup(output))
  104. story_dom.append(story_page)
  105. # create main html file from filled template html dom
  106. book_html_f = os.path.join(_BUILD_d,'stories.html')
  107. with open(book_html_f, 'w') as fp:
  108. fp.write(template_dom.prettify())
  109. if __name__ == "__main__":
  110. main()