From 42cc68c5c76d1ad8984e1f331def1cce9540f008 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 30 Dec 2021 15:35:30 +0100 Subject: [PATCH] Deduplicate help block formatting code After fetching the help string, the bodies of `_format_description()` and `_format_epilog()` were identical. --- sphinx_click/ext.py | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/sphinx_click/ext.py b/sphinx_click/ext.py index c021f4b..b49b839 100644 --- a/sphinx_click/ext.py +++ b/sphinx_click/ext.py @@ -101,16 +101,7 @@ def _write_opts(opts): return ', '.join(rv), '\n'.join(out) -def _format_description(ctx): - """Format the description for a given `click.Command`. - - We parse this as reStructuredText, allowing users to embed rich - information in their help messages if they so choose. - """ - help_string = ctx.command.help or ctx.command.short_help - if not help_string: - return - +def _format_help(help_string): help_string = ANSI_ESC_SEQ_RE.sub('', help_string) bar_enabled = False @@ -127,6 +118,17 @@ def _format_description(ctx): yield '' +def _format_description(ctx): + """Format the description for a given `click.Command`. + + We parse this as reStructuredText, allowing users to embed rich + information in their help messages if they so choose. + """ + help_string = ctx.command.help or ctx.command.short_help + if help_string: + yield from _format_help(help_string) + + def _format_usage(ctx): """Format the usage for a `click.Command`.""" yield '.. code-block:: shell' @@ -236,23 +238,8 @@ def _format_epilog(ctx): We parse this as reStructuredText, allowing users to embed rich information in their help messages if they so choose. """ - if not ctx.command.epilog: - return - - bar_enabled = False - for line in statemachine.string2lines( - ANSI_ESC_SEQ_RE.sub('', ctx.command.epilog), - tab_width=4, - convert_whitespace=True, - ): - if line == '\b': - bar_enabled = True - continue - if line == '': - bar_enabled = False - line = '| ' + line if bar_enabled else line - yield line - yield '' + if ctx.command.epilog: + yield from _format_help(ctx.command.epilog) def _get_lazyload_commands(multicommand):