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,
)
| 104 | |
| 105 | |
| 106 | def 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) |