MCPcopy
hub / github.com/Textualize/rich / __rich_console__

Method __rich_console__

rich/markdown.py:579–704  ·  view source on GitHub ↗

Render markdown to the console.

(
        self, console: Console, options: ConsoleOptions
    )

Source from the content-addressed store, hash-verified

577 yield token
578
579 def __rich_console__(
580 self, console: Console, options: ConsoleOptions
581 ) -> RenderResult:
582 """Render markdown to the console."""
583 style = console.get_style(self.style, default="none")
584 options = options.update(height=None)
585 context = MarkdownContext(
586 console,
587 options,
588 style,
589 inline_code_lexer=self.inline_code_lexer,
590 inline_code_theme=self.inline_code_theme,
591 )
592 tokens = self.parsed
593 inline_style_tags = self.inlines
594 new_line = False
595 _new_line_segment = Segment.line()
596
597 for token in self._flatten_tokens(tokens):
598 node_type = token.type
599 tag = token.tag
600
601 entering = token.nesting == 1
602 exiting = token.nesting == -1
603 self_closing = token.nesting == 0
604
605 if node_type == "text":
606 context.on_text(token.content, node_type)
607 elif node_type == "hardbreak":
608 context.on_text("\n", node_type)
609 elif node_type == "softbreak":
610 context.on_text(" ", node_type)
611 elif node_type == "link_open":
612 href = str(token.attrs.get("href", ""))
613 if self.hyperlinks:
614 link_style = console.get_style("markdown.link_url", default="none")
615 link_style += Style(link=href)
616 context.enter_style(link_style)
617 else:
618 context.stack.push(Link.create(self, token))
619 elif node_type == "html_inline":
620 if token.content == "<kbd>":
621 kbd_style = console.get_style("markdown.kbd", default="bold")
622 context.enter_style(kbd_style)
623 elif token.content == "</kbd>":
624 context.leave_style()
625 else:
626 continue
627 elif node_type == "link_close":
628 if self.hyperlinks:
629 context.leave_style()
630 else:
631 element = context.stack.pop()
632 assert isinstance(element, Link)
633 link_style = console.get_style("markdown.link", default="none")
634 context.enter_style(link_style)
635 context.on_text(element.text.plain, node_type)
636 context.leave_style()

Callers

nothing calls this directly

Calls 15

_flatten_tokensMethod · 0.95
on_textMethod · 0.95
enter_styleMethod · 0.95
leave_styleMethod · 0.95
MarkdownContextClass · 0.85
StyleClass · 0.85
get_styleMethod · 0.80
updateMethod · 0.45
lineMethod · 0.45
getMethod · 0.45
pushMethod · 0.45
createMethod · 0.45

Tested by

no test coverage detected