From 0867638174d22b44d49afcd95e88d84e401e06da Mon Sep 17 00:00:00 2001 From: dadav <33197631+dadav@users.noreply.github.com> Date: Tue, 12 Oct 2021 19:47:09 +0200 Subject: [PATCH 1/7] add the option to simply print the md file --- tldr.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tldr.py b/tldr.py index 49e7ee5..48b5f32 100755 --- a/tldr.py +++ b/tldr.py @@ -330,11 +330,15 @@ def colors_of(key: str) -> Tuple[str, str, List[str]]: return (color, on_color, attrs) -def output(page: str) -> None: +def output(page: str, plain: bool = False) -> None: print() for line in page: line = line.rstrip().decode('utf-8') - if len(line) == 0: + + if plain: + print(line) + continue + elif len(line) == 0: continue elif line[0] == '#': line = ' ' * LEADING_SPACES_NUM + \ @@ -456,6 +460,11 @@ def create_parser() -> ArgumentParser: type=str, help='Override the default language') + parser.add_argument('-m', '--markdown', + default=False, + action='store_true', + help='Just print the plain page file.') + parser.add_argument( 'command', type=str, nargs='*', help="command to lookup", metavar='command' ).complete = {"bash": "shtab_tldr_cmd_list", "zsh": "shtab_tldr_cmd_list"} @@ -492,7 +501,7 @@ def main() -> None: for command in options.command: if os.path.exists(command): with open(command, encoding='utf-8') as open_file: - output(open_file.read().encode('utf-8').splitlines()) + output(open_file.read().encode('utf-8').splitlines(), plain=options.markdown) else: try: command = '-'.join(options.command) @@ -509,7 +518,7 @@ def main() -> None: " send a pull request to: https://github.com/tldr-pages/tldr" ).format(cmd=command)) else: - output(result) + output(result, plain=options.markdown) except URLError as e: sys.exit("Error fetching from tldr: {}".format(e)) From 0f3cc3e2b55d4324c5c1b25473244f94b15d8616 Mon Sep 17 00:00:00 2001 From: dadav <33197631+dadav@users.noreply.github.com> Date: Tue, 12 Oct 2021 20:01:55 +0200 Subject: [PATCH 2/7] remove leading newline on md mode --- tldr.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tldr.py b/tldr.py index 48b5f32..840ad47 100755 --- a/tldr.py +++ b/tldr.py @@ -331,7 +331,8 @@ def colors_of(key: str) -> Tuple[str, str, List[str]]: def output(page: str, plain: bool = False) -> None: - print() + if not plain: + print() for line in page: line = line.rstrip().decode('utf-8') From 367f1a30094683735e0988d93339012c55c14e52 Mon Sep 17 00:00:00 2001 From: dadav <33197631+dadav@users.noreply.github.com> Date: Tue, 12 Oct 2021 20:43:19 +0200 Subject: [PATCH 3/7] add markdown mode test --- tests/test_tldr.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_tldr.py b/tests/test_tldr.py index 9add07e..d3ed522 100644 --- a/tests/test_tldr.py +++ b/tests/test_tldr.py @@ -23,6 +23,20 @@ def test_whole_page(): correct_output = f_rendered.read() assert tldr_output == correct_output +def test_markdown_mode(): + with open("tests/data/gem.md", "rb") as f_original: + d_original = f_original.read() + old_stdout = sys.stdout + sys.stdout = io.StringIO() + sys.stdout.buffer = types.SimpleNamespace() + sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8")) + tldr.output(d_original.splitlines(), plain=True) + + sys.stdout.seek(0) + tldr_output = sys.stdout.read().encode("utf-8") + sys.stdout = old_stdout + + assert tldr_output == d_original def test_error_message(): with mock.patch("sys.argv", ["tldr", "73eb6f19cd6f"]): From a4931be7dc7c5a8961ce2ba085cf5f9dc0973295 Mon Sep 17 00:00:00 2001 From: dadav <33197631+dadav@users.noreply.github.com> Date: Tue, 12 Oct 2021 20:43:33 +0200 Subject: [PATCH 4/7] dont print final newline in markdown mode --- tldr.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tldr.py b/tldr.py index 840ad47..9ecc43c 100755 --- a/tldr.py +++ b/tldr.py @@ -369,7 +369,8 @@ def output(page: str, plain: bool = False) -> None: elements.append(item) sys.stdout.buffer.write(''.join(elements).encode('utf-8')) print() - print() + if not plain: + print() def update_cache(language: Optional[List[str]] = None) -> None: From 4d34a251ce8cbd4bdf452e00f8e77ae943d49de1 Mon Sep 17 00:00:00 2001 From: dadav <33197631+dadav@users.noreply.github.com> Date: Tue, 12 Oct 2021 20:46:27 +0200 Subject: [PATCH 5/7] fix linting --- tests/test_tldr.py | 2 ++ tldr.py | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_tldr.py b/tests/test_tldr.py index d3ed522..865abbf 100644 --- a/tests/test_tldr.py +++ b/tests/test_tldr.py @@ -23,6 +23,7 @@ def test_whole_page(): correct_output = f_rendered.read() assert tldr_output == correct_output + def test_markdown_mode(): with open("tests/data/gem.md", "rb") as f_original: d_original = f_original.read() @@ -38,6 +39,7 @@ def test_markdown_mode(): assert tldr_output == d_original + def test_error_message(): with mock.patch("sys.argv", ["tldr", "73eb6f19cd6f"]): with pytest.raises(SystemExit) as pytest_wrapped_e: diff --git a/tldr.py b/tldr.py index 9ecc43c..73713e6 100755 --- a/tldr.py +++ b/tldr.py @@ -503,7 +503,8 @@ def main() -> None: for command in options.command: if os.path.exists(command): with open(command, encoding='utf-8') as open_file: - output(open_file.read().encode('utf-8').splitlines(), plain=options.markdown) + output(open_file.read().encode('utf-8').splitlines(), + plain=options.markdown) else: try: command = '-'.join(options.command) From f29106202984081569709da4d76827629481c450 Mon Sep 17 00:00:00 2001 From: dadav <33197631+dadav@users.noreply.github.com> Date: Wed, 13 Oct 2021 17:32:51 +0200 Subject: [PATCH 6/7] Revert "dont print final newline in markdown mode" This reverts commit a4931be7dc7c5a8961ce2ba085cf5f9dc0973295. --- tldr.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tldr.py b/tldr.py index 73713e6..aeaa62f 100755 --- a/tldr.py +++ b/tldr.py @@ -369,8 +369,7 @@ def output(page: str, plain: bool = False) -> None: elements.append(item) sys.stdout.buffer.write(''.join(elements).encode('utf-8')) print() - if not plain: - print() + print() def update_cache(language: Optional[List[str]] = None) -> None: From 38bb67fbc5bbe5d25655abb53d91d5275a7725f4 Mon Sep 17 00:00:00 2001 From: dadav <33197631+dadav@users.noreply.github.com> Date: Wed, 13 Oct 2021 17:36:10 +0200 Subject: [PATCH 7/7] adjust test to trailing newline --- tests/test_tldr.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_tldr.py b/tests/test_tldr.py index 865abbf..1d3c02f 100644 --- a/tests/test_tldr.py +++ b/tests/test_tldr.py @@ -37,7 +37,8 @@ def test_markdown_mode(): tldr_output = sys.stdout.read().encode("utf-8") sys.stdout = old_stdout - assert tldr_output == d_original + # tldr adds a trailing newline + assert tldr_output == d_original + b"\n" def test_error_message():