| 96 | |
| 97 | |
| 98 | class Code(ft.UserControl): |
| 99 | ttf_font_regex = re.compile( |
| 100 | r"^(https?:\/\/[^\s\/$.?#].[^\s]*\.ttf$|([a-zA-Z]:\\|\/)[^\s]*\.ttf$)" |
| 101 | ) |
| 102 | |
| 103 | def __init__( |
| 104 | self, |
| 105 | language="python", |
| 106 | code="", |
| 107 | font="https://github.com/JetBrains/JetBrainsMono/raw/master/fonts/ttf/JetBrainsMono-Regular.ttf", |
| 108 | theme=CodeTheme.GITHUB_DARK, |
| 109 | read_only=False, |
| 110 | height=600, |
| 111 | **kwargs, |
| 112 | ): |
| 113 | super().__init__(**kwargs) |
| 114 | self.language = language |
| 115 | self.code = code |
| 116 | self.font = font |
| 117 | self.read_only = read_only |
| 118 | self.theme = theme.value if isinstance(theme, Enum) else theme |
| 119 | self.height = height |
| 120 | self.syntax_rules = { |
| 121 | "python": { |
| 122 | "keywords": ( |
| 123 | r"\b(?P<KEYWORD>False|None|True|and|as|assert|async|await|break|class|continue|def|del|elif|else|except|finally|for|from|global|if|import|in|is|lambda|nonlocal|not|or|pass|raise|return|try|while|with|yield)\b", |
| 124 | self.theme.keyword, |
| 125 | ), |
| 126 | "exceptions": ( |
| 127 | r"([^.'\"\\#]\b|^)(?P<EXCEPTION>ArithmeticError|AssertionError|AttributeError|BaseException|BlockingIOError|BrokenPipeError|BufferError|BytesWarning|ChildProcessError|ConnectionAbortedError|ConnectionError|ConnectionRefusedError|ConnectionResetError|DeprecationWarning|EOFError|Ellipsis|EnvironmentError|Exception|FileExistsError|FileNotFoundError|FloatingPointError|FutureWarning|GeneratorExit|IOError|ImportError|ImportWarning|IndentationError|IndexError|InterruptedError|IsADirectoryError|KeyError|KeyboardInterrupt|LookupError|MemoryError|ModuleNotFoundError|NameError|NotADirectoryError|NotImplemented|NotImplementedError|OSError|OverflowError|PendingDeprecationWarning|PermissionError|ProcessLookupError|RecursionError|ReferenceError|ResourceWarning|RuntimeError|RuntimeWarning|StopAsyncIteration|StopIteration|SyntaxError|SyntaxWarning|SystemError|SystemExit|TabError|TimeoutError|TypeError|UnboundLocalError|UnicodeDecodeError|UnicodeEncodeError|UnicodeError|UnicodeTranslateError|UnicodeWarning|UserWarning|ValueError|Warning|WindowsError|ZeroDivisionError)\b", |
| 128 | self.theme.exception, |
| 129 | ), |
| 130 | "builtins": ( |
| 131 | r"([^.'\"\\#]\b|^)(?P<BUILTIN>abs|all|any|ascii|bin|breakpoint|callable|chr|classmethod|compile|complex|copyright|credits|delattr|dir|divmod|enumerate|eval|exec|exit|filter|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|isinstance|issubclass|iter|len|license|locals|map|max|memoryview|min|next|oct|open|ord|pow|print|quit|range|repr|reversed|round|set|setattr|slice|sorted|staticmethod|sum|type|vars|zip)\b", |
| 132 | self.theme.builtin, |
| 133 | ), |
| 134 | "docstrings": ( |
| 135 | r"(?P<DOCSTRING>(?i:r|u|f|fr|rf|b|br|rb)?'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?|(?i:r|u|f|fr|rf|b|br|rb)?\"\"\"[^\"\\]*((\\.|\"(?!\"\"))[^\"\\]*)*(\"\"\")?)", |
| 136 | self.theme.docstring, |
| 137 | ), |
| 138 | "strings": ( |
| 139 | r"(?P<STRING>(?i:r|u|f|fr|rf|b|br|rb)?'[^'\\\n]*(\\.[^'\\\n]*)*'?|(?i:r|u|f|fr|rf|b|br|rb)?\"[^\"\\\n]*(\\.[^\"\\\n]*)*\"?)", |
| 140 | self.theme.string, |
| 141 | ), |
| 142 | "types": ( |
| 143 | r"\b(?P<TYPES>bool|bytearray|bytes|dict|float|int|list|str|tuple|object)\b", |
| 144 | self.theme.type_annotation, |
| 145 | ), |
| 146 | "numbers": ( |
| 147 | r"\b(?P<NUMBER>((0x|0b|0o|#)[\da-fA-F]+)|((\d*\.)?\d+))\b", |
| 148 | self.theme.number, |
| 149 | ), |
| 150 | "function_calls": ( |
| 151 | r"\b(\w+)\s*(?=\()", # matches both standalone and dot-prefixed function calls |
| 152 | self.theme.function_call, |
| 153 | ), |
| 154 | "class_definitions": ( |
| 155 | r"(?<=\bclass)[ \t]+(?P<CLASSDEF>\w+)[ \t]*[:\(]", # recolor of DEFINITION for class definitions |