Package python-module-logilab-mtconverter-0 :: Package 8 :: Package 4 :: Package transforms :: Module pygmentstransforms
[frames] | no frames]

Source Code for Module python-module-logilab-mtconverter-0.8.4.transforms.pygmentstransforms

 1  # This program is free software; you can redistribute it and/or modify it under 
 2  # the terms of the GNU General Public License as published by the Free Software 
 3  # Foundation; either version 2 of the License, or (at your option) any later 
 4  # version. 
 5  # 
 6  # This program is distributed in the hope that it will be useful, but WITHOUT 
 7  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 8  # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 
 9  # 
10  # You should have received a copy of the GNU General Public License along with 
11  # this program; if not, write to the Free Software Foundation, Inc., 
12  # 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
13  # copyright 2006-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 
14  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 
15  # 
16  # This file is part of logilab-mtconverter. 
17  # 
18  # logilab-mtconverter is free software: you can redistribute it and/or modify it 
19  # under the terms of the GNU Lesser General Public License as published by the 
20  # Free Software Foundation, either version 2.1 of the License, or (at your 
21  # option) any later version. 
22  # 
23  # logilab-mtconverter is distributed in the hope that it will be useful, but 
24  # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
25  # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License 
26  # for more details. 
27  # 
28  # You should have received a copy of the GNU Lesser General Public License along 
29  # with logilab-mtconverter. If not, see <http://www.gnu.org/licenses/>. 
30  """interface pygments (http://pygments.org/) with mtconverter""" 
31   
32  __docformat__ = "restructuredtext en" 
33   
34  from logilab.mtconverter.transform import Transform 
35   
36  from pygments import highlight 
37  from pygments.lexers import LEXERS, get_lexer_for_mimetype as mtlexer 
38  from pygments.formatters import HtmlFormatter, LatexFormatter, RtfFormatter, SvgFormatter 
39   
40  # hint: changes MIMETYPES before registering pygments transforms if you don't 
41  #       want all available inputs 
42  mimetypes = sorted(set([mt for _, _, _, _, mimetypes in LEXERS.itervalues() 
43                         for mt in mimetypes])) 
44   
45 -class PygmentsHTMLTransform(Transform):
46 """note: this transform use CSS classes which can be obtained using: 47 formatter.get_style_defs() 48 """ 49 inputs = mimetypes 50 output = 'text/html'
51 - def _convert(self, trdata):
52 lexer = mtlexer(trdata.mimetype, encoding=trdata.encoding) 53 return highlight(trdata.decode(force=True), lexer, HtmlFormatter())
54 55
56 -class PygmentsLatexTransform(Transform):
57 inputs = mimetypes 58 output = 'text/x-latex'
59 - def _convert(self, trdata):
60 lexer = mtlexer(trdata.mimetype, encoding=trdata.encoding) 61 return highlight(trdata.decode(force=True), lexer, LatexFormatter())
62 63
64 -class PygmentsRTFTransform(Transform):
65 inputs = mimetypes 66 output = 'application/rtf'
67 - def _convert(self, trdata):
68 lexer = mtlexer(trdata.mimetype, encoding=trdata.encoding) 69 return highlight(trdata.decode(force=True), lexer, RtfFormatter())
70
71 -class PygmentsSVGTransform(Transform):
72 inputs = mimetypes 73 output = 'image/svg+xml'
74 - def _convert(self, trdata):
75 lexer = mtlexer(trdata.mimetype, encoding=trdata.encoding) 76 return highlight(trdata.decode(force=True), lexer, SvgFormatter())
77 78 79 transform_classes = (PygmentsHTMLTransform, PygmentsLatexTransform, 80 PygmentsRTFTransform, PygmentsSVGTransform) 81