From 73bd84ddd1ab845643705d63f94e5eac33d162af Mon Sep 17 00:00:00 2001 From: Laurent Rineau Date: Fri, 17 Nov 2023 11:25:44 +0100 Subject: [PATCH 1/2] Improve add_toc_to_github_wiki_page.py - reformat using `black` - add an option `--max-level` - quote anchors (for accentuated character) - fix linter errors --- .../add_toc_to_github_wiki_page.py | 191 +++++++++--------- 1 file changed, 98 insertions(+), 93 deletions(-) diff --git a/Scripts/developer_scripts/add_toc_to_github_wiki_page.py b/Scripts/developer_scripts/add_toc_to_github_wiki_page.py index ce61f49db7e1..494878cca738 100644 --- a/Scripts/developer_scripts/add_toc_to_github_wiki_page.py +++ b/Scripts/developer_scripts/add_toc_to_github_wiki_page.py @@ -1,125 +1,130 @@ -from sys import argv -from sys import exit import codecs import re import argparse +import sys +from urllib.parse import quote parser = argparse.ArgumentParser() -parser.add_argument("filename", - help="the Mardown file to process") -parser.add_argument("--codebase", - help="for a Markdown file of Codebase instead of Github", - action="store_true") -parser.add_argument("--h1", - help="support level one sections (h1)", - action="store_true") +parser.add_argument("filename", help="the Mardown file to process") +parser.add_argument( + "--codebase", + help="for a Markdown file of Codebase instead of Github", + action="store_true", +) +parser.add_argument("--h1", help="support level one sections (h1)", action="store_true") +parser.add_argument("--max-level", help="maximum level of sections", type=int, default = 5) args = parser.parse_args() + # a probably incomplete version to generate an anchor from a section name def get_anchor(s): - s = s.replace("`","") - s = s.replace("(","") - s = s.replace(")","") - s = s.replace(".","") - s = s.replace("#","") - s = s.replace(":","") - s = s.replace(",","") - s = s.replace(";","") - if args.codebase: - s = s.replace("/","-") - else: - s = s.replace("/","") - s = s.replace("<","") - s = s.replace(">","") - s = s.replace("+","") - s = s.replace("=","") - s = s.replace("?","") - s = s.replace("@","") - s = s.lstrip(" ") - s = s.rstrip("\n") - s = s.rstrip(" ") - s = re.sub(r'\s+','-',s) - if not args.codebase: - s = s.lower() - if args.codebase: - s = s.replace("'","-and-39-") - return "#"+s + s = s.replace("`", "") + s = s.replace("(", "") + s = s.replace(")", "") + s = s.replace(".", "") + s = s.replace("#", "") + s = s.replace(":", "") + s = s.replace(",", "") + s = s.replace(";", "") + if args.codebase: + s = s.replace("/", "-") + else: + s = s.replace("/", "") + s = s.replace("<", "") + s = s.replace(">", "") + s = s.replace("+", "") + s = s.replace("=", "") + s = s.replace("?", "") + s = s.replace("@", "") + s = s.lstrip(" ") + s = s.rstrip("\n") + s = s.rstrip(" ") + s = re.sub(r"\s+", "-", s) + if not args.codebase: + s = s.lower() + if args.codebase: + s = s.replace("'", "-and-39-") + return "#" + quote(s) + # indices the nesting level (first level allowed is ##) def get_level(s): - m = re.search('^(#+)\s', s) - if m: - return len(m.group(1)) - else: - return 0 + m = re.search(r"^(#+)\s", s) + if m: + return len(m.group(1)) + else: + return 0 + def get_name(s): - m = re.search('^#+\s+(.*)\s*$', s) - if m: - return m.group(1) - else: - return "ERROR: Section name extraction" + m = re.search(r"^#+\s+(.*)\s*$", s) + if m: + return m.group(1) + else: + return "ERROR: Section name extraction" + -#generate the entry for one section +# generate the entry for one section def get_toc_entry(s): - name = get_name(s) - if args.h1: - level = get_level(s)-1 - else: - level = get_level(s)-2 - anchor = get_anchor(s) + name = get_name(s) + if args.h1: + level = get_level(s) - 1 + else: + level = get_level(s) - 2 + anchor = get_anchor(s) + + if level < 0: + return "ERROR: h1 sections are not allowed" - if level<0: - return "ERROR: h1 sections are not allowed" + res = "* [" + name + "](" + anchor + ")" + for _ in range(0, level): + res = " " + res + return res - res="* ["+name+"]("+anchor+")" - for i in range(0,level): - res=" "+res - return res -#now the main -input = args.filename +# now the main +filename = args.filename -f = codecs.open(input, 'r', encoding='utf-8') +f = codecs.open(filename, "r", encoding="utf-8") if not f: - print("Cannot open "+input+"\n") - exit() + print("Cannot open " + input + "\n") + sys.exit() -#look for the begin of the file -line=f.readline() -if line.find("")==-1: - exit() +# look for the begin of the file +line = f.readline() +if line.find("") == -1: + sys.exit() -#skip current TOC -line=f.readline() -while line and line.find("")==-1: - line=f.readline() +# skip current TOC +line = f.readline() +while line and line.find("") == -1: + line = f.readline() if not line: - exit() + sys.exit() -buffer="" -TOC="\n\n# Table of Contents\n" +buffer = "" +TOC = "\n\n# Table of Contents\n" -verbatim_mode=False # to ignore verbatim mode while looking for sections -TOC_empty=True +verbatim_mode = False # to ignore verbatim mode while looking for sections +TOC_empty = True for line in f.readlines(): - buffer+=line - if verbatim_mode: - if line[:3]=="```": - verbatim_mode=False - else: - if line[:3]=="```": - verbatim_mode=True + buffer += line + if verbatim_mode: + if line[:3] == "```": + verbatim_mode = False else: - if line[0]=="#": - TOC+=(get_toc_entry(line)+"\n") - TOC_empty=False -TOC+="\n\n" + if line[:3] == "```": + verbatim_mode = True + else: + if line[0] == "#" and get_level(line) <= args.max_level: + TOC += get_toc_entry(line) + "\n" + TOC_empty = False +TOC += "\n\n" if not TOC_empty: - f.close() - f = codecs.open(input, 'w', encoding='utf-8') - f.write(TOC) - f.write(buffer) + f.close() + f = codecs.open(filename, "w", encoding="utf-8") + f.write(TOC) + f.write(buffer) From fa96bf86232add30f1896390a9a82af76180498e Mon Sep 17 00:00:00 2001 From: Laurent Rineau Date: Fri, 17 Nov 2023 17:11:46 +0100 Subject: [PATCH 2/2] fix other linter warnings --- .../add_toc_to_github_wiki_page.py | 84 ++++++++++--------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/Scripts/developer_scripts/add_toc_to_github_wiki_page.py b/Scripts/developer_scripts/add_toc_to_github_wiki_page.py index 494878cca738..928703c502e8 100644 --- a/Scripts/developer_scripts/add_toc_to_github_wiki_page.py +++ b/Scripts/developer_scripts/add_toc_to_github_wiki_page.py @@ -5,7 +5,7 @@ from urllib.parse import quote parser = argparse.ArgumentParser() -parser.add_argument("filename", help="the Mardown file to process") +parser.add_argument("filename", help="the Markdown file to process") parser.add_argument( "--codebase", help="for a Markdown file of Codebase instead of Github", @@ -83,48 +83,52 @@ def get_toc_entry(s): # now the main -filename = args.filename +def main(): + filename = args.filename -f = codecs.open(filename, "r", encoding="utf-8") + f = codecs.open(filename, "r", encoding="utf-8") -if not f: - print("Cannot open " + input + "\n") - sys.exit() + if not f: + print("Cannot open " + input + "\n") + sys.exit() -# look for the begin of the file -line = f.readline() -if line.find("") == -1: - sys.exit() - -# skip current TOC -line = f.readline() -while line and line.find("") == -1: + # look for the begin of the file line = f.readline() + if line.find("") == -1: + sys.exit() -if not line: - sys.exit() - -buffer = "" -TOC = "\n\n# Table of Contents\n" - -verbatim_mode = False # to ignore verbatim mode while looking for sections -TOC_empty = True -for line in f.readlines(): - buffer += line - if verbatim_mode: - if line[:3] == "```": - verbatim_mode = False - else: - if line[:3] == "```": - verbatim_mode = True + # skip current TOC + line = f.readline() + while line and line.find("") == -1: + line = f.readline() + + if not line: + sys.exit() + + buffer = "" + toc = "\n\n# Table of Contents\n" + + verbatim_mode = False # to ignore verbatim mode while looking for sections + toc_empty = True + for line in f.readlines(): + buffer += line + if verbatim_mode: + if line[:3] == "```": + verbatim_mode = False else: - if line[0] == "#" and get_level(line) <= args.max_level: - TOC += get_toc_entry(line) + "\n" - TOC_empty = False -TOC += "\n\n" - -if not TOC_empty: - f.close() - f = codecs.open(filename, "w", encoding="utf-8") - f.write(TOC) - f.write(buffer) + if line[:3] == "```": + verbatim_mode = True + else: + if line[0] == "#" and get_level(line) <= args.max_level: + toc += get_toc_entry(line) + "\n" + toc_empty = False + toc += "\n\n" + + if not toc_empty: + f.close() + f = codecs.open(filename, "w", encoding="utf-8") + f.write(toc) + f.write(buffer) + +if __name__ == "__main__": + main()