Skip to content

Commit 61c2ace

Browse files
authored
Add ability to ignore ASCII art (#45)
1 parent f8ae972 commit 61c2ace

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,4 @@ Options:
134134
- `prog_name`: _(Optional, default: same as `command`)_ The name to display for the command.
135135
- `depth`: _(Optional, default: `0`)_ Offset to add when generating headers.
136136
- `style`: _(Optional, default: `plain`)_ Style for the options section. The possible choices are `plain` and `table`.
137+
- `remove_ascii_art`: _(Optional, default: `False`)_ When docstrings begin with the escape character `\b`, all text will be ignored until the next blank line is encountered.

mkdocs_click/_docs.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ def make_command_docs(
1414
command: click.BaseCommand,
1515
depth: int = 0,
1616
style: str = "plain",
17+
remove_ascii_art: bool = False,
1718
has_attr_list: bool = False,
1819
) -> Iterator[str]:
1920
"""Create the Markdown lines for a command and its sub-commands."""
2021
for line in _recursively_make_command_docs(
21-
prog_name, command, depth=depth, style=style, has_attr_list=has_attr_list
22+
prog_name, command, depth=depth, style=style, remove_ascii_art=remove_ascii_art, has_attr_list=has_attr_list
2223
):
23-
yield line.replace("\b", "")
24+
if line.strip() == "\b":
25+
continue
26+
27+
yield line
2428

2529

2630
def _recursively_make_command_docs(
@@ -29,13 +33,14 @@ def _recursively_make_command_docs(
2933
parent: click.Context = None,
3034
depth: int = 0,
3135
style: str = "plain",
36+
remove_ascii_art: bool = False,
3237
has_attr_list: bool = False,
3338
) -> Iterator[str]:
3439
"""Create the raw Markdown lines for a command and its sub-commands."""
3540
ctx = click.Context(cast(click.Command, command), info_name=prog_name, parent=parent)
3641

3742
yield from _make_title(ctx, depth, has_attr_list=has_attr_list)
38-
yield from _make_description(ctx)
43+
yield from _make_description(ctx, remove_ascii_art=remove_ascii_art)
3944
yield from _make_usage(ctx)
4045
yield from _make_options(ctx, style)
4146

@@ -103,12 +108,26 @@ def _make_title_full_command_path(ctx: click.Context, depth: int) -> Iterator[st
103108
yield ""
104109

105110

106-
def _make_description(ctx: click.Context) -> Iterator[str]:
111+
def _make_description(ctx: click.Context, remove_ascii_art: bool = False) -> Iterator[str]:
107112
"""Create markdown lines based on the command's own description."""
108113
help_string = ctx.command.help or ctx.command.short_help
109114

110115
if help_string:
111-
yield from help_string.splitlines()
116+
if remove_ascii_art:
117+
skipped_ascii_art = True
118+
for i, line in enumerate(help_string.splitlines()):
119+
if skipped_ascii_art is False:
120+
if not line.strip():
121+
skipped_ascii_art = True
122+
continue
123+
elif i == 0 and line.strip() == "\b":
124+
skipped_ascii_art = False
125+
126+
if skipped_ascii_art:
127+
yield line
128+
else:
129+
yield from help_string.splitlines()
130+
112131
yield ""
113132

114133

mkdocs_click/_extension.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ def replace_command_docs(has_attr_list: bool = False, **options: Any) -> Iterato
2323
prog_name = options.get("prog_name", None)
2424
depth = int(options.get("depth", 0))
2525
style = options.get("style", "plain")
26+
remove_ascii_art = options.get("remove_ascii_art", False)
2627

2728
command_obj = load_command(module, command)
2829

2930
prog_name = prog_name or command_obj.name or command
3031

3132
return make_command_docs(
32-
prog_name=prog_name, command=command_obj, depth=depth, style=style, has_attr_list=has_attr_list
33+
prog_name=prog_name,
34+
command=command_obj,
35+
depth=depth,
36+
style=style,
37+
remove_ascii_art=remove_ascii_art,
38+
has_attr_list=has_attr_list,
3339
)
3440

3541

tests/test_docs.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@ def hello():
1616
"""Hello, world!"""
1717

1818

19+
@click.command()
20+
@click.option("-d", "--debug", help="Include debug output")
21+
def hello_escape_marker():
22+
"""
23+
\b
24+
Hello, world!
25+
"""
26+
27+
28+
@click.command()
29+
@click.option("-d", "--debug", help="Include debug output")
30+
def hello_ascii_art():
31+
"""
32+
\b
33+
______ __ __ ______ __ ___
34+
/ || | | | / || |/ /
35+
| ,----'| | | | | ,----'| ' /
36+
| | | | | | | | | <
37+
| `----.| `----.| | | `----.| . \\
38+
\\______||_______||__| \\______||__|\\__\\
39+
40+
Hello, world!
41+
"""
42+
43+
1944
HELLO_EXPECTED = dedent(
2045
"""
2146
# hello
@@ -60,6 +85,16 @@ def test_style_invalid():
6085
list(make_command_docs("hello", hello, style="invalid"))
6186

6287

88+
def test_basic_escape_marker():
89+
output = "\n".join(make_command_docs("hello", hello_escape_marker))
90+
assert output == HELLO_EXPECTED
91+
92+
93+
def test_basic_ascii_art():
94+
output = "\n".join(make_command_docs("hello", hello_ascii_art, remove_ascii_art=True))
95+
assert output == HELLO_EXPECTED
96+
97+
6398
@click.command()
6499
@click.option("-d", "--debug", help="Include debug output")
65100
@click.option("--choice", type=click.Choice(["foo", "bar"]), default="foo")

0 commit comments

Comments
 (0)