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/), 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). 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 ## [9.7.0] - 2021-01-09
### Added ### Added

View File

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

View File

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