1#!/usr/bin/env python3 2 3import sys, yaml 4import os, re, getopt 5 6pandoc_cmd_template = """ 7pandoc -f markdown -t latex --filter pandoc-fignos --filter pandoc-tablenos --listings LATEX_FOLDERbtstack_generated.md -o LATEX_FOLDERbtstack_generated.tex 8 9""" 10 11figures = { 12 'btstack-architecture' : '1', 13 'singlethreading-btstack' : '0.3', 14 'multithreading-monolithic': '0.8', 15 'multithreading-btdaemon' : '0.8', 16 'btstack-protocols' : '0.8' 17} 18 19 20def fix_empty_href(line): 21 corr = re.match(r'.*(href{}).*',line) 22 if corr: 23 line = line.replace(corr.group(1), "path") 24 return line 25 26 27def fix_listing_after_section(line): 28 corr = re.match(r'.*begin{lstlisting}',line) 29 if corr: 30 line = "\leavevmode" + line 31 return line 32 33def fix_listing_hyperref_into_ref(line): 34 corr = re.match(r'(.*\\)hyperref\[(lst:.*)\]{.*}(.*)',line) 35 if corr: 36 line = corr.group(1)+"ref{" + corr.group(2) +"} " + corr.group(3) 37 return line 38 39 40def fix_figure_width_and_type(line): 41 global figures 42 for name, width in figures.items(): 43 corr = re.match(r'(.*includegraphics)(.*'+name+'.*)',line) 44 if corr: 45 line = corr.group(1) + '[width='+width+'\\textwidth]' + corr.group(2).replace('png','pdf') 46 return line 47 48 49def fix_appendix_pagebreak(line): 50 corr = re.match(r'.*section{APIs}.*',line) 51 if corr: 52 line = "\leavevmode\pagebreak\n" + line 53 return line 54 55def fix_tightlist(line): 56 if 'tightlist' in line: 57 return '' 58 else: 59 return line 60 61def postprocess_file(markdown_filepath, fout, title): 62 with open(markdown_filepath, 'r') as fin: 63 for line in fin: 64 if line == "#\n": 65 fout.write("\n\n#"+ title +"\n\n") 66 continue 67 # remove path from section reference 68 # e.g. [the SPP Counter example](examples/generated/#sec:sppcounterExample) 69 # replace with [the SPP Counter example](#sec:sppcounterExample) 70 section_ref = re.match(r'.*\(((.*)(#sec:.*))\).*',line) 71 if section_ref: 72 line = line.replace(section_ref.group(2),"") 73 fout.write(line) 74 75def main(argv): 76 yml_file = "mkdocs.yml" 77 latexfolder = "latex/" 78 mkdocsfolder = "docs/" 79 80 cmd = 'markdown2tex.py [-i <mkdocsfolder>] [-o <latexfolder>] ' 81 82 try: 83 opts, args = getopt.getopt(argv,"i:o:",["ifolder=","ofolder="]) 84 except getopt.GetoptError: 85 print (cmd) 86 sys.exit(2) 87 for opt, arg in opts: 88 if opt == '-h': 89 print (cmd) 90 sys.exit() 91 elif opt in ("-i", "--ifolder"): 92 mkdocsfolder = arg 93 elif opt in ("-o", "--ofolder"): 94 latexfolder = arg 95 96 latex_filepath = latexfolder + "btstack_generated.md" 97 98 with open(latex_filepath, 'wt') as fout: 99 with open(yml_file, 'rt') as yin: 100 doc = yaml.load(yin, Loader=yaml.SafeLoader) 101 for page in doc["nav"]: 102 navigation_group_filepath = list(page.values())[0] 103 navigation_group_title = list(page.keys())[0] 104 markdown_filepath = mkdocsfolder + navigation_group_filepath 105 postprocess_file(markdown_filepath, fout, navigation_group_title) 106 107 pandoc_cmd = pandoc_cmd_template.replace("LATEX_FOLDER", latexfolder) 108 109 p = os.popen(pandoc_cmd,"r") 110 while 1: 111 line = p.readline() 112 if not line: break 113 print (line) 114 115 116 # btstatck_root_file = "latex/btstack_gettingstarted.tex" 117 btstack_generated_file = latexfolder + "btstack_generated.tex" 118 btstack_final_file = latexfolder + "btstack_final.tex" 119 120 with open(btstack_final_file, 'w') as aout: 121 aout.write("% !TEX root = btstack_gettingstarted.tex\n\n") 122 123 with open(btstack_generated_file, 'r') as fin: 124 for line in fin: 125 line = fix_empty_href(line) 126 line = fix_listing_after_section(line) 127 line = fix_listing_hyperref_into_ref(line) 128 line = fix_figure_width_and_type(line) 129 line = fix_appendix_pagebreak(line) 130 line = fix_tightlist(line) 131 aout.write(line) 132 133 134if __name__ == "__main__": 135 main(sys.argv[1:]) 136