Skip to content

Commit 1021cac

Browse files
committed
Minor improvements to usage message reformatting
1 parent 1df92d7 commit 1021cac

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

humanfriendly/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from humanfriendly.compat import is_string
3838

3939
# Semi-standard module versioning.
40-
__version__ = '1.44.6'
40+
__version__ = '1.44.7'
4141

4242
# Spinners are redrawn at most this many seconds.
4343
minimum_spinner_interval = 0.2

humanfriendly/tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Tests for the `humanfriendly' package.
55
#
66
# Author: Peter Odding <peter.odding@paylogic.eu>
7-
# Last Change: March 20, 2016
7+
# Last Change: April 21, 2016
88
# URL: https://humanfriendly.readthedocs.org
99

1010
"""Test suite for the `humanfriendly` package."""
@@ -748,13 +748,13 @@ def test_render_usage(self):
748748
$ echo test
749749
test
750750
"""))
751-
assert all(token in render_usage("""
751+
assert all(token in render_usage(dedent("""
752752
Supported options:
753753
754754
-n, --dry-run
755755
756756
Don't change anything.
757-
""") for token in ('`-n`', '`--dry-run`'))
757+
""")) for token in ('`-n`', '`--dry-run`'))
758758

759759
def test_sphinx_customizations(self):
760760
"""Test the :mod:`humanfriendly.sphinx` module."""

humanfriendly/usage.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,17 +308,29 @@ def render_paragraph(paragraph, meta_variables):
308308
return "**%s**" % paragraph
309309
# Reformat shell transcripts into code blocks.
310310
if re.match(r'^\s*\$\s+\S', paragraph):
311-
lines = [' %s' % line for line in paragraph.splitlines()]
311+
# Split the paragraph into lines.
312+
lines = paragraph.splitlines()
313+
# Check if the paragraph is already indented.
314+
if not paragraph[0].isspace():
315+
# If the paragraph isn't already indented we'll indent it now.
316+
lines = [' %s' % line for line in lines]
312317
lines.insert(0, '.. code-block:: sh')
313318
lines.insert(1, '')
314319
return "\n".join(lines)
315-
# Change `quoting' so it doesn't trip up DocUtils.
316-
paragraph = re.sub("`(.+?)'", r'"\1"', paragraph)
317-
# Escape asterisks.
318-
paragraph = paragraph.replace('*', r'\*')
319-
# Reformat inline tokens.
320-
return replace_special_tokens(paragraph, meta_variables,
321-
lambda token: '``%s``' % token)
320+
# The following reformatting applies only to paragraphs which are not
321+
# indented. Yes this is a hack - for now we assume that indented paragraphs
322+
# are code blocks, even though this assumption can be wrong.
323+
if not paragraph[0].isspace():
324+
# Change UNIX style `quoting' so it doesn't trip up DocUtils.
325+
paragraph = re.sub("`(.+?)'", r'"\1"', paragraph)
326+
# Escape asterisks.
327+
paragraph = paragraph.replace('*', r'\*')
328+
# Reformat inline tokens.
329+
paragraph = replace_special_tokens(
330+
paragraph, meta_variables,
331+
lambda token: '``%s``' % token,
332+
)
333+
return paragraph
322334

323335

324336
def replace_special_tokens(text, meta_variables, replace_fn):

0 commit comments

Comments
 (0)