Browse Source

handled images in build

Bachir Soussi Chiadmi 7 years ago
parent
commit
17d56e1f73
1 changed files with 34 additions and 11 deletions
  1. 34 11
      bin/build.py

+ 34 - 11
bin/build.py

@@ -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: