bd00866b8b95a98edc8956608e895a6329a944a0
[SubU] /
1 """
2     pygments.formatters.pangomarkup
3     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5     Formatter for Pango markup output.
6
7     :copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS.
8     :license: BSD, see LICENSE for details.
9 """
10
11 from pip._vendor.pygments.formatter import Formatter
12
13
14 __all__ = ['PangoMarkupFormatter']
15
16
17 _escape_table = {
18     ord('&'): '&',
19     ord('<'): '&lt;',
20 }
21
22
23 def escape_special_chars(text, table=_escape_table):
24     """Escape & and < for Pango Markup."""
25     return text.translate(table)
26
27
28 class PangoMarkupFormatter(Formatter):
29     """
30     Format tokens as Pango Markup code. It can then be rendered to an SVG.
31
32     .. versionadded:: 2.9
33     """
34
35     name = 'Pango Markup'
36     aliases = ['pango', 'pangomarkup']
37     filenames = []
38
39     def __init__(self, **options):
40         Formatter.__init__(self, **options)
41
42         self.styles = {}
43
44         for token, style in self.style:
45             start = ''
46             end = ''
47             if style['color']:
48                 start += '<span fgcolor="#%s">' % style['color']
49                 end = '</span>' + end
50             if style['bold']:
51                 start += '<b>'
52                 end = '</b>' + end
53             if style['italic']:
54                 start += '<i>'
55                 end = '</i>' + end
56             if style['underline']:
57                 start += '<u>'
58                 end = '</u>' + end
59             self.styles[token] = (start, end)
60
61     def format_unencoded(self, tokensource, outfile):
62         lastval = ''
63         lasttype = None
64
65         outfile.write('<tt>')
66
67         for ttype, value in tokensource:
68             while ttype not in self.styles:
69                 ttype = ttype.parent
70             if ttype == lasttype:
71                 lastval += escape_special_chars(value)
72             else:
73                 if lastval:
74                     stylebegin, styleend = self.styles[lasttype]
75                     outfile.write(stylebegin + lastval + styleend)
76                 lastval = escape_special_chars(value)
77                 lasttype = ttype
78
79         if lastval:
80             stylebegin, styleend = self.styles[lasttype]
81             outfile.write(stylebegin + lastval + styleend)
82
83         outfile.write('</tt>')