Skip to content

Commit cb0cef0

Browse files
committed
Modified
1 parent 42fbf37 commit cb0cef0

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

pybind11_mkdoc/mkdoc_lib.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def sanitize_name(name):
8585
return '__doc_' + name
8686

8787

88-
def process_comment(comment):
88+
def process_comment(comment, fmt='rst'):
8989
result = ''
9090

9191
# Remove C++ comment syntax
@@ -121,17 +121,22 @@ def process_comment(comment):
121121
s = re.sub(r'[\\@]em\s+%s' % cpp_group, r'*\1*', s)
122122
s = re.sub(r'[\\@]b\s+%s' % cpp_group, r'**\1**', s)
123123
s = re.sub(r'[\\@]ingroup\s+%s' % cpp_group, r'', s)
124-
s = re.sub(r'[\\@]param%s?\s+%s' % (param_group, cpp_group),
125-
r'\n\n$Parameter ``\2``:\n\n', s)
124+
if fmt == 'rst':
125+
s = re.sub(r'[\\@]param%s?\s+%s' % (param_group, cpp_group),
126+
r'\n\n$:param \2:\n\n', s)
127+
else:
128+
s = re.sub(r'[\\@]param%s?\s+%s' % (param_group, cpp_group),
129+
r'\n\n$Parameter ``\2``:\n\n', s)
126130
s = re.sub(r'[\\@]tparam%s?\s+%s' % (param_group, cpp_group),
127131
r'\n\n$Template parameter ``\2``:\n\n', s)
128132

129133
# Remove class and struct tags
130134
s = re.sub(r'[\\@](class|struct)\s+.*', '', s)
131135

136+
132137
for in_, out_ in {
133-
'returns': 'Returns',
134-
'return': 'Returns',
138+
'returns': ':return' if fmt == 'rst' else 'Returns',
139+
'return': ':return' if fmt == 'rst' else 'Returns',
135140
'authors': 'Authors',
136141
'author': 'Author',
137142
'copyright': 'Copyright',
@@ -140,15 +145,15 @@ def process_comment(comment):
140145
'sa': 'See also',
141146
'see': 'See also',
142147
'extends': 'Extends',
143-
'throws': 'Throws',
144-
'throw': 'Throws'
148+
'throws': ':raise' if fmt == 'rst' else 'Throws',
149+
'throw': ':raise' if fmt == 'rst' else 'Throws'
145150
}.items():
146151
s = re.sub(r'[\\@]%s\s*' % in_, r'\n\n$%s:\n\n' % out_, s)
147152

148153
s = re.sub(r'[\\@]details\s*', r'\n\n', s)
149154
s = re.sub(r'[\\@]brief\s*', r'', s)
150155
s = re.sub(r'[\\@]short\s*', r'', s)
151-
s = re.sub(r'[\\@]ref\s*', r'', s)
156+
# s = re.sub(r'[\\@]ref\s*', r'', s)
152157

153158
s = re.sub(r'[\\@]code\s?(.*?)\s?[\\@]endcode',
154159
r"```\n\1\n```\n", s, flags=re.DOTALL)
@@ -180,13 +185,17 @@ def process_comment(comment):
180185
wrapper = textwrap.TextWrapper()
181186
wrapper.expand_tabs = True
182187
wrapper.replace_whitespace = True
188+
# wrapper.replace_whitespace = False
183189
wrapper.drop_whitespace = True
190+
# wrapper.drop_whitespace = False
184191
wrapper.width = docstring_width
185192
wrapper.initial_indent = wrapper.subsequent_indent = ''
186193

187194
result = ''
188195
in_code_segment = False
189196
for x in re.split(r'(```)', s):
197+
198+
190199
if x == '```':
191200
if not in_code_segment:
192201
result += '```\n'
@@ -197,6 +206,9 @@ def process_comment(comment):
197206
result += x.strip()
198207
else:
199208
for y in re.split(r'(?: *\n *){2,}', x):
209+
r = re.search(r"^(\d+\.|\-#?|\*|\+)\s+", y)
210+
if r:
211+
wrapper.subsequent_indent = ' ' * r.end()
200212
wrapped = wrapper.fill(re.sub(r'\s+', ' ', y).strip())
201213
if len(wrapped) > 0 and wrapped[0] == '$':
202214
result += wrapped[1:] + '\n'

0 commit comments

Comments
 (0)