tools: Fix formatting of lists in capi comments

This commit is contained in:
Sergey Markelov 2024-01-05 14:57:47 -05:00 committed by Marshall Greenblatt
parent 784302280b
commit 9f6995f839
1 changed files with 29 additions and 13 deletions

View File

@ -19,15 +19,19 @@ def notify(msg):
sys.stdout.write(' NOTE: ' + msg + '\n') sys.stdout.write(' NOTE: ' + msg + '\n')
def wrap_text(text, indent='', maxchars=80): def wrap_text(text, indent='', maxchars=80, listitem=False):
""" Wrap the text to the specified number of characters. If """ Wrap the text to the specified number of characters. If
necessary a line will be broken and wrapped after a word. necessary a line will be broken and wrapped after a word.
""" """
result = '' if listitem:
lines = textwrap.wrap(text, maxchars - len(indent)) initial_indent=indent + '- '
for line in lines: subsequent_indent=indent+' '
result += indent + line + '\n' else:
return result initial_indent=indent
subsequent_indent=indent
lines = textwrap.wrap(text, maxchars, initial_indent=initial_indent,
subsequent_indent=subsequent_indent)
return '\n'.join(lines) + '\n'
def is_base_class(clsname): def is_base_class(clsname):
@ -143,6 +147,7 @@ def format_comment(comment, indent, translate_map=None, maxchars=80):
result = '' result = ''
wrapme = '' wrapme = ''
hasemptyline = False hasemptyline = False
listitem = False
for line in comment: for line in comment:
# if the line starts with a leading space, remove that space # if the line starts with a leading space, remove that space
if not line is None and len(line) > 0 and line[0] == ' ': if not line is None and len(line) > 0 and line[0] == ' ':
@ -151,19 +156,28 @@ def format_comment(comment, indent, translate_map=None, maxchars=80):
else: else:
didremovespace = False didremovespace = False
if line is None or len(line) == 0 or line[0] == ' ': if line is None or len(line) == 0 or (line[0] == ' ' and not listitem) \
# the previous paragraph, if any, has ended or line[0] == '-':
# the previous paragraph or list item, if any, has ended
if len(wrapme) > 0: if len(wrapme) > 0:
if not translate_map is None: if not translate_map is None:
# apply the translation # apply the translation
for key in translate_keys: for key in translate_keys:
wrapme = wrapme.replace(key, translate_map[key]) wrapme = wrapme.replace(key, translate_map[key])
# output the previous paragraph # output the previous paragraph
result += wrap_text(wrapme, indent + '/// ', maxchars) result += wrap_text(wrapme, indent + '/// ', maxchars, listitem)
wrapme = '' wrapme = ''
listitem = False
if not line is None: if not line is None:
if len(line) == 0 or line[0] == ' ': if len(line) > 0 and line[0] == '-':
# list item
listitem = True
wrapme = line[1:].strip()
if len(line) > 0 and line[0] == ' ' and listitem:
# list item continues
wrapme += line[1:]
if len(line) == 0 or (line[0] == ' ' and not listitem):
# blank lines or anything that's further indented should be # blank lines or anything that's further indented should be
# output as-is # output as-is
result += indent + '///' result += indent + '///'
@ -173,7 +187,9 @@ def format_comment(comment, indent, translate_map=None, maxchars=80):
else: else:
result += line result += line
result += '\n' result += '\n'
listitem = False;
else: else:
if not listitem:
# add to the current paragraph # add to the current paragraph
wrapme += line + ' ' wrapme += line + ' '
else: else:
@ -187,7 +203,7 @@ def format_comment(comment, indent, translate_map=None, maxchars=80):
for key in translate_map.keys(): for key in translate_map.keys():
wrapme = wrapme.replace(key, translate_map[key]) wrapme = wrapme.replace(key, translate_map[key])
# output the previous paragraph # output the previous paragraph
result += wrap_text(wrapme, indent + '/// ', maxchars) result += wrap_text(wrapme, indent + '/// ', maxchars, listitem)
if hasemptyline: if hasemptyline:
# an empty line means a break between comments, so the comment is # an empty line means a break between comments, so the comment is