]> Shamusworld >> Repos - ardour-manual/blobdiff - build.py
Cue: document Cue Isolate option
[ardour-manual] / build.py
index 4424f76bea483d2cfa710ec23ce64d99bca40ad8..16a01f91feb5ee5645bc188a22080cea7b7701c9 100755 (executable)
--- a/build.py
+++ b/build.py
@@ -27,8 +27,8 @@ global_screen_template = 'page-template.html'
 global_onepage_template = 'onepage-template.html'
 global_pdf_template = 'pdf-template.html'
 global_master_doc = 'master-doc.txt'
+global_pdflink = '<button class="btn btn-default" type="button" onclick="window.location.href=\'/manual.pdf\'"><span class="glyphicon glyphicon-book" aria-hidden="true"></span></button>'
 from datetime import datetime
-global_today_iso = datetime.today().strftime('%Y-%m-%dT%H%M%S')
 global_today = datetime.today().strftime('%Y-%m-%d')
 
 # This matches all *non* letter/number, ' ', '.', '-', and '_' chars
@@ -208,16 +208,22 @@ def GetParent(fs, pos):
 
 
 #
-# Change the hierarchy of titles : h1->hn, h2->hn+1, etc... n being delta-1
+# Change the hierarchy of titles : <h1>-><hn>, <h2>-><hn+1>, so that the
+# highest hyerarchy level is maxlevel
 #
-def reheader(txt, delta):
-       for i in range(6, 0, -1):
-               txt = txt.replace('<h' + str(i), '<h' + str(i + delta))
-               txt = txt.replace('</h' + str(i), '</h' + str(i + delta))
+def remapheader(txt, maxlevel):
+       maxlvl=1
+       # find the highest hierarchy level in the content
+       while maxlvl < 7 and txt.find('<h' + str(maxlvl)) == -1:
+               maxlvl += 1
+       # if there is a hierarchy, remap it so that the highest level is maxlevel
+       if maxlvl < 7:
+               for i in range(6, maxlvl-1, -1):
+                       txt = txt.replace('<h' + str(i), '<h' + str(i + maxlevel - maxlvl))
+                       txt = txt.replace('</h' + str(i), '</h' + str(i + maxlevel - maxlvl))
 
        return txt
-
-
+       
 #
 # Creates the BreadCrumbs
 #
@@ -456,6 +462,11 @@ template = temp.read()
 temp.close()
 template = template.replace('{{page.bootstrap_path}}', global_bootstrap_path)
 template = template.replace('{{page.page_title}}', global_page_title)
+if pdf:
+       template = template.replace('{{page.page_pdflink}}', global_pdflink)
+else:
+       template = template.replace('{{page.page_pdflink}}', '')
+
 
 # Same as above, but for the "One-Page" version
 temp = open(global_onepage_template)
@@ -617,7 +628,7 @@ for header in fileStruct:
 
        # Fix up any internal links
        opcontent = FixInternalLinks(oplinks, content, header['title'])
-       opcontent = reheader(opcontent, 2)
+       opcontent = remapheader(opcontent, level+2)
 
        # Create "one page" header
        oph = '<h' + str(level+1) + ' class="clear" id="' + header[('link' if 'link' in header else 'filename')] +'">' + header['title'] + '</h' + str(level+1) + '>\n';
@@ -691,12 +702,14 @@ onepageFile.close()
 if pdf:
        if noisy:
                print('Generating the PDF...')
+               import logging
+               logger = logging.getLogger('weasyprint')
+               logger.addHandler(logging.StreamHandler())
 
        # Create the PDF version of the documentation
        pdfpage = pdfpage.replace('{% tree %}', opsidebar) # create the TOC
        pdfpage = pdfpage.replace('{{ content }}', '') # cleans up the last spaceholder
        pdfpage = pdfpage.replace('{{ today }}', global_today)
-       pdfpage = pdfpage.replace('{{ today_iso }}', global_today_iso)
        pdfpage = pdfpage.replace('src="/images/', 'src="images/') # makes images links relative
        pdfpage = pdfpage.replace('url(\'/images/', 'url(\'images/') # CSS images links relative
        # Write it to disk (optional, can be removed)
@@ -706,7 +719,8 @@ if pdf:
 
        # Generating the actual PDF with weasyprint (https://weasyprint.org/)
        from weasyprint import HTML
-       from weasyprint.fonts import FontConfiguration
+       from weasyprint.text.fonts import FontConfiguration
+       
        html_font_config = FontConfiguration()
        doc = HTML(string = pdfpage, base_url = global_site_dir)
        doc.write_pdf(global_site_dir + 'manual.pdf', font_config = html_font_config)