50 Extracts the documentation from the given file contents and writes it to a markdown file.
55 The name of the file to extract documentation from.
57 The contents of the file.
59 pattern =
r'/\*\s*(#step|#name|#desc)(.*?)\*/'
60 comments = re.findall(pattern, contents, re.DOTALL)
64 made_description =
False
65 with open(self.
out_dir +
'/' + md_filename,
'w')
as md_file:
67 for (kind, comment)
in comments:
68 comment_parts = comment.strip().split(
'Sources:', 1)
69 comment_text = re.sub(
r'\n *',
'\n', comment_parts[0]).replace(
'\n*',
'\n').replace(
'\n *',
'\n')
70 sources = re.findall(
r'https?://[^\s]+', comment_parts[1])
if len(comment_parts) > 1
else []
73 method_name = re.findall(
r'register_method\("([^"]*)', contents)[0]
74 md_file.write(f
"\page {comment_text.lower()}_icp {comment_text} ICP\n")
75 conf_pattern =
r'/\*\s*#conf\s+"([^"]*)"\s+(.*?)\*/'
76 confs = re.findall(conf_pattern, contents, re.DOTALL)
78 md_file.write(f
'\par Usage\nYou can construct a new instance of {comment_text} ICP with `icp::ICP::from_method("{method_name}")`.')
80 md_file.write(f
'\par Usage\nYou can construct a new instance of {comment_text} ICP with `icp::ICP::from_method("{method_name}", config)`. Supply the following parameters to `config` (via icp::ICP::Config::set):\n')
81 md_file.write(
'\nKey | Description\n--- | ---\n')
82 for (key, descr)
in confs:
83 descr = descr.replace(
'\n',
' ')
84 md_file.write(f
'`"{key}"` | {descr}\n')
86 if not made_description:
87 md_file.write(
'\n\par Description\n')
88 made_description =
True
89 lines = comment_text.splitlines()
90 first_line_parts = lines[0].split(
':', 1)
91 if len(first_line_parts) == 2:
92 lines[0] = f
"**{first_line_parts[0]}**:{first_line_parts[1]}"
94 lines[0] = f
"**{first_line_parts[0]}**"
95 step_text =
'\n'.join(
' ' + line
for line
in lines)
96 md_file.write(f
"\n{step_cnt}. {step_text}\n")
99 md_file.write(
' Sources: \n')
100 for source
in sources:
101 md_file.write(f
" - {source}\n")
103 elif kind ==
'#desc':
104 md_file.write(f
"\n\par Description\n{comment_text}\n")
105 made_description =
True
106 md_file.write(
'\n\nRead \\ref icp_sources for a list of all resources used in this project.')
107 md_file.write(f
"\nThis page was automatically generated from {file} with {os.path.basename(__file__)}.")