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

Function render

rich/markup.py:106–231  ·  view source on GitHub ↗

Render console markup in to a Text instance. Args: markup (str): A string containing console markup. style: (Union[str, Style]): The style to use. emoji (bool, optional): Also render emoji code. Defaults to True. emoji_variant (str, optional): Optional emoji vari

(
    markup: str,
    style: Union[str, Style] = "",
    emoji: bool = True,
    emoji_variant: Optional[EmojiVariant] = None,
)

Source from the content-addressed store, hash-verified

104
105
106def render(
107 markup: str,
108 style: Union[str, Style] = "",
109 emoji: bool = True,
110 emoji_variant: Optional[EmojiVariant] = None,
111) -> Text:
112 """Render console markup in to a Text instance.
113
114 Args:
115 markup (str): A string containing console markup.
116 style: (Union[str, Style]): The style to use.
117 emoji (bool, optional): Also render emoji code. Defaults to True.
118 emoji_variant (str, optional): Optional emoji variant, either "text" or "emoji". Defaults to None.
119
120
121 Raises:
122 MarkupError: If there is a syntax error in the markup.
123
124 Returns:
125 Text: A test instance.
126 """
127 emoji_replace = _emoji_replace
128 if "[" not in markup:
129 return Text(
130 emoji_replace(markup, default_variant=emoji_variant) if emoji else markup,
131 style=style,
132 )
133 text = Text(style=style)
134 append = text.append
135 normalize = Style.normalize
136
137 style_stack: List[Tuple[int, Tag]] = []
138 pop = style_stack.pop
139
140 spans: List[Span] = []
141 append_span = spans.append
142
143 _Span = Span
144 _Tag = Tag
145
146 def pop_style(style_name: str) -> Tuple[int, Tag]:
147 """Pop tag matching given style name."""
148 for index, (_, tag) in enumerate(reversed(style_stack), 1):
149 if tag.name == style_name:
150 return pop(-index)
151 raise KeyError(style_name)
152
153 for position, plain_text, tag in _parse(markup):
154 if plain_text is not None:
155 # Handle open brace escapes, where the brace is not part of a tag.
156 plain_text = plain_text.replace("\\[", "[")
157 append(emoji_replace(plain_text) if emoji else plain_text)
158 elif tag is not None:
159 if tag.name.startswith("/"): # Closing tag
160 style_name = tag.name[1:].strip()
161
162 if style_name: # explicit close
163 style_name = normalize(style_name)

Callers 15

test_renderFunction · 0.90
test_render_not_tagsFunction · 0.90
test_render_linkFunction · 0.90
test_render_combineFunction · 0.90
test_render_overlapFunction · 0.90
test_adjointFunction · 0.90
test_render_closeFunction · 0.90
test_markup_errorFunction · 0.90
test_markup_escapeFunction · 0.90
test_escape_escapeFunction · 0.90
test_eventsFunction · 0.90

Calls 9

TextClass · 0.85
_parseFunction · 0.85
pop_styleFunction · 0.85
MarkupErrorClass · 0.85
StyleClass · 0.85
replaceMethod · 0.80
matchMethod · 0.80
appendMethod · 0.45
popMethod · 0.45

Tested by 14

test_renderFunction · 0.72
test_render_not_tagsFunction · 0.72
test_render_linkFunction · 0.72
test_render_combineFunction · 0.72
test_render_overlapFunction · 0.72
test_adjointFunction · 0.72
test_render_closeFunction · 0.72
test_markup_errorFunction · 0.72
test_markup_escapeFunction · 0.72
test_escape_escapeFunction · 0.72
test_eventsFunction · 0.72