xref: /btstack/doc/manual/markdown2tex.py (revision 503a627edab6ba8492c3d0cdd9ac598fe2b0f08a)
1#!/usr/bin/env python3
2
3import sys, yaml
4import os, re
5
6figures = {
7    'btstack-architecture'     : '1',
8    'singlethreading-btstack'  : '0.3',
9    'multithreading-monolithic': '0.8',
10    'multithreading-btdaemon'  : '0.8',
11    'btstack-protocols'        : '0.8'
12}
13
14
15def fix_empty_href(line):
16    corr = re.match('.*(href{}).*',line)
17    if corr:
18        line = line.replace(corr.group(1), "path")
19    return line
20
21
22def fix_listing_after_section(line):
23    corr = re.match('.*begin{lstlisting}',line)
24    if corr:
25        line = "\leavevmode" + line
26    return line
27
28def fix_listing_hyperref_into_ref(line):
29    corr = re.match('(.*\\\\)hyperref\[(lst:.*)\]{.*}(.*)',line)
30    if corr:
31        line = corr.group(1)+"ref{" + corr.group(2) +"} " + corr.group(3)
32    return line
33
34
35def fix_figure_width_and_type(line):
36    global figures
37    for name, width in figures.items():
38        corr = re.match('(.*includegraphics)(.*'+name+'.*)',line)
39        if corr:
40            line = corr.group(1) + '[width='+width+'\\textwidth]' + corr.group(2).replace('png','pdf')
41    return line
42
43
44def fix_appendix_pagebreak(line):
45    corr = re.match('.*section{APIs}.*',line)
46    if corr:
47        line = "\leavevmode\pagebreak\n" + line
48    return line
49
50def fix_tightlist(line):
51    if 'tightlist' in line:
52        return ''
53    else:
54        return line
55
56def main(argv):
57    docs_folder = "docs"
58    yml_file = "mkdocs.yml"
59    mk_file  = "latex/btstack_generated.md"
60
61    with open(mk_file, 'w') as aout:
62        with open(yml_file, 'r') as yin:
63            doc = yaml.load(yin, Loader=yaml.SafeLoader)
64            for page in doc["nav"]:
65                title   = list(page.keys())[0]
66                md_file = list(page.values())[0]
67                with open(docs_folder +"/"+ md_file, 'r') as mdin:
68
69                    for line in mdin:
70                        if line == "#\n":
71                            aout.write("\n\n#"+ title +"\n\n")
72                            continue
73                        # remove path from section reference
74                        # e.g. [the SPP Counter example](examples/generated/#sec:sppcounterExample)
75                        # replace with [the SPP Counter example](#sec:sppcounterExample)
76                        section_ref = re.match('.*\(((.*)(#sec:.*))\).*',line)
77                        if section_ref:
78                            line = line.replace(section_ref.group(2),"")
79                        aout.write(line)
80
81    pandoc_cmd = "pandoc -f markdown -t latex --filter pandoc-fignos --filter pandoc-tablenos --listings latex/btstack_generated.md -o latex/btstack_generated.tex"
82    p = os.popen(pandoc_cmd,"r")
83    while 1:
84        line = p.readline()
85        if not line: break
86        print (line)
87
88
89    # btstatck_root_file = "latex/btstack_gettingstarted.tex"
90    btstack_generated_file = "latex/btstack_generated.tex"
91    btstack_final_file = "latex/btstack_final.tex"
92
93    with open(btstack_final_file, 'w') as aout:
94        aout.write("% !TEX root = btstack_gettingstarted.tex\n\n")
95
96        with open(btstack_generated_file, 'r') as fin:
97            for line in fin:
98                line = fix_empty_href(line)
99                line = fix_listing_after_section(line)
100                line = fix_listing_hyperref_into_ref(line)
101                line = fix_figure_width_and_type(line)
102                line = fix_appendix_pagebreak(line)
103                line = fix_tightlist(line)
104                aout.write(line)
105
106
107if __name__ == "__main__":
108    main(sys.argv[1:])
109