Przeglądaj źródła

md2html is now compiling md contents to index.html

Bachir Soussi Chiadmi 7 lat temu
rodzic
commit
9e1ab238c3

+ 68 - 5
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(
+            '<div class="story-page story-page-'+str(pi)+'" id="'+pageid+'"></div>',
+            '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())
+
+
+
+
+
+
+
+
+      #

+ 39 - 0
templates/main.tpl.html

@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<html class="normal" lang="fr">
+  <head>
+    <meta charset="utf-8">
+    <title>Cascade</title>
+
+    <link rel="stylesheet" href="/assets/css/main.css">
+
+  </head>
+  <body>
+    <!-- PAGES -->
+    <div id="pages">
+
+      <div id="master-page" class="paper">
+        <div class="page">
+          <div class="body">
+            <div class="bloc x0 y0 w9 h12 flow-main" style="margin-bottom:0pt;"></div>
+            <!-- <div class="bloc x0 w1 flow-fn" style="bottom: 115pt;"></div> -->
+          </div>
+        </div>
+      </div>
+
+    </div>
+
+
+    <div id="stories">
+      <div id="flow-main">
+
+      </div>
+      <!-- // my-story -->
+    </div>
+    <!-- // stories -->
+
+    <script src="/assets/lib/jquery.min.js" charset="utf-8"></script>
+    <script src="/assets/js/setup.js" charset="utf-8"></script>
+    <script src="/assets/js/html2print.js" charset="utf-8"></script>
+    <script src="/assets/js/script.js" charset="utf-8"></script>
+  </body>
+</html>

+ 2 - 2
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;}
 

+ 1 - 1
templates/newproject/assets/js/setup.js

@@ -1 +1 @@
-nb_page = 100;
+nb_page = 2;