added --page argument to markdown

This commit is contained in:
Will McGugan 2020-05-12 21:25:39 +01:00
parent 7e61cd2fbc
commit 672f794dd4
5 changed files with 36 additions and 9 deletions

View file

@ -5,9 +5,15 @@ 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).
## [1.1.2] - Unreleased
### Added
- Added -p switch to python -m rich.markdown to page output
## [1.1.1] - 2020-05-12
## Changed
### Changed
- Stripped cursor moving control codes from text

View file

@ -521,6 +521,8 @@ class Console:
Args:
renderables (Iterable[RenderableType]): Any object or objects renderable in the console.
options (Optional[ConsoleOptions]): Console options used to render with.
style (Style, optional): Optional style to apply to renderables. Defaults to ``None``.
pad (bool, optional): Pad lines shorter than render width. Defaults to ``True``.
Returns:
List[List[Segment]]: A list of lines, where a line is a list of Segment objects.
@ -549,7 +551,8 @@ class Console:
markup: bool = None,
highlighter: HighlighterType = None,
) -> "Text":
"""Convert a string to a Text instance.
"""Convert a string to a Text instance. This is is called automatically if
you print or log a string.
Args:
text (str): Text to render.
@ -581,7 +584,7 @@ class Console:
def get_style(
self, name: Union[str, Style], *, default: Union[Style, str] = None
) -> Style:
"""Get a style merged with the current style.
"""Get a Style instance by it's theme name or parse a definition.
Args:
name (str): The name of a style or a style definition.
@ -598,7 +601,7 @@ class Console:
try:
style = self._styles.get(name)
return style.copy() if style is not None else Style.parse(name)
return style.copy() if style is not None else Style.parse(name)
except errors.StyleSyntaxError as error:
if default is not None:
return self.get_style(default)

View file

@ -153,7 +153,7 @@ class Heading(TextElement):
)
else:
# Styled text for h2 and beyond
if self.level:
if self.level == 2:
yield Text("\n")
yield text
@ -525,11 +525,17 @@ if __name__ == "__main__": # pragma: no cover
action="store_true",
help="enable full text justify",
)
parser.add_argument(
"-p",
"--page",
dest="page",
action="store_true",
help="use pager to scroll output",
)
args = parser.parse_args()
from rich.console import Console
console = Console(force_terminal=args.force_color, width=args.width)
with open(args.path, "rt") as markdown_file:
markdown = Markdown(
markdown_file.read(),
@ -537,4 +543,16 @@ if __name__ == "__main__": # pragma: no cover
code_theme=args.code_theme,
hyperlinks=args.hyperlinks,
)
console.print(markdown)
if args.page:
import pydoc
import io
console = Console(
file=io.StringIO(), force_terminal=args.force_color, width=args.width
)
console.print(markdown)
pydoc.pager(console.file.getvalue()) # type: ignore
else:
console = Console(force_terminal=args.force_color, width=args.width)
console.print(markdown)

View file

@ -5,4 +5,4 @@
import setuptools
if __name__ == "__main__":
setuptools.setup()
setuptools.setup(name="rich")

File diff suppressed because one or more lines are too long