3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-27 21:19:31 +01:00

ServiceBot: be more flexible in help formatting

* Fix text after indented docstring line breaks not showing (thanks @IotaSpencer for noticing this)
* Update formatting so that multiple consecutive newlines in a docstring are shown:
    - 2 newlines => 1 displayed new line
    - 3 newlines => 2 displayed new lines, and so on...
This commit is contained in:
James Lu 2017-02-15 17:06:16 -08:00
parent 902b246f96
commit ad873cfd7b

View File

@ -353,6 +353,14 @@ class ServiceBot():
""" """
self.reply(irc, text, private=private) self.reply(irc, text, private=private)
def _reply_format(next_line):
"""
Formats and outputs the given line.
"""
next_line = next_line.strip()
next_line = NORMALIZEWHITESPACE_RE.sub(' ', next_line)
_reply(next_line)
if command not in self.commands: if command not in self.commands:
_reply('Error: Unknown command %r.' % command) _reply('Error: Unknown command %r.' % command)
return return
@ -373,16 +381,33 @@ class ServiceBot():
_reply(args_desc.strip()) _reply(args_desc.strip())
if not shortform: if not shortform:
# Note: we handle newlines in docstrings a bit differently. Per # Note: we handle newlines in docstrings a bit differently. Per
# https://github.com/GLolol/PyLink/issues/307, only double newlines # https://github.com/GLolol/PyLink/issues/307, only double newlines (and
# have the effect of showing a new line on IRC. Single newlines # combinations of more) have the effect of showing a new line on IRC.
# are stripped so that word wrap can be applied in the source code # Single newlines are stripped so that word wrap can be applied in source
# without actually affecting the output on IRC. # code without affecting the output on IRC.
for line in doc.replace('\r', '').split('\n\n')[1:]: # TODO: we should probably verify that the output line doesn't exceed IRC
_reply(' ') # Empty line to break up output a bit. # line length limits...
real_line = line.replace('\n', ' ') next_line = ''
real_line = real_line.strip() for linenum, line in enumerate(lines[1:], 1):
real_line = NORMALIZEWHITESPACE_RE.sub(' ', real_line) stripped_line = line.strip()
_reply(real_line) log.debug("_show_command_help: Current line (%s): %r", linenum, stripped_line)
log.debug("_show_command_help: Last line (%s-1=%s): %r", linenum, linenum-1, lines[linenum-1].strip())
if stripped_line:
# If this line has content, join it with the previous one.
next_line += line.rstrip()
next_line += ' '
elif linenum > 0 and not lines[linenum-1].strip():
# The line before us was empty, so treat this one as a legitimate
# newline/break.
log.debug("_show_command_help: Adding an extra break...")
_reply(' ')
else:
# Otherwise, output it to IRC.
_reply_format(next_line)
next_line = '' # Reset the next line buffer
else:
_reply_format(next_line)
else: else:
_reply("Error: Command %r doesn't offer any help." % command) _reply("Error: Command %r doesn't offer any help." % command)
return return