diff --git a/tests/test_tldr.py b/tests/test_tldr.py index 9add07e..1d3c02f 100644 --- a/tests/test_tldr.py +++ b/tests/test_tldr.py @@ -24,6 +24,23 @@ def test_whole_page(): 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 + + # tldr adds a trailing newline + assert tldr_output == d_original + b"\n" + + 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 49e7ee5..aeaa62f 100755 --- a/tldr.py +++ b/tldr.py @@ -330,11 +330,16 @@ def colors_of(key: str) -> Tuple[str, str, List[str]]: return (color, on_color, attrs) -def output(page: str) -> None: - print() +def output(page: str, plain: bool = False) -> None: + if not plain: + 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 +461,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 +502,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()) + output(open_file.read().encode('utf-8').splitlines(), + plain=options.markdown) else: try: command = '-'.join(options.command) @@ -509,7 +520,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))