handled images in build
This commit is contained in:
+34
-11
@@ -10,8 +10,7 @@
|
|||||||
# @License: GPL-V3
|
# @License: GPL-V3
|
||||||
|
|
||||||
|
|
||||||
import sys, os
|
import sys, os, shutil
|
||||||
import shutil
|
|
||||||
import markdown
|
import markdown
|
||||||
# import mistune
|
# import mistune
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
@@ -19,7 +18,7 @@ import pypandoc
|
|||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
|
||||||
_BOOKS_SRC = 'book-src'
|
_BOOK_SRC = 'book-src'
|
||||||
_BUILD_d = "build"
|
_BUILD_d = "build"
|
||||||
# CUR_PATH = os.path.dirname(os.path.abspath(__file__))
|
# CUR_PATH = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
@@ -30,7 +29,7 @@ def main():
|
|||||||
shutil.rmtree(_BUILD_d, ignore_errors=True)
|
shutil.rmtree(_BUILD_d, ignore_errors=True)
|
||||||
os.mkdir(_BUILD_d)
|
os.mkdir(_BUILD_d)
|
||||||
|
|
||||||
parse_book(_BOOKS_SRC)
|
parse_book(_BOOK_SRC)
|
||||||
|
|
||||||
|
|
||||||
def parse_book(book):
|
def parse_book(book):
|
||||||
@@ -40,7 +39,7 @@ def parse_book(book):
|
|||||||
# print("- - -")
|
# print("- - -")
|
||||||
|
|
||||||
# table of content (ordered list of markdown files)
|
# 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):
|
if not os.path.isfile(sum_p):
|
||||||
print("No summary file, can't generate html")
|
print("No summary file, can't generate html")
|
||||||
return
|
return
|
||||||
@@ -102,7 +101,7 @@ def generate_html(book, toc):
|
|||||||
pagename = toc[p]['file'].replace('.md', '')
|
pagename = toc[p]['file'].replace('.md', '')
|
||||||
|
|
||||||
# files
|
# 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):
|
if not os.path.isfile(in_f):
|
||||||
print("Source path is not a file, can't generate html : "+in_f)
|
print("Source path is not a file, can't generate html : "+in_f)
|
||||||
continue
|
continue
|
||||||
@@ -114,18 +113,42 @@ def generate_html(book, toc):
|
|||||||
pdoc_args = ['--mathjax',
|
pdoc_args = ['--mathjax',
|
||||||
'--smart']
|
'--smart']
|
||||||
|
|
||||||
|
pdoc_filters = []
|
||||||
|
|
||||||
output = pypandoc.convert_file(in_f,
|
output = pypandoc.convert_file(in_f,
|
||||||
to='html5',
|
to='html5',
|
||||||
format='md',
|
format='markdown+header_attributes+link_attributes+bracketed_spans',
|
||||||
extra_args=pdoc_args)
|
extra_args=pdoc_args,
|
||||||
# filters=filters,
|
filters=pdoc_filters)
|
||||||
# outputfile=out_f)
|
# outputfile=out_f)
|
||||||
|
output_dom = BeautifulSoup(output, 'html.parser')
|
||||||
|
|
||||||
|
|
||||||
# append html story page to template_dom
|
# append html story page to template_dom
|
||||||
story_page = BeautifulSoup('<div class="story-page" id="'+pagename+'"></div>')
|
story_page = BeautifulSoup('<div class="story-page" id="'+pagename+'"></div>', 'html.parser')
|
||||||
story_page.div.append(BeautifulSoup(output))
|
story_page.div.append(output_dom)
|
||||||
story_dom.append(story_page)
|
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
|
# create main html file from filled template html dom
|
||||||
book_html_f = os.path.join(_BUILD_d,'stories.html')
|
book_html_f = os.path.join(_BUILD_d,'stories.html')
|
||||||
with open(book_html_f, 'w') as fp:
|
with open(book_html_f, 'w') as fp:
|
||||||
|
|||||||
Reference in New Issue
Block a user