btstack/doc/manual/markdown_update_references.py

175 lines
5.9 KiB
Python
Raw Normal View History

2020-10-05 16:03:24 +00:00
#!/usr/bin/env python3
2015-06-18 14:33:34 +00:00
2021-05-27 12:45:58 +00:00
import sys, os, shutil, getopt
import re, yaml
2023-03-03 09:52:40 +00:00
import subprocess
githuburl = "https://github.com/bluekitchen/btstack/tree/"
gitbranchname = "master"
2015-06-18 14:33:34 +00:00
2018-01-08 11:39:43 +00:00
# helper to write anchors and references
2015-06-18 14:33:34 +00:00
def insert_anchor(mdout, reference):
2018-01-08 11:39:43 +00:00
anchor = "<a name=\"" + reference + "\"></a>\n\n"
mdout.write(anchor)
2015-06-18 14:33:34 +00:00
def insert_reference(mdout, text, link):
mdout.write("")
2023-03-03 09:52:40 +00:00
def process_source_file_link(mdin, mdout, githuburl, line):
parts = re.match(r'.*(GITHUB_URL).*\n',line)
2023-03-03 09:52:40 +00:00
if parts:
line_with_source_file_link = line.replace("GITHUB_URL", githuburl)
mdout.write(line_with_source_file_link)
line = ''
return line
2018-01-08 11:39:43 +00:00
# handlers for various elements
def process_section(mdin, mdout, line):
section = re.match(r'(#+.*){#(sec:.*)}',line)
2018-01-08 11:39:43 +00:00
if section:
insert_anchor(mdout, section.group(2))
mdout.write(section.group(1)+"\n")
line = ''
return line
2015-06-18 14:33:34 +00:00
2018-01-08 11:39:43 +00:00
def process_figure(mdin, mdout, line):
# detect figure
figure = re.match(r'\s*(\!.*)({#(fig:.*)})',line)
2018-01-08 11:39:43 +00:00
if figure:
insert_anchor(mdout, figure.group(3))
mdout.write(figure.group(1)+"\n")
line = ''
return line
2015-06-19 13:41:29 +00:00
2018-01-08 11:39:43 +00:00
def process_fig_ref(mdin, mdout, line):
# detect figure reference
figure_ref = re.match(r'.*({@(fig:.*)})',line)
2018-01-08 11:39:43 +00:00
if figure_ref:
md_reference = "[below](#"+figure_ref.group(2)+")"
line = line.replace(figure_ref.group(1), md_reference)
mdout.write(line)
line = ''
return line
2015-06-19 13:41:29 +00:00
2018-01-08 11:39:43 +00:00
def process_table(mdin, mdout, line):
# detect table
table = re.match(r'\s*(Table:.*)({#(tbl:.*)})',line)
2018-01-08 11:39:43 +00:00
if table:
insert_anchor(mdout, table.group(3))
mdout.write(table.group(1)+"\n")
line = ''
return line
2015-06-18 14:33:34 +00:00
2018-01-08 11:39:43 +00:00
def process_tbl_ref(mdin, mdout, line):
table_ref = re.match(r'.*({@(tbl:.*)})',line)
2018-01-08 11:39:43 +00:00
if table_ref:
md_reference = "[below](#"+table_ref.group(2)+")"
line = line.replace(table_ref.group(1), md_reference)
mdout.write(line)
line = ''
return line
def process_listing(mdin, mdout, line):
listing_start = re.match(r'.*{#(lst:.*)\s+.c\s+.*',line)
listing_end = re.match(r'\s*~~~~\s*\n',line)
2018-01-08 11:39:43 +00:00
if listing_start:
insert_anchor(mdout, listing_start.group(1))
line = ''
elif listing_end:
mdout.write("\n")
line = ''
return line
2015-06-18 14:33:34 +00:00
2023-03-03 09:52:40 +00:00
def process_file(mk_file, markdownfolder, mkdocsfolder, githuburl):
source_file = markdownfolder +"/"+ mk_file
dest_file = mkdocsfolder +"/"+ mk_file
# print("Processing %s -> %s" % (source_file, dest_file))
with open(dest_file, 'wt') as mdout:
with open(source_file, 'rt') as mdin:
for line in mdin:
line = process_section(mdin, mdout, line)
2023-03-03 09:52:40 +00:00
if len(line) == 0:
continue
line = process_source_file_link(mdin, mdout, githuburl, line)
if len(line) == 0:
continue
line = process_figure(mdin, mdout, line)
if len(line) == 0:
continue
line = process_fig_ref(mdin, mdout, line)
if len(line) == 0:
continue
line = process_table(mdin, mdout, line)
if len(line) == 0:
continue
line = process_tbl_ref(mdin, mdout, line)
if len(line) == 0:
continue
line = process_listing(mdin, mdout, line)
if len(line) == 0:
continue
mdout.write(line)
2015-06-18 14:33:34 +00:00
def main(argv):
2021-05-27 12:45:58 +00:00
markdownfolder = "docs-markdown/"
mkdocsfolder = "docs/"
2023-03-03 09:52:40 +00:00
cmd = 'markdown_update_references.py [-i <markdownfolder>] [-o <mkdocsfolder>] [-g <githuburl>]'
2021-05-27 12:45:58 +00:00
try:
2023-03-03 09:52:40 +00:00
opts, args = getopt.getopt(argv,"i:o:g:",["ifolder=","ofolder=","github="])
2021-05-27 12:45:58 +00:00
except getopt.GetoptError:
print (cmd)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print (cmd)
sys.exit()
elif opt in ("-i", "--ifolder"):
markdownfolder = arg
elif opt in ("-o", "--ofolder"):
mkdocsfolder = arg
2023-03-03 09:52:40 +00:00
elif opt in ("-g", "--github"):
githuburl = arg
try:
output = subprocess.check_output("git symbolic-ref --short HEAD", stderr=subprocess.STDOUT, timeout=3, shell=True)
gitbranchname = output.decode().rstrip()
except subprocess.CalledProcessError as exc:
print('GIT branch name: failed to get, use default value \"%s\"" ', gitbranchname, exc.returncode, exc.output)
else:
print('GIT branch name: %s' % gitbranchname)
2023-03-03 10:11:20 +00:00
2023-03-03 09:52:40 +00:00
githuburl = githuburl + gitbranchname
print('GITHUB URL: %s\n' % githuburl)
2021-05-27 12:45:58 +00:00
2015-06-18 14:33:34 +00:00
yml_file = "mkdocs.yml"
with open(yml_file, 'r') as yin:
2020-10-05 16:03:24 +00:00
doc = yaml.load(yin, Loader=yaml.SafeLoader)
2021-05-28 13:35:09 +00:00
# page is either:
# - {title: filepath} dictionary for a direcr yml reference (e.g. - 'Welcome': index.md), or
# - {navigation_group_title: [{title: filepath}, ...] } dictionary for a navigation group
for page in doc["nav"]:
2021-05-28 13:35:09 +00:00
# navigation_group_filepath is either:
# - filepath string for a direcr yml reference (e.g. - 'Welcome': index.md), or
# - list of [{title: filepath}, ...] dictionaries for each item in navigation group
navigation_group_filepath = list(page.values())[0]
2021-05-28 13:35:09 +00:00
if type(navigation_group_filepath) == str:
2023-03-03 09:52:40 +00:00
process_file(navigation_group_filepath, markdownfolder, mkdocsfolder, githuburl)
continue
2015-06-18 14:33:34 +00:00
2021-05-28 13:35:09 +00:00
if type(navigation_group_filepath) == list:
for file_description_dict in navigation_group_filepath:
filepath = list(file_description_dict.values())[0]
2023-03-03 09:52:40 +00:00
process_file(filepath, markdownfolder, mkdocsfolder, githuburl)
continue
2015-06-18 14:33:34 +00:00
if __name__ == "__main__":
main(sys.argv[1:])