1#!/usr/bin/env python3 2 3import sys, os, shutil, getopt 4import re, yaml 5import subprocess 6 7githuburl = "https://github.com/bluekitchen/btstack/tree/" 8gitbranchname = "master" 9 10# helper to write anchors and references 11def insert_anchor(mdout, reference): 12 anchor = "<a name=\"" + reference + "\"></a>\n\n" 13 mdout.write(anchor) 14 15def insert_reference(mdout, text, link): 16 mdout.write("") 17 18def process_source_file_link(mdin, mdout, githuburl, line): 19 parts = re.match('.*(GITHUB_URL).*\n',line) 20 if parts: 21 line_with_source_file_link = line.replace("GITHUB_URL", githuburl) 22 mdout.write(line_with_source_file_link) 23 line = '' 24 return line 25 26# handlers for various elements 27def process_section(mdin, mdout, line): 28 section = re.match('(#+.*){#(sec:.*)}',line) 29 if section: 30 insert_anchor(mdout, section.group(2)) 31 mdout.write(section.group(1)+"\n") 32 line = '' 33 return line 34 35def process_figure(mdin, mdout, line): 36 # detect figure 37 figure = re.match('\s*(\!.*)({#(fig:.*)})',line) 38 if figure: 39 insert_anchor(mdout, figure.group(3)) 40 mdout.write(figure.group(1)+"\n") 41 line = '' 42 return line 43 44def process_fig_ref(mdin, mdout, line): 45 # detect figure reference 46 figure_ref = re.match('.*({@(fig:.*)})',line) 47 if figure_ref: 48 md_reference = "[below](#"+figure_ref.group(2)+")" 49 line = line.replace(figure_ref.group(1), md_reference) 50 mdout.write(line) 51 line = '' 52 return line 53 54def process_table(mdin, mdout, line): 55 # detect table 56 table = re.match('\s*(Table:.*)({#(tbl:.*)})',line) 57 if table: 58 insert_anchor(mdout, table.group(3)) 59 mdout.write(table.group(1)+"\n") 60 line = '' 61 return line 62 63def process_tbl_ref(mdin, mdout, line): 64 table_ref = re.match('.*({@(tbl:.*)})',line) 65 if table_ref: 66 md_reference = "[below](#"+table_ref.group(2)+")" 67 line = line.replace(table_ref.group(1), md_reference) 68 mdout.write(line) 69 line = '' 70 return line 71 72def process_listing(mdin, mdout, line): 73 listing_start = re.match('.*{#(lst:.*)\s+.c\s+.*',line) 74 listing_end = re.match('\s*~~~~\s*\n',line) 75 if listing_start: 76 insert_anchor(mdout, listing_start.group(1)) 77 line = '' 78 elif listing_end: 79 mdout.write("\n") 80 line = '' 81 return line 82 83def process_file(mk_file, markdownfolder, mkdocsfolder, githuburl): 84 source_file = markdownfolder +"/"+ mk_file 85 dest_file = mkdocsfolder +"/"+ mk_file 86 # print("Processing %s -> %s" % (source_file, dest_file)) 87 88 with open(dest_file, 'wt') as mdout: 89 with open(source_file, 'rt') as mdin: 90 for line in mdin: 91 line = process_section(mdin, mdout, line) 92 if len(line) == 0: 93 continue 94 line = process_source_file_link(mdin, mdout, githuburl, line) 95 if len(line) == 0: 96 continue 97 line = process_figure(mdin, mdout, line) 98 if len(line) == 0: 99 continue 100 line = process_fig_ref(mdin, mdout, line) 101 if len(line) == 0: 102 continue 103 line = process_table(mdin, mdout, line) 104 if len(line) == 0: 105 continue 106 line = process_tbl_ref(mdin, mdout, line) 107 if len(line) == 0: 108 continue 109 line = process_listing(mdin, mdout, line) 110 if len(line) == 0: 111 continue 112 mdout.write(line) 113 114def main(argv): 115 markdownfolder = "docs-markdown/" 116 mkdocsfolder = "docs/" 117 118 cmd = 'markdown_update_references.py [-i <markdownfolder>] [-o <mkdocsfolder>] [-g <githuburl>]' 119 120 try: 121 opts, args = getopt.getopt(argv,"i:o:g:",["ifolder=","ofolder=","github="]) 122 except getopt.GetoptError: 123 print (cmd) 124 sys.exit(2) 125 for opt, arg in opts: 126 if opt == '-h': 127 print (cmd) 128 sys.exit() 129 elif opt in ("-i", "--ifolder"): 130 markdownfolder = arg 131 elif opt in ("-o", "--ofolder"): 132 mkdocsfolder = arg 133 elif opt in ("-g", "--github"): 134 githuburl = arg 135 136 try: 137 output = subprocess.check_output("git symbolic-ref --short HEAD", stderr=subprocess.STDOUT, timeout=3, shell=True) 138 gitbranchname = output.decode().rstrip() 139 except subprocess.CalledProcessError as exc: 140 print('GIT branch name: failed to get, use default value \"%s\"" ', gitbranchname, exc.returncode, exc.output) 141 else: 142 print('GIT branch name: %s' % gitbranchname) 143 144 githuburl = githuburl + gitbranchname 145 print('GITHUB URL: %s\n' % githuburl) 146 147 yml_file = "mkdocs.yml" 148 149 with open(yml_file, 'r') as yin: 150 doc = yaml.load(yin, Loader=yaml.SafeLoader) 151 152 # page is either: 153 # - {title: filepath} dictionary for a direcr yml reference (e.g. - 'Welcome': index.md), or 154 # - {navigation_group_title: [{title: filepath}, ...] } dictionary for a navigation group 155 for page in doc["nav"]: 156 157 # navigation_group_filepath is either: 158 # - filepath string for a direcr yml reference (e.g. - 'Welcome': index.md), or 159 # - list of [{title: filepath}, ...] dictionaries for each item in navigation group 160 navigation_group_filepath = list(page.values())[0] 161 162 if type(navigation_group_filepath) == str: 163 process_file(navigation_group_filepath, markdownfolder, mkdocsfolder, githuburl) 164 continue 165 166 if type(navigation_group_filepath) == list: 167 for file_description_dict in navigation_group_filepath: 168 filepath = list(file_description_dict.values())[0] 169 process_file(filepath, markdownfolder, mkdocsfolder, githuburl) 170 continue 171 172 173if __name__ == "__main__": 174 main(sys.argv[1:]) 175