22from pygments import lex
33from pygments .lexers import get_lexer_by_name
44from pygments .token import Token
5+ from itertools import groupby
56
67COLORS = {
7- "background" : "#282C34 " ,
8- "line_number" : "#636D83 " ,
9- "text" : "#ABB2BF " ,
10- "keyword" : "#C678DD " ,
11- "string" : "#98C379 " ,
12- "comment" : "#7F848E " ,
13- "builtin" : "#7058a8 " ,
14- "name" : "#61AFEF " ,
15- "literal" : "#D19A66 " ,
8+ "background" : "#1E222A " ,
9+ "line_number" : "#4B5263 " ,
10+ "text" : "#D7DAE0 " ,
11+ "keyword" : "#C792EA " ,
12+ "string" : "#C3E88D " ,
13+ "comment" : "#5C6370 " ,
14+ "builtin" : "#82AAFF " ,
15+ "name" : "#89DDFF " ,
16+ "literal" : "#F78C6C " ,
1617}
1718
1819TOKEN_MAP = {
2829 Token .Literal : COLORS ["literal" ],
2930}
3031
31- CODE_SNIPPET = """
32- from pictex import Row, Text
33-
34- # Create a simple, styled component
35- def create_banner(text: str) -> Row:
36- '''This function demonstrates PicTex's power!'''
37- banner = Row(
38- Text(text).font_size(40).color("white")
39- ).padding(20).background_color("#5D3FD3")
40-
41- return banner
42-
43- # Render the final image
44- banner.render(create_banner("Hello, World! ✨")).show()
45- """
32+ CODE_SNIPPET = open ("code_snippet.py" , encoding = "utf-8" ).read ()
4633
4734def get_token_color (token ) -> str :
4835 current_token = token
@@ -56,8 +43,12 @@ def get_token_color(token) -> str:
5643def parse_python_line (line : str ) -> Row :
5744 python_lexer = get_lexer_by_name ("python" )
5845 tokens = lex (line , python_lexer )
46+ grouped_tokens = [
47+ (token_type , '' .join (token_text for _ , token_text in group ))
48+ for token_type , group in groupby (tokens , key = lambda t : t [0 ])
49+ ]
5950 line_children = []
60- for token_type , token_text in tokens :
51+ for token_type , token_text in grouped_tokens :
6152 color = get_token_color (token_type )
6253 line_children .append (Text (token_text ).color (color ))
6354
@@ -88,7 +79,7 @@ def parse_python_line(line: str) -> Row:
8879
8980canvas = (
9081 Canvas ()
91- .font_family ("Consolas " )
82+ .font_family ("FiraCode-VariableFont_wght.ttf " )
9283 .font_size (16 )
9384 .padding (60 )
9485 .background_color ("#757F9A" )
0 commit comments