Skip to content

Commit

Permalink
Add CLI option to render package README.
Browse files Browse the repository at this point in the history
  • Loading branch information
tillahoffmann committed Jan 2, 2023
1 parent da1a5a4 commit a141a52
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
25 changes: 20 additions & 5 deletions readme_renderer/__main__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import argparse
import email
from readme_renderer.rst import render
from pkg_resources import get_distribution
import sys


if __name__ == '__main__':
def __main__(args=None):
parser = argparse.ArgumentParser(
description="Renders a .rst README to HTML",
)
parser.add_argument('input', help="Input README file",
type=argparse.FileType('r'))
parser.add_argument("-p", "--package", help="Get README from package metadata",
action="store_true")
parser.add_argument('input', help="Input README file or package name")
parser.add_argument('-o', '--output', help="Output file (default: stdout)",
type=argparse.FileType('w'), default='-')
args = parser.parse_args()
args = parser.parse_args(args)

rendered = render(args.input.read(), stream=sys.stderr)
if args.package:
distribution = get_distribution(args.input)
pkg_info = distribution.get_metadata(distribution.PKG_INFO)
message = email.message_from_string(pkg_info)
source = message.get_payload()
else:
with open(args.input) as fp:
source = fp.read()
rendered = render(source, stream=sys.stderr)
if rendered is None:
sys.exit(1)
print(rendered, file=args.output)


if __name__ == '__main__':
__main__()
39 changes: 39 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pathlib
import pytest
from readme_renderer.__main__ import __main__
import tempfile
from unittest import mock


@pytest.mark.parametrize("input_file", ["test_CommonMark_001.md", "test_rst_003.rst"])
@pytest.mark.parametrize("output_file", [False, True])
def test_cli_input_file(input_file, output_file):
input_file = pathlib.Path("tests/fixtures", input_file)

if output_file:
with tempfile.TemporaryDirectory() as tmpdir:
output = pathlib.Path(tmpdir) / "output.html"
__main__(["-o", str(output), str(input_file)])
with output.open() as fp:
result = fp.read()
else:
with mock.patch("builtins.print") as print_:
__main__([str(input_file)])
print_.assert_called_once()
(result,), _ = print_.call_args

with input_file.with_suffix(".html").open() as fp:
expected = fp.read()
assert result.strip() == expected.strip()


@pytest.mark.parametrize("package, contains", [
("readme_renderer", "Readme Renderer is a library that will safely render"),
("docutils", "Docutils is a modular system for processing documentation"),
])
def test_cli_package(package, contains):
with mock.patch("builtins.print") as print_:
__main__(["-p", package])
print_.assert_called_once()
(result,), _ = print_.call_args
assert contains in result

0 comments on commit a141a52

Please sign in to comment.