build.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. # main markdown book file where all pages will be merge
  78. book_md_f = os.path.join(book_build_d,book_name+'.md')
  79. for p in toc:
  80. # print(toc[p]['file'])
  81. # files
  82. in_f = os.path.join(_BOOKS_SRC, book, toc[p]['file'])
  83. if not os.path.isfile(in_f):
  84. print("Source path is not a file, can't generate html : "+in_f)
  85. continue
  86. # print('in_f : '+in_f)
  87. md_f = open(in_f, 'r')
  88. with open(book_md_f, 'a') as fp:
  89. fp.write(md_f.read())
  90. # generate html with pandoc
  91. # create the html file name
  92. html_f = book_md_f.replace('.md', '.html')
  93. # print('out_f : '+out_f)
  94. # pandoc options
  95. # filters = []
  96. pdoc_args = ['-s',
  97. '--mathjax',
  98. '--smart',
  99. '--css=../../assets/fonts/amiri/amiri.css',
  100. '--css=../../assets/css/dist/main.css',
  101. '--include-before-body=templates/top.tpl.html',
  102. '--include-after-body=templates/bot.tpl.html',
  103. '--include-after-body=assets/lib/jquery.min.js',
  104. '--include-after-body=assets/js/setup.js',
  105. '--include-after-body=assets/js/html2print.js',
  106. '--include-after-body=assets/js/script.js',
  107. '--include-after-body=templates/end.tpl.html']
  108. # pandoc command line
  109. # print(pypandoc.get_pandoc_version())
  110. output = pypandoc.convert_file(book_md_f,
  111. to='html5',
  112. format='md',
  113. extra_args=pdoc_args,
  114. # filters=filters,
  115. outputfile=html_f)
  116. book_toc = {
  117. 'label':book_name,
  118. 'file':html_f
  119. }
  120. global _TOC
  121. _TOC.append(book_toc)
  122. if __name__ == "__main__":
  123. main()