def colourize(cls, code_line): """Returns a list containing markup tuples as used by urwid.Text widgets. :param code_line: a :class:`CodeLine` object to colourize """ if isinstance(code_line, FoldedCodeLine): return ('folded', ' ⋮') palette = code_line.lexer.palette ancestor_list = cls.palettes[palette].keys() output = [] if code_line.line_number >= 0: output.append(cls.line_number(code_line.line_number)) for part in code_line.parts: ancestor = token_ancestor(part.token, ancestor_list) key = f'{palette}_{ancestor}' if code_line.highlight: key += '_highlight' # Urwid uses a palette which has been built as a hash using the # names of the ancestor tokens as keys and the fg/bg colour # choices as values, each piece of marked up text is a tuple of # the palette key and the text to display markup = (key, part.text) output.append(markup) return output
def colourize(cls, code_line): """Returns a text string with RTF style tags. RTF uses a colour table, this styling assumes you're using the same colour table, available in the header in :attr:`RTFColourizer.rtf_header`. :param code_line: a :class:`CodeLine` object to colourize """ if isinstance(code_line, FoldedCodeLine): return '\\cf0 ⋮' palette = code_line.lexer.palette ancestor_list = cls.palettes[palette].keys() output = [] if code_line.line_number >= 0: output.append(cls.line_number(code_line.line_number)) for part in code_line.parts: text = rtf_encode(part.text) key = token_ancestor(part.token, ancestor_list) render = cls.palettes[palette][key] % text if code_line.highlight: render = cls.highlight % render output.append(render) return ''.join(output)
def test_tokens(self): #--- Test token_is_a() self.assertTrue(token_is_a(Token, Token)) self.assertTrue(token_is_a(Token.Name.Function, Token.Name)) self.assertFalse(token_is_a(Token.Name.Function, Token.Keyword)) #--- Test token_ancestor() token = token_ancestor(Token.Name.Function, [Token.Name.Function]) self.assertEqual(Token.Name.Function, token) token = token_ancestor(Token.Name.Function, [Token.Name]) self.assertEqual(Token.Name, token) token = token_ancestor(Token.Text, [Token.Name]) self.assertEqual(Token, token)
def colourize(cls, code_line): """Returns a text string with ANSI colour escapes corresponding to the tokens sent in :param code_line: a :class:`CodeLine` object to colourize """ if isinstance(code_line, FoldedCodeLine): fg = colored.fg('white') bold = colored.attr('bold') return f' {fg}{bold}⋮{cls.reset}' palette = code_line.lexer.palette ancestor_list = cls.palettes[palette].keys() bg = '' if code_line.highlight: bg = cls.highlight output = [] if code_line.line_number != -1: output.append(cls.line_number(code_line.line_number)) for part in code_line.parts: key = token_ancestor(part.token, ancestor_list) colours = cls.palettes[palette][key] fg = cls.get_code(colored.fg, colours[0]) attr = cls.get_code(colored.attr, colours[1]) output.append(f'{fg}{attr}{bg}{part.text}{cls.reset}') return ''.join(output)
def colourize(cls, code_line): """Returns a text string with ANSI colour escapes corresponding to the tokens sent in :param code_line: a :class:`CodeLine` object to colourize """ if isinstance(code_line, FoldedCodeLine): return '<span style="color: #000; font-weight: bold;">⋮</span>' palette = code_line.lexer.palette ancestor_list = cls.palettes[palette].keys() output = [] if code_line.line_number >= 0: output.append(HTMLColourizer.line_number(code_line.line_number)) if code_line.highlight: output.append(f'<span style="{cls.highlight}">') for part in code_line.parts: text = html.escape(part.text) key = token_ancestor(part.token, ancestor_list) style = cls.palettes[palette][key] if style: output.append(f'<span style="{style}">{text}</span>') else: output.append(text) if code_line.highlight: output.append('</span>') return ''.join(output)