Skip to content

Commit

Permalink
added --page argument to markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed May 12, 2020
1 parent 7e61cd2 commit 672f794
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions rich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ def render_lines(
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.
Expand Down Expand Up @@ -549,7 +551,8 @@ def render_str(
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.
Expand Down Expand Up @@ -581,7 +584,7 @@ def render_str(
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.
Expand All @@ -598,7 +601,7 @@ def get_style(

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)
Expand Down
24 changes: 21 additions & 3 deletions rich/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult
)
else:
# Styled text for h2 and beyond
if self.level:
if self.level == 2:
yield Text("\n")
yield text

Expand Down Expand Up @@ -525,16 +525,34 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult
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(),
justify="full" if args.justify else "left",
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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
import setuptools

if __name__ == "__main__":
setuptools.setup()
setuptools.setup(name="rich")
2 changes: 1 addition & 1 deletion tests/test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def test_markdown_render():
markdown = Markdown(MARKDOWN)
rendered_markdown = render(markdown)
expected = "╔══════════════════════════════════════════════════════════════════════════════════════════════════╗\n\x1b[1mHeading\x1b[0m ║\n╚══════════════════════════════════════════════════════════════════════════════════════════════════╝\n\n\n \x1b[1;4mSub-heading\x1b[0m \n\n\n \x1b[1mHeading\x1b[0m \n\n\n \x1b[1;2mH4 Heading\x1b[0m \n\n\n \x1b[4mH5 Heading\x1b[0m \n\n\n \x1b[3mH6 Heading\x1b[0m \n\nParagraphs are separated by a blank line. \n\nTwo spaces at the end of a line \nproduces a line break. \n\nText attributes \x1b[3mitalic\x1b[0m, \x1b[1mbold\x1b[0m, \x1b[38;5;15;40mmonospace\x1b[0m. \n\nHorizontal rule: \n\n\x1b[2m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m\nBullet list: \n\n\x1b[1;33m • \x1b[0mapples \n\x1b[1;33m • \x1b[0moranges \n\x1b[1;33m • \x1b[0mpears \n\nNumbered list: \n\n\x1b[1;33m 1 \x1b[0mlather \n\x1b[1;33m 2 \x1b[0mrinse \n\x1b[1;33m 3 \x1b[0mrepeat \n\nAn \x1b]8;id=3607223980;http://example.com\x1b\\\x1b[38;5;12mexample\x1b[0m\x1b]8;;\x1b\\. \n\n\x1b[35m▌ \x1b[0m\x1b[35mMarkdown uses email-style > characters for blockquoting.\x1b[0m\x1b[35m \x1b[0m\n\x1b[35m▌ \x1b[0m\x1b[35mLorem ipsum\x1b[0m\x1b[35m \x1b[0m\n\n🌆 \x1b]8;id=1358623393;https://github.com/willmcgugan/rich/raw/master/imgs/progress.gif\x1b\\progress\x1b]8;;\x1b\\ \n\n\x1b[2m┌──────────────────────────────────────────────────────────────────────────────────────────────────┐\x1b[0m\n\x1b[2m│\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34ma=1 \x1b[0m\x1b[2m│\x1b[0m\n\x1b[2m└──────────────────────────────────────────────────────────────────────────────────────────────────┘\x1b[0m\n\n\x1b[2m┌──────────────────────────────────────────────────────────────────────────────────────────────────┐\x1b[0m\n\x1b[2m│\x1b[0m\x1b[38;2;249;38;114;48;2;39;40;34mimport\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m \x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34mthis\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m \x1b[0m\x1b[2m│\x1b[0m\n\x1b[2m└──────────────────────────────────────────────────────────────────────────────────────────────────┘\x1b[0m\n\n\x1b[2m┌──────────────────────────────────────────────────────────────────────────────────────────────────┐\x1b[0m\n\x1b[2m│\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34mfoobar \x1b[0m\x1b[2m│\x1b[0m\n\x1b[2m└──────────────────────────────────────────────────────────────────────────────────────────────────┘\x1b[0m\n"
expected = "╔══════════════════════════════════════════════════════════════════════════════════════════════════╗\n\x1b[1mHeading\x1b[0m ║\n╚══════════════════════════════════════════════════════════════════════════════════════════════════╝\n\n\n \x1b[1;4mSub-heading\x1b[0m \n\n \x1b[1mHeading\x1b[0m \n\n \x1b[1;2mH4 Heading\x1b[0m \n\n \x1b[4mH5 Heading\x1b[0m \n\n \x1b[3mH6 Heading\x1b[0m \n\nParagraphs are separated by a blank line. \n\nTwo spaces at the end of a line \nproduces a line break. \n\nText attributes \x1b[3mitalic\x1b[0m, \x1b[1mbold\x1b[0m, \x1b[38;5;15;40mmonospace\x1b[0m. \n\nHorizontal rule: \n\n\x1b[2m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m\nBullet list: \n\n\x1b[1;33m • \x1b[0mapples \n\x1b[1;33m • \x1b[0moranges \n\x1b[1;33m • \x1b[0mpears \n\nNumbered list: \n\n\x1b[1;33m 1 \x1b[0mlather \n\x1b[1;33m 2 \x1b[0mrinse \n\x1b[1;33m 3 \x1b[0mrepeat \n\nAn \x1b]8;id=3607223980;http://example.com\x1b\\\x1b[38;5;12mexample\x1b[0m\x1b]8;;\x1b\\. \n\n\x1b[35m▌ \x1b[0m\x1b[35mMarkdown uses email-style > characters for blockquoting.\x1b[0m\x1b[35m \x1b[0m\n\x1b[35m▌ \x1b[0m\x1b[35mLorem ipsum\x1b[0m\x1b[35m \x1b[0m\n\n🌆 \x1b]8;id=1358623393;https://github.com/willmcgugan/rich/raw/master/imgs/progress.gif\x1b\\progress\x1b]8;;\x1b\\ \n\n\x1b[2m┌──────────────────────────────────────────────────────────────────────────────────────────────────┐\x1b[0m\n\x1b[2m│\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34ma=1 \x1b[0m\x1b[2m│\x1b[0m\n\x1b[2m└──────────────────────────────────────────────────────────────────────────────────────────────────┘\x1b[0m\n\n\x1b[2m┌──────────────────────────────────────────────────────────────────────────────────────────────────┐\x1b[0m\n\x1b[2m│\x1b[0m\x1b[38;2;249;38;114;48;2;39;40;34mimport\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m \x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34mthis\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34m \x1b[0m\x1b[2m│\x1b[0m\n\x1b[2m└──────────────────────────────────────────────────────────────────────────────────────────────────┘\x1b[0m\n\n\x1b[2m┌──────────────────────────────────────────────────────────────────────────────────────────────────┐\x1b[0m\n\x1b[2m│\x1b[0m\x1b[38;2;248;248;242;48;2;39;40;34mfoobar \x1b[0m\x1b[2m│\x1b[0m\n\x1b[2m└──────────────────────────────────────────────────────────────────────────────────────────────────┘\x1b[0m\n"
assert rendered_markdown == expected


Expand Down

0 comments on commit 672f794

Please sign in to comment.