1#!/usr/bin/env python3 2 3import sys, os, shutil, getopt 4import re, yaml 5 6# helper to write anchors and references 7def insert_anchor(mdout, reference): 8 anchor = "<a name=\"" + reference + "\"></a>\n\n" 9 mdout.write(anchor) 10 11def insert_reference(mdout, text, link): 12 mdout.write("") 13 14# handlers for various elements 15def process_section(mdin, mdout, line): 16 section = re.match('(#+.*){#(sec:.*)}',line) 17 if section: 18 insert_anchor(mdout, section.group(2)) 19 mdout.write(section.group(1)+"\n") 20 line = '' 21 return line 22 23def process_figure(mdin, mdout, line): 24 # detect figure 25 figure = re.match('\s*(\!.*)({#(fig:.*)})',line) 26 if figure: 27 insert_anchor(mdout, figure.group(3)) 28 mdout.write(figure.group(1)+"\n") 29 line = '' 30 return line 31 32def process_fig_ref(mdin, mdout, line): 33 # detect figure reference 34 figure_ref = re.match('.*({@(fig:.*)})',line) 35 if figure_ref: 36 md_reference = "[below](#"+figure_ref.group(2)+")" 37 line = line.replace(figure_ref.group(1), md_reference) 38 mdout.write(line) 39 line = '' 40 return line 41 42def process_table(mdin, mdout, line): 43 # detect table 44 table = re.match('\s*(Table:.*)({#(tbl:.*)})',line) 45 if table: 46 insert_anchor(mdout, table.group(3)) 47 mdout.write(table.group(1)+"\n") 48 line = '' 49 return line 50 51def process_tbl_ref(mdin, mdout, line): 52 table_ref = re.match('.*({@(tbl:.*)})',line) 53 if table_ref: 54 md_reference = "[below](#"+table_ref.group(2)+")" 55 line = line.replace(table_ref.group(1), md_reference) 56 mdout.write(line) 57 line = '' 58 return line 59 60def process_listing(mdin, mdout, line): 61 listing_start = re.match('.*{#(lst:.*)\s+.c\s+.*',line) 62 listing_end = re.match('\s*~~~~\s*\n',line) 63 if listing_start: 64 insert_anchor(mdout, listing_start.group(1)) 65 line = '' 66 elif listing_end: 67 mdout.write("\n") 68 line = '' 69 return line 70 71def main(argv): 72 markdownfolder = "docs-markdown/" 73 mkdocsfolder = "docs/" 74 75 cmd = 'markdown_update_references.py [-i <markdownfolder>] [-o <mkdocsfolder>] ' 76 77 try: 78 opts, args = getopt.getopt(argv,"i:o:",["ifolder=","ofolder="]) 79 except getopt.GetoptError: 80 print (cmd) 81 sys.exit(2) 82 for opt, arg in opts: 83 if opt == '-h': 84 print (cmd) 85 sys.exit() 86 elif opt in ("-i", "--ifolder"): 87 markdownfolder = arg 88 elif opt in ("-o", "--ofolder"): 89 mkdocsfolder = arg 90 91 yml_file = "mkdocs.yml" 92 93 with open(yml_file, 'r') as yin: 94 doc = yaml.load(yin, Loader=yaml.SafeLoader) 95 for page in doc["nav"]: 96 mk_file = list(page.values())[0] 97 source_file = markdownfolder +"/"+ mk_file 98 dest_file = mkdocsfolder +"/"+ mk_file 99 print("Processing %s -> %s" % (source_file, dest_file)) 100 with open(dest_file, 'w') as mdout: 101 with open(source_file, 'r') as mdin: 102 for line in mdin: 103 line = process_section(mdin, mdout, line) 104 if len(line) == 0: 105 continue 106 line = process_figure(mdin, mdout, line) 107 if len(line) == 0: 108 continue 109 line = process_fig_ref(mdin, mdout, line) 110 if len(line) == 0: 111 continue 112 line = process_table(mdin, mdout, line) 113 if len(line) == 0: 114 continue 115 line = process_tbl_ref(mdin, mdout, line) 116 if len(line) == 0: 117 continue 118 line = process_listing(mdin, mdout, line) 119 if len(line) == 0: 120 continue 121 mdout.write(line) 122 123if __name__ == "__main__": 124 main(sys.argv[1:]) 125