Skip to content

Commit 67e52a9

Browse files
authored
add option to print the raw markdown (#168)
1 parent de6c3d2 commit 67e52a9

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

tests/test_tldr.py

+17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ def test_whole_page():
2424
assert tldr_output == correct_output
2525

2626

27+
def test_markdown_mode():
28+
with open("tests/data/gem.md", "rb") as f_original:
29+
d_original = f_original.read()
30+
old_stdout = sys.stdout
31+
sys.stdout = io.StringIO()
32+
sys.stdout.buffer = types.SimpleNamespace()
33+
sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8"))
34+
tldr.output(d_original.splitlines(), plain=True)
35+
36+
sys.stdout.seek(0)
37+
tldr_output = sys.stdout.read().encode("utf-8")
38+
sys.stdout = old_stdout
39+
40+
# tldr adds a trailing newline
41+
assert tldr_output == d_original + b"\n"
42+
43+
2744
def test_error_message():
2845
with mock.patch("sys.argv", ["tldr", "73eb6f19cd6f"]):
2946
with pytest.raises(SystemExit) as pytest_wrapped_e:

tldr.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,16 @@ def colors_of(key: str) -> Tuple[str, str, List[str]]:
330330
return (color, on_color, attrs)
331331

332332

333-
def output(page: str) -> None:
334-
print()
333+
def output(page: str, plain: bool = False) -> None:
334+
if not plain:
335+
print()
335336
for line in page:
336337
line = line.rstrip().decode('utf-8')
337-
if len(line) == 0:
338+
339+
if plain:
340+
print(line)
341+
continue
342+
elif len(line) == 0:
338343
continue
339344
elif line[0] == '#':
340345
line = ' ' * LEADING_SPACES_NUM + \
@@ -456,6 +461,11 @@ def create_parser() -> ArgumentParser:
456461
type=str,
457462
help='Override the default language')
458463

464+
parser.add_argument('-m', '--markdown',
465+
default=False,
466+
action='store_true',
467+
help='Just print the plain page file.')
468+
459469
parser.add_argument(
460470
'command', type=str, nargs='*', help="command to lookup", metavar='command'
461471
).complete = {"bash": "shtab_tldr_cmd_list", "zsh": "shtab_tldr_cmd_list"}
@@ -492,7 +502,8 @@ def main() -> None:
492502
for command in options.command:
493503
if os.path.exists(command):
494504
with open(command, encoding='utf-8') as open_file:
495-
output(open_file.read().encode('utf-8').splitlines())
505+
output(open_file.read().encode('utf-8').splitlines(),
506+
plain=options.markdown)
496507
else:
497508
try:
498509
command = '-'.join(options.command)
@@ -509,7 +520,7 @@ def main() -> None:
509520
" send a pull request to: https://github.com/tldr-pages/tldr"
510521
).format(cmd=command))
511522
else:
512-
output(result)
523+
output(result, plain=options.markdown)
513524
except URLError as e:
514525
sys.exit("Error fetching from tldr: {}".format(e))
515526

0 commit comments

Comments
 (0)