comparison examples/trac/trac/mimeview/enscript.py @ 39:93b4dcbafd7b trunk

Copy Trac to main branch.
author cmlenz
date Mon, 03 Jul 2006 18:53:27 +0000
parents
children
comparison
equal deleted inserted replaced
38:ee669cb9cccc 39:93b4dcbafd7b
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2004-2005 Edgewall Software
4 # Copyright (C) 2004 Daniel Lundin <daniel@edgewall.com>
5 # Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
6 # All rights reserved.
7 #
8 # This software is licensed as described in the file COPYING, which
9 # you should have received as part of this distribution. The terms
10 # are also available at http://trac.edgewall.com/license.html.
11 #
12 # This software consists of voluntary contributions made by many
13 # individuals. For the exact contribution history, see the revision
14 # history and logs, available at http://projects.edgewall.com/trac/.
15 #
16 # Author: Daniel Lundin <daniel@edgewall.com>
17
18 from trac.config import Option, ListOption
19 from trac.core import *
20 from trac.mimeview.api import IHTMLPreviewRenderer, Mimeview
21 from trac.util import NaivePopen
22 from trac.util.markup import escape, Deuglifier
23
24 __all__ = ['EnscriptRenderer']
25
26 types = {
27 'application/xhtml+xml': ('html', 2),
28 'application/postscript': ('postscript', 2),
29 'application/x-csh': ('csh', 2),
30 'application/x-javascript': ('javascript', 2),
31 'application/x-troff': ('nroff', 2),
32 'text/html': ('html', 2),
33 'text/x-ada': ('ada', 2),
34 'text/x-asm': ('asm', 2),
35 'text/x-awk': ('awk', 2),
36 'text/x-c++src': ('cpp', 2),
37 'text/x-c++hdr': ('cpp', 2),
38 'text/x-chdr': ('c', 2),
39 'text/x-csh': ('csh', 2),
40 'text/x-csrc': ('c', 2),
41 'text/x-diff': ('diffu', 2), # Assume unified diff (works otherwise)
42 'text/x-eiffel': ('eiffel', 2),
43 'text/x-elisp': ('elisp', 2),
44 'text/x-fortran': ('fortran', 2),
45 'text/x-haskell': ('haskell', 2),
46 'text/x-idl': ('idl', 2),
47 'text/x-inf': ('inf', 2),
48 'text/x-java': ('java', 2),
49 'text/x-javascript': ('javascript', 2),
50 'text/x-ksh': ('ksh', 2),
51 'text/x-lua': ('lua', 2),
52 'text/x-m4': ('m4', 2),
53 'text/x-makefile': ('makefile', 2),
54 'text/x-mail': ('mail', 2),
55 'text/x-matlab': ('matlab', 2),
56 'text/x-objc': ('objc', 2),
57 'text/x-pascal': ('pascal', 2),
58 'text/x-perl': ('perl', 2),
59 'text/x-pyrex': ('pyrex', 2),
60 'text/x-python': ('python', 2),
61 'text/x-rfc': ('rfc', 2),
62 'text/x-ruby': ('ruby', 2),
63 'text/x-sh': ('sh', 2),
64 'text/x-scheme': ('scheme', 2),
65 'text/x-sql': ('sql', 2),
66 'text/x-tcl': ('tcl', 2),
67 'text/x-tex': ('tex', 2),
68 'text/x-vba': ('vba', 2),
69 'text/x-verilog': ('verilog', 2),
70 'text/x-vhdl': ('vhdl', 2),
71 'model/vrml': ('vrml', 2),
72 'application/x-sh': ('sh', 2),
73 'text/x-zsh': ('zsh', 2),
74 'text/vnd.wap.wmlscript': ('wmlscript', 2),
75 }
76
77
78 class EnscriptDeuglifier(Deuglifier):
79 def rules(cls):
80 return [
81 r'(?P<comment><FONT COLOR="#B22222">)',
82 r'(?P<keyword><FONT COLOR="#5F9EA0">)',
83 r'(?P<type><FONT COLOR="#228B22">)',
84 r'(?P<string><FONT COLOR="#BC8F8F">)',
85 r'(?P<func><FONT COLOR="#0000FF">)',
86 r'(?P<prep><FONT COLOR="#B8860B">)',
87 r'(?P<lang><FONT COLOR="#A020F0">)',
88 r'(?P<var><FONT COLOR="#DA70D6">)',
89 r'(?P<font><FONT.*?>)',
90 r'(?P<endfont></FONT>)'
91 ]
92 rules = classmethod(rules)
93
94
95 class EnscriptRenderer(Component):
96 """Syntax highlighting using GNU Enscript."""
97
98 implements(IHTMLPreviewRenderer)
99
100 expand_tabs = True
101
102 path = Option('mimeviewer', 'enscript_path', 'enscript',
103 """Path to the Enscript executable.""")
104
105 enscript_modes = ListOption('mimeviewer', 'enscript_modes',
106 'text/x-dylan:dylan:4',
107 """List of additional MIME types known by Enscript.
108 For each, a tuple `mimetype:mode:quality` has to be
109 specified, where `mimetype` is the MIME type,
110 `mode` is the corresponding Enscript mode to be used
111 for the conversion and `quality` is the quality ratio
112 associated to this conversion.
113 That can also be used to override the default
114 quality ratio used by the Enscript render, which is 2
115 (''since 0.10'').""")
116
117 def __init__(self):
118 self._types = None
119
120 # IHTMLPreviewRenderer methods
121
122 def get_quality_ratio(self, mimetype):
123 # Extend default MIME type to mode mappings with configured ones
124 if not self._types:
125 self._types = {}
126 self._types.update(types)
127 self._types.update(
128 Mimeview(self.env).configured_modes_mapping('enscript'))
129 return self._types.get(mimetype, (None, 0))[1]
130
131 def render(self, req, mimetype, content, filename=None, rev=None):
132 cmdline = self.path
133 mimetype = mimetype.split(';', 1)[0] # strip off charset
134 mode = self._types[mimetype][0]
135 cmdline += ' --color -h -q --language=html -p - -E%s' % mode
136 self.env.log.debug("Enscript command line: %s" % cmdline)
137
138 np = NaivePopen(cmdline, content.encode('utf-8'), capturestderr=1)
139 if np.errorlevel or np.err:
140 err = 'Running (%s) failed: %s, %s.' % (cmdline, np.errorlevel,
141 np.err)
142 raise Exception, err
143 odata = np.out
144
145 # Strip header and footer
146 i = odata.find('<PRE>')
147 beg = i > 0 and i + 6
148 i = odata.rfind('</PRE>')
149 end = i > 0 and i or len(odata)
150
151 odata = EnscriptDeuglifier().format(odata[beg:end].decode('utf-8'))
152 return odata.splitlines()
Copyright (C) 2012-2017 Edgewall Software