merge all pages from each book to one html file

This commit is contained in:
Bachir Soussi Chiadmi
2017-04-22 18:53:12 +02:00
parent 6366f20dcb
commit 909bfc1d6e
5 changed files with 145 additions and 134 deletions
+51 -52
View File
@@ -19,23 +19,23 @@ import pypandoc
import json
import re
BOOKS_SRC = 'book-src'
BUILD_d = "build"
CUR_PATH = os.path.dirname(os.path.abspath(__file__))
_BOOKS_SRC = 'book-src'
_BUILD_d = "build"
_TOC = []
# CUR_PATH = os.path.dirname(os.path.abspath(__file__))
def main():
print("Building books")
if not os.path.isdir(BUILD_d):
os.mkdir(BUILD_d)
if not os.path.isdir(_BUILD_d):
os.mkdir(_BUILD_d)
# loop through books sources
for book in os.listdir(BOOKS_SRC):
if os.path.isdir(os.path.join(BOOKS_SRC, book)):
for book in os.listdir(_BOOKS_SRC):
if os.path.isdir(os.path.join(_BOOKS_SRC, book)):
# print(book)
parse_book(book)
with open(BUILD_d+'/toc.json', 'w') as fp:
with open(_BUILD_d+'/toc.json', 'w') as fp:
json.dump(_TOC, fp, ensure_ascii=False, indent="\t")
@@ -46,7 +46,7 @@ def parse_book(book):
print("- - -")
# table of content (ordered list of markdown files)
sum_p = os.path.join(BOOKS_SRC, book, "SUMMARY.md")
sum_p = os.path.join(_BOOKS_SRC, book, "SUMMARY.md")
if not os.path.isfile(sum_p):
print("No summary file, can't generate html")
return
@@ -84,71 +84,70 @@ def parse_summary(ul, toc):
return toc
def generate_html(book, toc, book_name):
# build destination
book_build_d = os.path.join(BUILD_d,book_name)
# build directory destination
book_build_d = os.path.join(_BUILD_d,book_name)
if os.path.isdir(book_build_d):
shutil.rmtree(book_build_d, ignore_errors=True)
os.mkdir(book_build_d)
book_toc = {
'label':book_name,
'pages':[]
}
# main markdown book file where all pages will be merge
book_md_f = os.path.join(book_build_d,book_name+'.md')
for p in toc:
# print(toc[p]['file'])
# generate html with pandoc
# files
md_f = toc[p]['file']
in_f = os.path.join(BOOKS_SRC, book, md_f)
in_f = os.path.join(_BOOKS_SRC, book, toc[p]['file'])
if not os.path.isfile(in_f):
print("Source path is not a file, can't generate html : "+in_f)
continue
# print('in_f : '+in_f)
html_f = md_f.replace('.md', '.html')
html_f = html_f.replace('README', 'index')
html_f = html_f.replace('/', '-')
out_f = os.path.join(book_build_d,html_f)
# print('out_f : '+out_f)
md_f = open(in_f, 'r')
with open(book_md_f, 'a') as fp:
fp.write(md_f.read())
# pandoc options
# filters = []
pdoc_args = ['-s',
'--mathjax',
'--smart',
'--css=../../assets/fonts/amiri/amiri.css',
'--css=../../assets/css/dist/main.css',
'--include-before-body=templates/top.tpl.html',
'--include-after-body=templates/bot.tpl.html',
'--include-after-body=assets/lib/jquery.min.js',
'--include-after-body=assets/js/setup.js',
'--include-after-body=assets/js/html2print.js',
'--include-after-body=assets/js/script.js',
'--include-after-body=templates/end.tpl.html']
# generate html with pandoc
# pandoc command line
# print(pypandoc.get_pandoc_version())
output = pypandoc.convert_file(in_f,
to='html5',
format='md',
extra_args=pdoc_args,
# filters=filters,
outputfile=out_f)
# create the html file name
html_f = book_md_f.replace('.md', '.html')
# print('out_f : '+out_f)
# save reference in railway
book_toc['pages'].append({'label':toc[p]['label'], 'file':html_f})
# pandoc options
# filters = []
pdoc_args = ['-s',
'--mathjax',
'--smart',
'--css=../../assets/fonts/amiri/amiri.css',
'--css=../../assets/css/dist/main.css',
'--include-before-body=templates/top.tpl.html',
'--include-after-body=templates/bot.tpl.html',
'--include-after-body=assets/lib/jquery.min.js',
'--include-after-body=assets/js/setup.js',
'--include-after-body=assets/js/html2print.js',
'--include-after-body=assets/js/script.js',
'--include-after-body=templates/end.tpl.html']
# pandoc command line
# print(pypandoc.get_pandoc_version())
output = pypandoc.convert_file(book_md_f,
to='html5',
format='md',
extra_args=pdoc_args,
# filters=filters,
outputfile=html_f)
book_toc = {
'label':book_name,
'file':html_f
}
global _TOC
_TOC.append(book_toc)
# save railway as json file
# with open(book_build_d+'/railway.json', 'w') as fp:
# json.dump(railway, fp, ensure_ascii=False, indent="\t")
if __name__ == "__main__":