Skip to content

Commit 672f794

Browse files
committed
added --page argument to markdown
1 parent 7e61cd2 commit 672f794

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.1.2] - Unreleased
9+
10+
### Added
11+
12+
- Added -p switch to python -m rich.markdown to page output
13+
814
## [1.1.1] - 2020-05-12
915

10-
## Changed
16+
### Changed
1117

1218
- Stripped cursor moving control codes from text
1319

rich/console.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,8 @@ def render_lines(
521521
Args:
522522
renderables (Iterable[RenderableType]): Any object or objects renderable in the console.
523523
options (Optional[ConsoleOptions]): Console options used to render with.
524+
style (Style, optional): Optional style to apply to renderables. Defaults to ``None``.
525+
pad (bool, optional): Pad lines shorter than render width. Defaults to ``True``.
524526
525527
Returns:
526528
List[List[Segment]]: A list of lines, where a line is a list of Segment objects.
@@ -549,7 +551,8 @@ def render_str(
549551
markup: bool = None,
550552
highlighter: HighlighterType = None,
551553
) -> "Text":
552-
"""Convert a string to a Text instance.
554+
"""Convert a string to a Text instance. This is is called automatically if
555+
you print or log a string.
553556
554557
Args:
555558
text (str): Text to render.
@@ -581,7 +584,7 @@ def render_str(
581584
def get_style(
582585
self, name: Union[str, Style], *, default: Union[Style, str] = None
583586
) -> Style:
584-
"""Get a style merged with the current style.
587+
"""Get a Style instance by it's theme name or parse a definition.
585588
586589
Args:
587590
name (str): The name of a style or a style definition.
@@ -598,7 +601,7 @@ def get_style(
598601

599602
try:
600603
style = self._styles.get(name)
601-
return style.copy() if style is not None else Style.parse(name)
604+
return style.copy() if style is not None else Style.parse(name)
602605
except errors.StyleSyntaxError as error:
603606
if default is not None:
604607
return self.get_style(default)

rich/markdown.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult
153153
)
154154
else:
155155
# Styled text for h2 and beyond
156-
if self.level:
156+
if self.level == 2:
157157
yield Text("\n")
158158
yield text
159159

@@ -525,16 +525,34 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult
525525
action="store_true",
526526
help="enable full text justify",
527527
)
528+
parser.add_argument(
529+
"-p",
530+
"--page",
531+
dest="page",
532+
action="store_true",
533+
help="use pager to scroll output",
534+
)
528535
args = parser.parse_args()
529536

530537
from rich.console import Console
531538

532-
console = Console(force_terminal=args.force_color, width=args.width)
533539
with open(args.path, "rt") as markdown_file:
534540
markdown = Markdown(
535541
markdown_file.read(),
536542
justify="full" if args.justify else "left",
537543
code_theme=args.code_theme,
538544
hyperlinks=args.hyperlinks,
539545
)
540-
console.print(markdown)
546+
if args.page:
547+
import pydoc
548+
import io
549+
550+
console = Console(
551+
file=io.StringIO(), force_terminal=args.force_color, width=args.width
552+
)
553+
console.print(markdown)
554+
pydoc.pager(console.file.getvalue()) # type: ignore
555+
556+
else:
557+
console = Console(force_terminal=args.force_color, width=args.width)
558+
console.print(markdown)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
import setuptools
66

77
if __name__ == "__main__":
8-
setuptools.setup()
8+
setuptools.setup(name="rich")

tests/test_markdown.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
def test_markdown_render():
1212
markdown = Markdown(MARKDOWN)
1313
rendered_markdown = render(markdown)
14-
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"
14+
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"
1515
assert rendered_markdown == expected
1616

1717

0 commit comments

Comments
 (0)