63 Extracts the documentation from the given file contents and writes it to a markdown file.
68 The name of the file to extract documentation from.
70 The contents of the file.
73 pattern =
r"/\*\s*(#step|#name|#desc)(.*?)\*/"
74 comments = re.findall(pattern, contents, re.DOTALL)
78 made_description =
False
79 with open(self.
out_dir +
"/" + md_filename,
"w")
as md_file:
81 for kind, comment
in comments:
82 comment_parts = comment.strip().split(
"Sources:", 1)
84 re.sub(
r"\n *",
"\n", comment_parts[0])
86 .replace(
"\n *",
"\n")
89 re.findall(
r"https?://[^\s]+", comment_parts[1])
90 if len(comment_parts) > 1
95 doxygen_ref = f
"{comment_text.lower()}_icp".replace(
"-",
"_")
96 md_file.write(f
"\\page {doxygen_ref} {comment_text} ICP\n")
98 register_pattern =
r"/\*\s*#register(.*?)\*/"
99 registers = re.findall(register_pattern, contents, re.DOTALL)
100 assert len(registers) == 1
101 register_name = registers[0].strip()
103 conf_pattern =
r'/\*\s*#conf\s+"([^"]*)"\s+(.*?)\*/'
104 confs = re.findall(conf_pattern, contents, re.DOTALL)
108 f
'\\par Usage\nYou can construct a new instance of {comment_text} ICP with `icp::ICP::from_method("{register_name}")`.'
112 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'
114 md_file.write(
"\nKey | Description\n--- | ---\n")
115 for key, descr
in confs:
116 descr_lines = descr.split(
"\n")
118 line.strip().removeprefix(
"*")
for line
in descr_lines
120 descr =
" ".join(cleaned_lines)
121 md_file.write(f
'`"{key}"` | {descr}\n')
122 elif kind ==
"#step":
123 if not made_description:
124 md_file.write(
"\n\\par Description\n")
125 made_description =
True
126 lines = comment_text.splitlines()
127 first_line_parts = lines[0].split(
":", 1)
128 if len(first_line_parts) == 2:
129 lines[0] = f
"**{first_line_parts[0]}**:{first_line_parts[1]}"
131 lines[0] = f
"**{first_line_parts[0]}**"
132 step_text =
"\n".join(
" " + line
for line
in lines)
133 md_file.write(f
"\n{step_cnt}. {step_text}\n")
136 md_file.write(
" Sources: \n")
137 for source
in sources:
138 md_file.write(f
" - {source}\n")
140 elif kind ==
"#desc":
141 icp_description = comment_text
142 md_file.write(f
"\n\\par Description\n{comment_text}\n")
143 made_description =
True
145 "\n\nRead \\ref icp_sources for a list of all resources used in this project."
148 f
"\nThis page was automatically generated from {file} with {os.path.basename(__file__)}."
150 return doxygen_ref, icp_description