Skip to content

Commit

Permalink
Fix to the code formatter in Python 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
strichter committed Jul 24, 2014
1 parent cd0216c commit fd9f318
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/z3c/rml/flowable.py
Expand Up @@ -22,7 +22,7 @@
import reportlab.platypus.flowables
import reportlab.platypus.tables
import zope.schema
from reportlab.lib import styles, pygments2xpre, utils
from reportlab.lib import styles, utils
from xml.sax.saxutils import unescape
from z3c.rml import attr, directive, interfaces, occurence
from z3c.rml import form, platypus, special, SampleStyleSheet, stylesheet
Expand All @@ -36,6 +36,33 @@
reportlab.graphics.barcode = types.ModuleType('barcode')
reportlab.graphics.barcode.createBarcodeDrawing = None

# XXX:Copy of reportlab.lib.pygments2xpre.pygments2xpre to fix bug in Python 2.
def pygments2xpre(s, language="python"):
"Return markup suitable for XPreformatted"
try:
from pygments import highlight
from pygments.formatters import HtmlFormatter
except ImportError:
return s

from pygments.lexers import get_lexer_by_name

l = get_lexer_by_name(language)

h = HtmlFormatter()
# XXX: Does not work in Python 2, since pygments creates non-unicode
# outpur snippets.
#from io import StringIO
from cStringIO import StringIO
out = StringIO()
highlight(s,l,h,out)
styles = [(cls, style.split(';')[0].split(':')[1].strip())
for cls, (style, ttype, level) in h.class2style.items()
if cls and style and style.startswith('color:')]
from reportlab.lib.pygments2xpre import _2xpre
return _2xpre(out.getvalue(),styles)


class Flowable(directive.RMLDirective):
klass=None
attrMapping = None
Expand Down Expand Up @@ -254,8 +281,7 @@ def process(self):
lang = args.pop('language', None)
args['text'] = unescape(args['text'])
if lang is not None:
args['text'] = pygments2xpre.pygments2xpre(
args['text'], lang.lower())
args['text'] = pygments2xpre(args['text'], lang.lower())
if 'style' not in args:
args['style'] = attr._getStyle(self, 'Code')
self.parent.flow.append(self.klass(**args))
Expand Down

0 comments on commit fd9f318

Please sign in to comment.