2 # Author: Dave Kuhlman <dkuhlman@rexx.com>
3 # Copyright: This module has been placed in the public domain.
7 Additional support for Pygments formatter.
13 import pygments
.formatter
16 class OdtPygmentsFormatter(pygments
.formatter
.Formatter
):
17 def __init__(self
, rststyle_function
, escape_function
):
18 pygments
.formatter
.Formatter
.__init
__(self
)
19 self
.rststyle_function
= rststyle_function
20 self
.escape_function
= escape_function
22 def rststyle(self
, name
, parameters
=( )):
23 return self
.rststyle_function(name
, parameters
)
26 class OdtPygmentsProgFormatter(OdtPygmentsFormatter
):
27 def format(self
, tokensource
, outfile
):
28 tokenclass
= pygments
.token
.Token
29 for ttype
, value
in tokensource
:
30 value
= self
.escape_function(value
)
31 if ttype
== tokenclass
.Keyword
:
32 s2
= self
.rststyle('codeblock-keyword')
33 s1
= '<text:span text:style-name="%s">%s</text:span>' % \
35 elif ttype
== tokenclass
.Literal
.String
:
36 s2
= self
.rststyle('codeblock-string')
37 s1
= '<text:span text:style-name="%s">%s</text:span>' % \
40 tokenclass
.Literal
.Number
.Integer
,
41 tokenclass
.Literal
.Number
.Integer
.Long
,
42 tokenclass
.Literal
.Number
.Float
,
43 tokenclass
.Literal
.Number
.Hex
,
44 tokenclass
.Literal
.Number
.Oct
,
45 tokenclass
.Literal
.Number
,
47 s2
= self
.rststyle('codeblock-number')
48 s1
= '<text:span text:style-name="%s">%s</text:span>' % \
50 elif ttype
== tokenclass
.Operator
:
51 s2
= self
.rststyle('codeblock-operator')
52 s1
= '<text:span text:style-name="%s">%s</text:span>' % \
54 elif ttype
== tokenclass
.Comment
:
55 s2
= self
.rststyle('codeblock-comment')
56 s1
= '<text:span text:style-name="%s">%s</text:span>' % \
58 elif ttype
== tokenclass
.Name
.Class
:
59 s2
= self
.rststyle('codeblock-classname')
60 s1
= '<text:span text:style-name="%s">%s</text:span>' % \
62 elif ttype
== tokenclass
.Name
.Function
:
63 s2
= self
.rststyle('codeblock-functionname')
64 s1
= '<text:span text:style-name="%s">%s</text:span>' % \
66 elif ttype
== tokenclass
.Name
:
67 s2
= self
.rststyle('codeblock-name')
68 s1
= '<text:span text:style-name="%s">%s</text:span>' % \
75 class OdtPygmentsLaTeXFormatter(OdtPygmentsFormatter
):
76 def format(self
, tokensource
, outfile
):
77 tokenclass
= pygments
.token
.Token
78 for ttype
, value
in tokensource
:
79 value
= self
.escape_function(value
)
80 if ttype
== tokenclass
.Keyword
:
81 s2
= self
.rststyle('codeblock-keyword')
82 s1
= '<text:span text:style-name="%s">%s</text:span>' % \
84 elif ttype
in (tokenclass
.Literal
.String
,
85 tokenclass
.Literal
.String
.Backtick
,
87 s2
= self
.rststyle('codeblock-string')
88 s1
= '<text:span text:style-name="%s">%s</text:span>' % \
90 elif ttype
== tokenclass
.Name
.Attribute
:
91 s2
= self
.rststyle('codeblock-operator')
92 s1
= '<text:span text:style-name="%s">%s</text:span>' % \
94 elif ttype
== tokenclass
.Comment
:
96 s2
= self
.rststyle('codeblock-comment')
97 s1
= '<text:span text:style-name="%s">%s</text:span>\n' % \
100 s2
= self
.rststyle('codeblock-comment')
101 s1
= '<text:span text:style-name="%s">%s</text:span>' % \
103 elif ttype
== tokenclass
.Name
.Builtin
:
104 s2
= self
.rststyle('codeblock-name')
105 s1
= '<text:span text:style-name="%s">%s</text:span>' % \