Debian Codegraph @

docutils/writers/odf_odt/pygmentsformatter.py

raw symbols SCIP
1 # $Id: pygmentsformatter.py 10136 2025-05-20 15:48:27Z milde $
2 # Author: Dave Kuhlman <[email protected]>
3 # Copyright: This module has been placed in the public domain.
4
5 """Additional support for Pygments formatter."""
6
7 from __future__ import annotations
8
9 import pygments
10 import pygments.formatter
11
12
13 class OdtPygmentsFormatter(pygments.formatter.Formatter):
14 def __init__(self, rststyle_function, escape_function) -> None:
15 pygments.formatter.Formatter.__init__(self)
16 self.rststyle_function = rststyle_function
17 self.escape_function = escape_function
18
19 def rststyle(self, name, parameters=()):
20 return self.rststyle_function(name, parameters)
21
22
23 class OdtPygmentsProgFormatter(OdtPygmentsFormatter):
24 def format(self, tokensource, outfile) -> None:
25 tokenclass = pygments.token.Token
26 for ttype, value in tokensource:
27 value = self.escape_function(value)
28 if ttype == tokenclass.Keyword:
29 s2 = self.rststyle('codeblock-keyword')
30 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
31 (s2, value, )
32 elif ttype == tokenclass.Literal.String:
33 s2 = self.rststyle('codeblock-string')
34 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
35 (s2, value, )
36 elif ttype in (
37 tokenclass.Literal.Number.Integer,
38 tokenclass.Literal.Number.Integer.Long,
39 tokenclass.Literal.Number.Float,
40 tokenclass.Literal.Number.Hex,
41 tokenclass.Literal.Number.Oct,
42 tokenclass.Literal.Number,
43 ):
44 s2 = self.rststyle('codeblock-number')
45 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
46 (s2, value, )
47 elif ttype == tokenclass.Operator:
48 s2 = self.rststyle('codeblock-operator')
49 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
50 (s2, value, )
51 elif ttype == tokenclass.Comment:
52 s2 = self.rststyle('codeblock-comment')
53 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
54 (s2, value, )
55 elif ttype == tokenclass.Name.Class:
56 s2 = self.rststyle('codeblock-classname')
57 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
58 (s2, value, )
59 elif ttype == tokenclass.Name.Function:
60 s2 = self.rststyle('codeblock-functionname')
61 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
62 (s2, value, )
63 elif ttype == tokenclass.Name:
64 s2 = self.rststyle('codeblock-name')
65 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
66 (s2, value, )
67 else:
68 s1 = value
69 outfile.write(s1)
70
71
72 class OdtPygmentsLaTeXFormatter(OdtPygmentsFormatter):
73 def format(self, tokensource, outfile) -> None:
74 tokenclass = pygments.token.Token
75 for ttype, value in tokensource:
76 value = self.escape_function(value)
77 if ttype == tokenclass.Keyword:
78 s2 = self.rststyle('codeblock-keyword')
79 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
80 (s2, value, )
81 elif ttype in (tokenclass.Literal.String,
82 tokenclass.Literal.String.Backtick,
83 ):
84 s2 = self.rststyle('codeblock-string')
85 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
86 (s2, value, )
87 elif ttype == tokenclass.Name.Attribute:
88 s2 = self.rststyle('codeblock-operator')
89 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
90 (s2, value, )
91 elif ttype == tokenclass.Comment:
92 if value[-1] == '\n':
93 s2 = self.rststyle('codeblock-comment')
94 s1 = '<text:span text:style-name="%s">%s</text:span>\n' % \
95 (s2, value[:-1], )
96 else:
97 s2 = self.rststyle('codeblock-comment')
98 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
99 (s2, value, )
100 elif ttype == tokenclass.Name.Builtin:
101 s2 = self.rststyle('codeblock-name')
102 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
103 (s2, value, )
104 else:
105 s1 = value
106 outfile.write(s1)