handled images in build

This commit is contained in:
Bachir Soussi Chiadmi
2017-05-01 19:20:57 +02:00
parent 72c2b3b5dd
commit 17d56e1f73
+34 -11
View File
@@ -10,8 +10,7 @@
# @License: GPL-V3
import sys, os
import shutil
import sys, os, shutil
import markdown
# import mistune
from bs4 import BeautifulSoup
@@ -19,7 +18,7 @@ import pypandoc
import json
import re
_BOOKS_SRC = 'book-src'
_BOOK_SRC = 'book-src'
_BUILD_d = "build"
# CUR_PATH = os.path.dirname(os.path.abspath(__file__))
@@ -30,7 +29,7 @@ def main():
shutil.rmtree(_BUILD_d, ignore_errors=True)
os.mkdir(_BUILD_d)
parse_book(_BOOKS_SRC)
parse_book(_BOOK_SRC)
def parse_book(book):
@@ -40,7 +39,7 @@ def parse_book(book):
# print("- - -")
# table of content (ordered list of markdown files)
sum_p = os.path.join(_BOOKS_SRC, "SUMMARY.md")
sum_p = os.path.join(_BOOK_SRC, "SUMMARY.md")
if not os.path.isfile(sum_p):
print("No summary file, can't generate html")
return
@@ -102,7 +101,7 @@ def generate_html(book, toc):
pagename = toc[p]['file'].replace('.md', '')
# files
in_f = os.path.join(_BOOKS_SRC, toc[p]['file'])
in_f = os.path.join(_BOOK_SRC, toc[p]['file'])
if not os.path.isfile(in_f):
print("Source path is not a file, can't generate html : "+in_f)
continue
@@ -114,18 +113,42 @@ def generate_html(book, toc):
pdoc_args = ['--mathjax',
'--smart']
pdoc_filters = []
output = pypandoc.convert_file(in_f,
to='html5',
format='md',
extra_args=pdoc_args)
# filters=filters,
format='markdown+header_attributes+link_attributes+bracketed_spans',
extra_args=pdoc_args,
filters=pdoc_filters)
# outputfile=out_f)
output_dom = BeautifulSoup(output, 'html.parser')
# append html story page to template_dom
story_page = BeautifulSoup('<div class="story-page" id="'+pagename+'"></div>')
story_page.div.append(BeautifulSoup(output))
story_page = BeautifulSoup('<div class="story-page" id="'+pagename+'"></div>', 'html.parser')
story_page.div.append(output_dom)
story_dom.append(story_page)
# copy images
for img in output_dom.find_all('img'):
att_src = re.sub(r"^\/", "", img['src'])
src_img = os.path.join(_BOOK_SRC, att_src)
# print('- - '+src_img)
if not os.path.isfile(src_img):
print("Source path is not a file, can't copy img : "+src_img)
continue
dest_img = os.path.join(_BUILD_d, att_src)
# print('- - '+dest_img)
dest_path, dest_file = os.path.split(dest_img)
if not os.path.isdir(dest_path):
os.makedirs(dest_path)
shutil.copyfile(src_img, dest_img)
# create main html file from filled template html dom
book_html_f = os.path.join(_BUILD_d,'stories.html')
with open(book_html_f, 'w') as fp: