re_integrated html2print boilerplate from osp

This commit is contained in:
Bachir Soussi Chiadmi
2017-04-22 16:56:29 +02:00
parent b68f53c0b0
commit 6366f20dcb
26 changed files with 1293 additions and 565 deletions
+51 -22
View File
@@ -1,6 +1,15 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Author: Bachir Soussi Chiadmi <bach>
# @Date: 27-03-2017
# @Email: bachir@figureslibres.io
# @Last modified by: bach
# @Last modified time: 21-04-2017
# @License: GPL-V3
import sys, os
import shutil
import markdown
@@ -10,23 +19,25 @@ import pypandoc
import json
import re
# import json
# import getopt
# import urllib
BOOKS_SRC = 'book-src'
BUILD_d = "build"
CUR_PATH = os.path.dirname(os.path.abspath(__file__))
_TOC = []
def main():
print("Building books")
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)):
# print(book)
parse_book(book)
with open(BUILD_d+'/toc.json', 'w') as fp:
json.dump(_TOC, fp, ensure_ascii=False, indent="\t")
def parse_book(book):
book_name = book.replace('.git', '')
@@ -34,12 +45,6 @@ def parse_book(book):
print(book_name)
print("- - -")
# build 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)
# table of content (ordered list of markdown files)
sum_p = os.path.join(BOOKS_SRC, book, "SUMMARY.md")
if not os.path.isfile(sum_p):
@@ -49,14 +54,18 @@ def parse_book(book):
sum_f = open(sum_p)
sum_str = sum_f.read()
# print(sum_str)
# convert md to html
sum_html = markdown.markdown(sum_str)
# print(sum_html)
# create dom from html string (as it will be parsable)
sum_dom = BeautifulSoup(sum_html, 'html.parser')
# print(sum_dom)
# parse html dom to get file list in the right order
toc = parse_summary(sum_dom.ul, {})
# print(toc)
generate_html(book, toc, book_build_d)
# generate final html build for html2print
generate_html(book, toc, book_name)
def parse_summary(ul, toc):
i=0
@@ -75,21 +84,37 @@ def parse_summary(ul, toc):
return toc
def generate_html(book, toc, book_build_d):
railway = []
def generate_html(book, toc, book_name):
# build 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':[]
}
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)
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('/', '-')
in_f = os.path.join(BOOKS_SRC, book, md_f)
# print(in_f)
out_f = os.path.join(book_build_d,html_f)
# print(out_f)
# print('out_f : '+out_f)
# pandoc options
# filters = []
@@ -97,11 +122,13 @@ def generate_html(book, toc, book_build_d):
pdoc_args = ['-s',
'--mathjax',
'--smart',
'--css=assets/fonts/amiri/amiri.css',
'--css=assets/css/dist/styles.css',
'--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/js/jquery.min.js',
'--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']
@@ -115,11 +142,13 @@ def generate_html(book, toc, book_build_d):
outputfile=out_f)
# save reference in railway
railway.append({'label':toc[p]['label'], 'file':html_f})
book_toc['pages'].append({'label':toc[p]['label'], '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")
# with open(book_build_d+'/railway.json', 'w') as fp:
# json.dump(railway, fp, ensure_ascii=False, indent="\t")
if __name__ == "__main__":
View File
+8
View File
@@ -1,5 +1,13 @@
#!/bin/bash
# @Author: Bachir Soussi Chiadmi <bach>
# @Date: 26-03-2017
# @Email: bachir@figureslibres.io
# @Last modified by: bach
# @Last modified time: 21-04-2017
# @License: GPL-V3
echo "Sync books"
# activate credential cache