Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/docutils/writers/odf_odt/pygmentsformatter.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 # $Id: pygmentsformatter.py 5853 2009-01-19 21:02:02Z dkuhlman $ | |
2 # Author: Dave Kuhlman <dkuhlman@rexx.com> | |
3 # Copyright: This module has been placed in the public domain. | |
4 | |
5 """ | |
6 | |
7 Additional support for Pygments formatter. | |
8 | |
9 """ | |
10 | |
11 | |
12 import pygments | |
13 import pygments.formatter | |
14 | |
15 | |
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 | |
21 | |
22 def rststyle(self, name, parameters=( )): | |
23 return self.rststyle_function(name, parameters) | |
24 | |
25 | |
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, | |
46 ): | |
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) | |
73 | |
74 | |
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, | |
86 ): | |
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) |