btstack/docs/manual/markdown2pdf.py

86 lines
2.9 KiB
Python
Raw Normal View History

2015-06-05 13:22:36 +00:00
#!/usr/bin/env python
import sys, yaml
import os, re
2015-06-18 15:39:57 +00:00
def fix_latex(line):
2015-06-05 13:22:36 +00:00
corr = re.match('.*(href{}).*',line)
if corr:
line = line.replace(corr.group(1), "path")
2015-06-18 15:39:57 +00:00
corr = re.match('.*begin{lstlisting}',line)
if corr:
line = "\leavevmode" + line
corr = re.match('.*section{APIs}.*',line)
if corr:
line = "\leavevmode\pagebreak\n" + line
corr = re.match('(.*includegraphics)(.*btstack-architecture.*)',line)
if corr:
line = corr.group(1) + "[width=\\textwidth]" + corr.group(2)
corr = re.match('(.*includegraphics)(.*singlethreading-btstack.*)',line)
if corr:
line = corr.group(1) + "[width=0.3\\textwidth]" + corr.group(2)
corr = re.match('(.*includegraphics)(.*multithreading-monolithic.*)',line)
if corr:
line = corr.group(1) + "[width=0.8\\textwidth]" + corr.group(2)
corr = re.match('(.*includegraphics)(.*multithreading-btdaemon.*)',line)
if corr:
line = corr.group(1) + "[width=0.8\\textwidth]" + corr.group(2)
corr = re.match('(.*includegraphics)(.*btstack-protocols.*)',line)
if corr:
line = corr.group(1) + "[width=0.8\\textwidth]" + corr.group(2)
2015-06-05 13:22:36 +00:00
return line
def main(argv):
docs_folder = "docs"
yml_file = "mkdocs.yml"
mk_file = "latex/btstack_generated.md"
with open(mk_file, 'w') as aout:
with open(yml_file, 'r') as yin:
doc = yaml.load(yin)
for page in doc["pages"]:
md_file = page[0]
title = page[1]
with open(docs_folder +"/"+ md_file, 'r') as mdin:
aout.write("\n\n#"+ title +"\n\n")
for line in mdin:
2015-06-18 14:33:34 +00:00
# remove path from section reference
# e.g. [the SPP Counter example](examples/generated/#sec:sppcounterExample)
# replace with [the SPP Counter example](#sec:sppcounterExample)
section_ref = re.match('.*\(((.*)(#sec:.*))\).*',line)
if section_ref:
line = line.replace(section_ref.group(2),"")
2015-06-18 15:39:57 +00:00
aout.write(line)
2015-06-05 13:22:36 +00:00
2015-06-18 14:33:34 +00:00
pandoc_cmd = "pandoc -f markdown -t latex --filter pandoc-fignos --listings latex/btstack_generated.md -o latex/btstack_generated.tex"
2015-06-05 13:22:36 +00:00
p = os.popen(pandoc_cmd,"r")
while 1:
line = p.readline()
if not line: break
print line
# btstatck_root_file = "latex/btstack_gettingstarted.tex"
btstack_generated_file = "latex/btstack_generated.tex"
btstack_final_file = "latex/btstack_final.tex"
with open(btstack_final_file, 'w') as aout:
aout.write("% !TEX root = btstack_gettingstarted.tex\n\n")
with open(btstack_generated_file, 'r') as fin:
for line in fin:
2015-06-18 15:39:57 +00:00
line = fix_latex(line)
2015-06-05 13:22:36 +00:00
aout.write(line)
if __name__ == "__main__":
main(sys.argv[1:])