-
Notifications
You must be signed in to change notification settings - Fork 90
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
Add CLI option to render package README. #271
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9a05b66
Add CLI option to render package README.
tillahoffmann 6b4b18b
Add `txt` and `md` rendering to CLI.
tillahoffmann 8ab6df1
Disable function name linting for `__main__`.
tillahoffmann f0b962b
Fix type annotations and rename `__main__`.
tillahoffmann 8cc6c59
Mock output file test for pypy.
tillahoffmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,65 @@ | ||
import argparse | ||
from readme_renderer.rst import render | ||
import email | ||
from readme_renderer.markdown import render as render_md | ||
from readme_renderer.rst import render as render_rst | ||
from readme_renderer.txt import render as render_txt | ||
import pathlib | ||
from pkg_resources import get_distribution | ||
import sys | ||
from typing import Optional, List | ||
|
||
|
||
if __name__ == '__main__': | ||
def main(cli_args: Optional[List[str]] = None) -> None: | ||
parser = argparse.ArgumentParser( | ||
description="Renders a .rst README to HTML", | ||
description="Renders a .md, .rst, or .txt 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("-f", "--format", choices=["md", "rst", "txt"], | ||
help="README format (inferred from input file name or package)") | ||
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(cli_args) | ||
|
||
content_format = args.format | ||
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() | ||
|
||
# Infer the format of the description from package metadata. | ||
if not content_format: | ||
content_type = message.get("Description-Content-Type", "text/x-rst") | ||
if content_type == "text/x-rst": | ||
content_format = "rst" | ||
elif content_type == "text/markdown": | ||
content_format = "md" | ||
elif content_type == "text/plain": | ||
content_format = "txt" | ||
else: | ||
raise ValueError(f"invalid content type {content_type} for package " | ||
"`long_description`") | ||
else: | ||
filename = pathlib.Path(args.input) | ||
content_format = content_format or filename.suffix.lstrip(".") | ||
with filename.open() as fp: | ||
source = fp.read() | ||
|
||
rendered = render(args.input.read(), stream=sys.stderr) | ||
if content_format == "md": | ||
rendered = render_md(source, stream=sys.stderr) | ||
elif content_format == "rst": | ||
rendered = render_rst(source, stream=sys.stderr) | ||
elif content_format == "txt": | ||
rendered = render_txt(source, stream=sys.stderr) | ||
else: | ||
raise ValueError(f"invalid README format: {content_format} (expected `md`, " | ||
"`rst`, or `txt`)") | ||
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,65 @@ | ||
import pathlib | ||
import pytest | ||
from readme_renderer.__main__ import main | ||
import tempfile | ||
from unittest import mock | ||
|
||
|
||
@pytest.fixture(params=["test_CommonMark_001.md", "test_rst_003.rst", | ||
"test_GFM_001.md", "test_txt_001.txt"]) | ||
def input_file(request): | ||
return pathlib.Path("tests/fixtures", request.param) | ||
|
||
|
||
@pytest.mark.parametrize("output_file", [False, True]) | ||
def test_cli_input_file(input_file, output_file): | ||
with mock.patch("builtins.print") as print_: | ||
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: | ||
main([str(input_file)]) | ||
|
||
print_.assert_called_once() | ||
(result,), kwargs = print_.call_args | ||
|
||
with input_file.with_suffix(".html").open() as fp: | ||
expected = fp.read() | ||
assert result.strip() == expected.strip() | ||
|
||
if output_file: | ||
assert kwargs["file"].name == str(output) | ||
|
||
|
||
def test_cli_invalid_format(): | ||
with mock.patch("pathlib.Path.open"), \ | ||
pytest.raises(ValueError, match="invalid README format: invalid"): | ||
main(["no-file.invalid"]) | ||
|
||
|
||
def test_cli_explicit_format(input_file): | ||
fmt = input_file.suffix.lstrip(".") | ||
with input_file.open() as fp, \ | ||
mock.patch("pathlib.Path.open", return_value=fp), \ | ||
mock.patch("builtins.print") as print_: | ||
main(["-f", fmt, "no-file.invalid"]) | ||
print_.assert_called_once() | ||
(result,), _ = print_.call_args | ||
|
||
with input_file.with_suffix(".html").open() as fp: | ||
assert result.strip() == fp.read().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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part isn't particularly well tested because we'd have to install a bunch of dummy packages for the tests to run. Or we could mock out
get_distribution
to return the right data (not quite as reliable a test if we get the mocking wrong).