@@ -137,12 +137,13 @@ def __new__(cls, text):
137
137
if match :
138
138
stl , * parts = match .groups ()
139
139
stl = stl .strip ("_" )
140
- part = next (filter (None , parts ))
141
- for l in part .splitlines ():
142
- self .tokens .append (self .ColoredToken (l , stl ))
143
- self .tokens .append (self .Token ("\n " ))
144
- if not part .endswith ("\n " ):
145
- del self .tokens [- 1 ]
140
+ part = next (filter (None , parts ), "" ) # default to "", in case there's no text in the token
141
+ if part :
142
+ for l in part .splitlines ():
143
+ self .tokens .append (self .ColoredToken (l , stl ))
144
+ self .tokens .append (self .Token ("\n " ))
145
+ if not part .endswith ("\n " ):
146
+ del self .tokens [- 1 ]
146
147
else :
147
148
self .tokens .append (self .Token (part ))
148
149
self .uncolored = "" .join (str .__str__ (token ) for token in self .tokens )
@@ -336,7 +337,8 @@ def colorize(text):
336
337
337
338
def _subfunc (match_obj ):
338
339
colorizer = Colorizer .from_markup (match_obj .group (1 ))
339
- return colorizer (next (filter (None , match_obj .groups ()[1 :])))
340
+ token = next (filter (None , match_obj .groups ()[1 :]), "" )
341
+ return colorizer (token ) if token else ""
340
342
341
343
return RE_PARSE_COLOR_MARKUP .sub (_subfunc , text )
342
344
0 commit comments