allow stdin on rich.markdown.__main__

If user provides no PATH or uses "-" as PATH, markdown is read from sys.stdin.

Example:
python -m rich.markdown < README.md
cat README.md | python -m rich.markdown
This commit is contained in:
Alexander Mancevice 2021-01-09 15:59:52 -05:00
parent 6b0fd05c92
commit d28cbf8878
3 changed files with 24 additions and 9 deletions

View File

@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [9.8.0]
- Allow passing markdown over STDIN when using `python -m rich.markdown`
## [9.7.0] - 2021-01-09
### Added

View File

@ -6,5 +6,6 @@ The following people have contributed to the development of Rich:
- [Oleksis Fraga](https://github.com/oleksis)
- [Hedy Li](https://github.com/hedythedev)
- [Alexander Mancevice](https://github.com/amancevice)
- [Will McGugan](https://github.com/willmcgugan)
- [Nathan Page](https://github.com/nathanrpage97)

View File

@ -527,11 +527,17 @@ class Markdown(JupyterMixin):
if __name__ == "__main__": # pragma: no cover
import argparse
import sys
parser = argparse.ArgumentParser(
description="Render Markdown to the console with Rich"
)
parser.add_argument("path", metavar="PATH", help="path to markdown file")
parser.add_argument(
"path",
metavar="PATH",
nargs="?",
help="path to markdown file",
)
parser.add_argument(
"-c",
"--force-color",
@ -587,14 +593,18 @@ if __name__ == "__main__": # pragma: no cover
from rich.console import Console
with open(args.path, "rt", encoding="utf-8") as markdown_file:
markdown = Markdown(
markdown_file.read(),
justify="full" if args.justify else "left",
code_theme=args.code_theme,
hyperlinks=args.hyperlinks,
inline_code_lexer=args.inline_code_lexer,
)
if not args.path or args.path == "-":
markdown_body = sys.stdin.read()
else:
with open(args.path, "rt", encoding="utf-8") as markdown_file:
markdown_body = markdown_file.read()
markdown = Markdown(
markdown_body,
justify="full" if args.justify else "left",
code_theme=args.code_theme,
hyperlinks=args.hyperlinks,
inline_code_lexer=args.inline_code_lexer,
)
if args.page:
import pydoc
import io