62 Extracts the documentation from the given file contents and writes it to a markdown file.
67 The name of the file to extract documentation from.
69 The contents of the file.
72 pattern =
r"/\*\s*(#step|#name|#desc)(.*?)\*/"
73 comments = re.findall(pattern, contents, re.DOTALL)
77 made_description =
False
78 with open(self.
out_dir +
"/" + md_filename,
"w")
as md_file:
80 for kind, comment
in comments:
81 comment_parts = comment.strip().split(
"Sources:", 1)
83 re.sub(
r"\n *",
"\n", comment_parts[0])
85 .replace(
"\n *",
"\n")
88 re.findall(
r"https?://[^\s]+", comment_parts[1])
89 if len(comment_parts) > 1
94 doxygen_ref = f
"{comment_text.lower()}_icp".replace(
"-",
"_")
95 md_file.write(f
"\\page {doxygen_ref} {comment_text} ICP\n")
97 register_pattern =
r"/\*\s*#register(.*?)\*/"
98 registers = re.findall(register_pattern, contents, re.DOTALL)
99 assert len(registers) == 1
100 register_name = registers[0].strip()
102 conf_pattern =
r'/\*\s*#conf\s+"([^"]*)"\s+(.*?)\*/'
103 confs = re.findall(conf_pattern, contents, re.DOTALL)
107 f
'\\par Usage\nYou can construct a new instance of {comment_text} ICP with `icp::ICP::from_method("{register_name}")`.'
111 f
'\\par Usage\nYou can construct a new instance of {comment_text} ICP with `icp::ICP::from_method("{register_name}", config)`. Supply the following parameters to `config` (via icp::ICP::Config::set):\n'
113 md_file.write(
"\nKey | Description\n--- | ---\n")
114 for key, descr
in confs:
115 descr_lines = descr.split(
"\n")
117 line.strip().removeprefix(
"*")
for line
in descr_lines
119 descr =
" ".join(cleaned_lines)
120 md_file.write(f
'`"{key}"` | {descr}\n')
121 elif kind ==
"#step":
122 if not made_description:
123 md_file.write(
"\n\\par Description\n")
124 made_description =
True
125 lines = comment_text.splitlines()
126 first_line_parts = lines[0].split(
":", 1)
127 if len(first_line_parts) == 2:
128 lines[0] = f
"**{first_line_parts[0]}**:{first_line_parts[1]}"
130 lines[0] = f
"**{first_line_parts[0]}**"
131 step_text =
"\n".join(
" " + line
for line
in lines)
132 md_file.write(f
"\n{step_cnt}. {step_text}\n")
135 md_file.write(
" Sources: \n")
136 for source
in sources:
137 md_file.write(f
" - {source}\n")
139 elif kind ==
"#desc":
140 icp_description = comment_text
141 md_file.write(f
"\n\\par Description\n{comment_text}\n")
142 made_description =
True
144 "\n\nRead \\ref icp_sources for a list of all resources used in this project."
147 f
"\nThis page was automatically generated from {file} with {os.path.basename(__file__)}."
149 return doxygen_ref, icp_description