btstack/doc/manual/html_postprocess_code_blocks.py
David Lechner 73677349c9 tool: consistently use raw strings for regular expressions
Newer versions of Python raise a SyntaxWarning when a regular expression
contains a backslash that is not part of an escape sequence. To prevent
this warning and future exceptions, use raw strings for all regular
expressions. Even strings without escape sequences are converted for
consistency. Some IDEs will apply special syntax highlighting to raw
strings, which can make it easier to decipher regular expressions.
2025-01-22 08:40:56 +01:00

63 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python3
import os, sys, shutil, re, pickle, getopt
from pathlib import Path
def writeCodeBlock(aout, code, references):
for function_name, url in references.items():
html_link = '<a href="' + url + '">' + function_name + '</a>'
#print "before:" + code + "\n\n"
code = code.replace(function_name, html_link)
aout.write(code)
def main(argv):
htmlfolder = "btstack/"
cmd = 'html_postprocess_code_blocks.py [-o <htmlkfolder>]'
try:
opts, args = getopt.getopt(argv,"o:",["ofolder="])
except getopt.GetoptError:
print (cmd)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print (cmd)
sys.exit()
elif opt in ("-o", "--ofolder"):
htmlfolder = arg
html_path = htmlfolder + "examples/"
html_tmppath = htmlfolder + "examples/tmp/"
html_in = html_path + "examples/index.html"
html_tmp = html_tmppath + "index.html"
references = pickle.load(open( "references.p", "rb" ))
Path(html_tmppath).mkdir(parents=True, exist_ok=True)
codeblock = 0
codeblock_end = 0
with open(html_in, 'r') as fin:
with open(html_tmp, 'w') as fout:
for line in fin:
if not codeblock:
fout.write(line)
if re.match(r'.*<pre><code>.*',line):
codeblock = 1
continue
writeCodeBlock(fout,line, references)
# check if codeblock ended
if re.match(r'.*</code></pre>.*',line):
codeblock = 0
shutil.copyfile(html_tmp, html_in)
shutil.rmtree(html_tmppath)
if __name__ == "__main__":
main(sys.argv[1:])