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 aout.write("\n\n#"+ title +"\n\n") 69 for line in mdin: 70 # remove path from section reference 71 # e.g. [the SPP Counter example](examples/generated/#sec:sppcounterExample) 72 # replace with [the SPP Counter example](#sec:sppcounterExample) 73 section_ref = re.match('.*\(((.*)(#sec:.*))\).*',line) 74 if section_ref: 75 line = line.replace(section_ref.group(2),"") 76 aout.write(line) 77 78 pandoc_cmd = "pandoc -f markdown -t latex --filter pandoc-fignos --filter pandoc-tablenos --listings latex/btstack_generated.md -o latex/btstack_generated.tex" 79 p = os.popen(pandoc_cmd,"r") 80 while 1: 81 line = p.readline() 82 if not line: break 83 print (line) 84 85 86 # btstatck_root_file = "latex/btstack_gettingstarted.tex" 87 btstack_generated_file = "latex/btstack_generated.tex" 88 btstack_final_file = "latex/btstack_final.tex" 89 90 with open(btstack_final_file, 'w') as aout: 91 aout.write("% !TEX root = btstack_gettingstarted.tex\n\n") 92 93 with open(btstack_generated_file, 'r') as fin: 94 for line in fin: 95 line = fix_empty_href(line) 96 line = fix_listing_after_section(line) 97 line = fix_listing_hyperref_into_ref(line) 98 line = fix_figure_width_and_type(line) 99 line = fix_appendix_pagebreak(line) 100 line = fix_tightlist(line) 101 aout.write(line) 102 103 104if __name__ == "__main__": 105 main(sys.argv[1:]) 106