-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI option to render package README.
- Loading branch information
1 parent
da1a5a4
commit a141a52
Showing
2 changed files
with
59 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |