From 9e1ab238c31046efc9bacc69424d345460e5505b Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Tue, 30 May 2017 11:31:37 +0200 Subject: [PATCH] md2html is now compiling md contents to index.html --- classes/md2html.py | 73 +++++++++++++++++++-- templates/main.tpl.html | 39 +++++++++++ templates/newproject/assets/css/layout.scss | 4 +- templates/newproject/assets/js/setup.js | 2 +- 4 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 templates/main.tpl.html diff --git a/classes/md2html.py b/classes/md2html.py index bbc370a..067c837 100644 --- a/classes/md2html.py +++ b/classes/md2html.py @@ -2,13 +2,16 @@ # -*- coding: utf-8 -*- import os +import re from PyQt5.QtCore import QFileSystemWatcher import json +from bs4 import BeautifulSoup +import pypandoc class Compiler(): - def __init__(self,parent): - self.parent = parent + def __init__(self,core): + self.core = core self.initWatching() self.compileContents() @@ -27,13 +30,13 @@ class Compiler(): pass def refreshPaths(self): - jsonfilepath = os.path.join(self.parent.cwd,'.config/summary.json') + jsonfilepath = os.path.join(self.core.cwd,'.config/summary.json') sum_json = open(jsonfilepath).read() self.sum = json.loads(sum_json) - self.paths = [os.path.join(self.parent.cwd,'contents')] + self.paths = [os.path.join(self.core.cwd,'contents')] for item in self.sum: - self.paths.append(os.path.join(self.parent.cwd,'contents',item['file'])) + self.paths.append(os.path.join(self.core.cwd,'contents',item['file'])) def reload(self): self.fs_watcher.removePaths(self.paths) @@ -43,3 +46,63 @@ class Compiler(): def compileContents(self): print('Compiling md') + + # create main html dom from template + template_f = open(os.path.join(self.core.appcwd,"templates/main.tpl.html"), "r") + template_html = template_f.read() + template_dom = BeautifulSoup(template_html, 'html.parser') + + # get story div + story_dom = template_dom.find('div', {"id":"flow-main"}) + + pi = 0 + for p in self.sum: + # print(toc[p]) + pagename = p['title'] + pageid = re.sub('[^a-z0-9]+', '-', pagename.lower()) + print(pageid) + + # files + in_f = os.path.join(self.core.cwd, "contents", 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) + + pdoc_args = ['--mathjax', + '--smart'] + + pdoc_filters = [] + + output = pypandoc.convert_file(in_f, + to='html5', + format='markdown+header_attributes+link_attributes+bracketed_spans', + extra_args=pdoc_args, + filters=pdoc_filters) + + output_dom = BeautifulSoup(output, 'html.parser') + + # append html story page to template_dom + story_page = BeautifulSoup( + '
', + 'html.parser' + ) + story_page.div.append(output_dom) + story_dom.append(story_page) + + + pi = pi+1 + + # create main html file from filled template html dom + book_html_f = os.path.join(self.core.cwd,'index.html') + with open(book_html_f, 'w') as fp: + fp.write(template_dom.prettify()) + + + + + + + + + # diff --git a/templates/main.tpl.html b/templates/main.tpl.html new file mode 100644 index 0000000..18afe10 --- /dev/null +++ b/templates/main.tpl.html @@ -0,0 +1,39 @@ + + + + + Cascade + + + + + + +
+ +
+
+
+
+ +
+
+
+ +
+ + +
+
+ +
+ +
+ + + + + + + + diff --git a/templates/newproject/assets/css/layout.scss b/templates/newproject/assets/css/layout.scss index e6dda57..c6e8344 100644 --- a/templates/newproject/assets/css/layout.scss +++ b/templates/newproject/assets/css/layout.scss @@ -47,9 +47,9 @@ // .body:after { bottom: $line-height * -3; } /*gauche*/ -.paper:nth-child(odd) .body:before { content: "Tous, des sang-mélés"; } +.paper:nth-child(odd) .body:before { content: "Cascade, default header"; } /*droite*/ -.paper:nth-child(even) .body:before { content: "Tous, des sang-mélés"; } +.paper:nth-child(even) .body:before { content: "Cascade, default header"; } .body:after { content: counter(folio); z-index: 499;} diff --git a/templates/newproject/assets/js/setup.js b/templates/newproject/assets/js/setup.js index 6b31209..78d53e7 100644 --- a/templates/newproject/assets/js/setup.js +++ b/templates/newproject/assets/js/setup.js @@ -1 +1 @@ -nb_page = 100; +nb_page = 2;