Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/rich/markup.py: 21%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

119 statements  

1import re 

2from ast import literal_eval 

3from operator import attrgetter 

4from typing import Callable, Iterable, Match, NamedTuple 

5 

6from ._emoji_replace import _emoji_replace 

7from .emoji import EmojiVariant 

8from .errors import MarkupError 

9from .style import Style 

10from .text import Span, Text 

11 

12RE_TAGS = re.compile( 

13 r"""((\\*)\[([a-z#/@][^[]*?)])""", 

14 re.VERBOSE, 

15) 

16 

17RE_HANDLER = re.compile(r"^([\w.]*?)(\(.*?\))?$") 

18 

19 

20class Tag(NamedTuple): 

21 """A tag in console markup.""" 

22 

23 name: str 

24 """The tag name. e.g. 'bold'.""" 

25 parameters: str | None 

26 """Any additional parameters after the name.""" 

27 

28 def __str__(self) -> str: 

29 return ( 

30 self.name if self.parameters is None else f"{self.name} {self.parameters}" 

31 ) 

32 

33 @property 

34 def markup(self) -> str: 

35 """Get the string representation of this tag.""" 

36 return ( 

37 f"[{self.name}]" 

38 if self.parameters is None 

39 else f"[{self.name}={self.parameters}]" 

40 ) 

41 

42 

43_ReStringMatch = Match[str] # regex match object 

44_ReSubCallable = Callable[[_ReStringMatch], str] # Callable invoked by re.sub 

45_EscapeSubMethod = Callable[[_ReSubCallable, str], str] # Sub method of a compiled re 

46 

47 

48def escape( 

49 markup: str, 

50 _escape: _EscapeSubMethod = re.compile(r"(\\*)(\[[a-z#/@][^[]*?])").sub, 

51) -> str: 

52 """Escapes text so that it won't be interpreted as markup. 

53 

54 Args: 

55 markup (str): Content to be inserted in to markup. 

56 

57 Returns: 

58 str: Markup with square brackets escaped. 

59 """ 

60 

61 def escape_backslashes(match: Match[str]) -> str: 

62 """Called by re.sub replace matches.""" 

63 backslashes, text = match.groups() 

64 return f"{backslashes}{backslashes}\\{text}" 

65 

66 markup = _escape(escape_backslashes, markup) 

67 if markup.endswith("\\") and not markup.endswith("\\\\"): 

68 return markup + "\\" 

69 

70 return markup 

71 

72 

73def _parse(markup: str) -> Iterable[tuple[int, str | None, Tag | None]]: 

74 """Parse markup in to an iterable of tuples of (position, text, tag). 

75 

76 Args: 

77 markup (str): A string containing console markup 

78 

79 """ 

80 position = 0 

81 _divmod = divmod 

82 _Tag = Tag 

83 for match in RE_TAGS.finditer(markup): 

84 full_text, escapes, tag_text = match.groups() 

85 start, end = match.span() 

86 if start > position: 

87 yield start, markup[position:start], None 

88 if escapes: 

89 backslashes, escaped = _divmod(len(escapes), 2) 

90 if backslashes: 

91 # Literal backslashes 

92 yield start, "\\" * backslashes, None 

93 start += backslashes * 2 

94 if escaped: 

95 # Escape of tag 

96 yield start, full_text[len(escapes) :], None 

97 position = end 

98 continue 

99 text, equals, parameters = tag_text.partition("=") 

100 yield start, None, _Tag(text, parameters if equals else None) 

101 position = end 

102 if position < len(markup): 

103 yield position, markup[position:], None 

104 

105 

106def render( 

107 markup: str, 

108 style: str | Style = "", 

109 emoji: bool = True, 

110 emoji_variant: EmojiVariant | None = 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: (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) 

164 try: 

165 start, open_tag = pop_style(style_name) 

166 except KeyError: 

167 raise MarkupError( 

168 f"closing tag '{tag.markup}' at position {position} doesn't match any open tag" 

169 ) from None 

170 else: # implicit close 

171 try: 

172 start, open_tag = pop() 

173 except IndexError: 

174 raise MarkupError( 

175 f"closing tag '[/]' at position {position} has nothing to close" 

176 ) from None 

177 

178 if open_tag.name.startswith("@"): 

179 if open_tag.parameters: 

180 handler_name = "" 

181 parameters = open_tag.parameters.strip() 

182 handler_match = RE_HANDLER.match(parameters) 

183 if handler_match is not None: 

184 handler_name, match_parameters = handler_match.groups() 

185 parameters = ( 

186 "()" if match_parameters is None else match_parameters 

187 ) 

188 

189 try: 

190 meta_params = literal_eval(parameters) 

191 except SyntaxError as error: 

192 raise MarkupError( 

193 f"error parsing {parameters!r} in {open_tag.parameters!r}; {error.msg}" 

194 ) 

195 except Exception as error: 

196 raise MarkupError( 

197 f"error parsing {open_tag.parameters!r}; {error}" 

198 ) from None 

199 

200 if handler_name: 

201 meta_params = ( 

202 handler_name, 

203 ( 

204 meta_params 

205 if isinstance(meta_params, tuple) 

206 else (meta_params,) 

207 ), 

208 ) 

209 

210 else: 

211 meta_params = () 

212 

213 append_span( 

214 _Span( 

215 start, len(text), Style(meta={open_tag.name: meta_params}) 

216 ) 

217 ) 

218 else: 

219 append_span(_Span(start, len(text), str(open_tag))) 

220 

221 else: # Opening tag 

222 normalized_tag = _Tag(normalize(tag.name), tag.parameters) 

223 style_stack.append((len(text), normalized_tag)) 

224 

225 text_length = len(text) 

226 while style_stack: 

227 start, tag = style_stack.pop() 

228 style = str(tag) 

229 if style: 

230 append_span(_Span(start, text_length, style)) 

231 

232 text.spans = sorted(spans[::-1], key=attrgetter("start")) 

233 return text 

234 

235 

236if __name__ == "__main__": # pragma: no cover 

237 MARKUP = [ 

238 "[red]Hello World[/red]", 

239 "[magenta]Hello [b]World[/b]", 

240 "[bold]Bold[italic] bold and italic [/bold]italic[/italic]", 

241 "Click [link=https://www.willmcgugan.com]here[/link] to visit my Blog", 

242 ":warning-emoji: [bold red blink] DANGER![/]", 

243 ] 

244 

245 from rich import print 

246 from rich.table import Table 

247 

248 grid = Table("Markup", "Result", padding=(0, 1)) 

249 

250 for markup in MARKUP: 

251 grid.add_row(Text(markup), markup) 

252 

253 print(grid)