Website: add template and basic stylesheet with menu. Update buildhtml.py (and theref...
[docutils.git] / sandbox / gitpull / web_stylesheet_and_menu / docutils / writers / odf_odt / pygmentsformatter.py
blob8d3316b4304ccaa4b3868ecb551a016302ea4f22
1 # $Id$
2 # Author: Dave Kuhlman <dkuhlman@rexx.com>
3 # Copyright: This module has been placed in the public domain.
5 """
7 Additional support for Pygments formatter.
9 """
12 import pygments
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>' % \
34 (s2, value, )
35 elif ttype == tokenclass.Literal.String:
36 s2 = self.rststyle('codeblock-string')
37 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
38 (s2, value, )
39 elif ttype in (
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>' % \
49 (s2, value, )
50 elif ttype == tokenclass.Operator:
51 s2 = self.rststyle('codeblock-operator')
52 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
53 (s2, value, )
54 elif ttype == tokenclass.Comment:
55 s2 = self.rststyle('codeblock-comment')
56 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
57 (s2, value, )
58 elif ttype == tokenclass.Name.Class:
59 s2 = self.rststyle('codeblock-classname')
60 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
61 (s2, value, )
62 elif ttype == tokenclass.Name.Function:
63 s2 = self.rststyle('codeblock-functionname')
64 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
65 (s2, value, )
66 elif ttype == tokenclass.Name:
67 s2 = self.rststyle('codeblock-name')
68 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
69 (s2, value, )
70 else:
71 s1 = value
72 outfile.write(s1)
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>' % \
83 (s2, value, )
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>' % \
89 (s2, value, )
90 elif ttype == tokenclass.Name.Attribute:
91 s2 = self.rststyle('codeblock-operator')
92 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
93 (s2, value, )
94 elif ttype == tokenclass.Comment:
95 if value[-1] == '\n':
96 s2 = self.rststyle('codeblock-comment')
97 s1 = '<text:span text:style-name="%s">%s</text:span>\n' % \
98 (s2, value[:-1], )
99 else:
100 s2 = self.rststyle('codeblock-comment')
101 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
102 (s2, value, )
103 elif ttype == tokenclass.Name.Builtin:
104 s2 = self.rststyle('codeblock-name')
105 s1 = '<text:span text:style-name="%s">%s</text:span>' % \
106 (s2, value, )
107 else:
108 s1 = value
109 outfile.write(s1)