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
#
-# 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
#
# 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';
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)
# Generating the actual PDF with weasyprint (https://weasyprint.org/)
from weasyprint import HTML
from weasyprint.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)