Skip to content

add the option to simply print the md file #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/test_tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 16 additions & 5 deletions tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 + \
Expand Down Expand Up @@ -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"}
Expand Down Expand Up @@ -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)
Expand All @@ -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))

Expand Down