src/reportlab/lib/pygments2xpre.py
author andy
Thu, 17 Dec 2009 16:18:34 +0000
changeset 3290 6e5e33a15a3d
parent 3289 58fcf065b4e0
child 3558 ae12a0645e9f
permissions -rw-r--r--
allowed lexer to be varied
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3289
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
     1
"""Helps you output colourised code snippets in ReportLab documents.
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
     2
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
     3
Platypus has an 'XPreformatted' flowable for handling preformatted
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
     4
text, with variations in fonts and colors.   If Pygments is installed,
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
     5
calling 'pygments2xpre' will return content suitable for display in
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
     6
an XPreformatted object.  If it's not installed, you won't get colours.
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
     7
3290
6e5e33a15a3d allowed lexer to be varied
andy
parents: 3289
diff changeset
     8
For a list of available lexers see http://pygments.org/docs/
6e5e33a15a3d allowed lexer to be varied
andy
parents: 3289
diff changeset
     9
3289
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    10
"""
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    11
__all__ = ('pygments2xpre',)
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    12
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    13
def _2xpre(s,styles):
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    14
    "Helper to transform Pygments HTML output to ReportLab markup"
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    15
    s = s.replace('<div class="highlight">','')
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    16
    s = s.replace('</div>','')
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    17
    s = s.replace('<pre>','')
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    18
    s = s.replace('</pre>','')
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    19
    s = s.replace('</span>','</font>')
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    20
    for k,c in styles+[('p','#000000'),('n','#000000')]:
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    21
        s = s.replace('<span class="%s">' % k,'<font color="%s">' % c)
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    22
    return s
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    23
3290
6e5e33a15a3d allowed lexer to be varied
andy
parents: 3289
diff changeset
    24
def pygments2xpre(s, language="python"):
3289
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    25
    "Return markup suitable for XPreformatted"
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    26
    try:
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    27
        from pygments import highlight
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    28
        from pygments.formatters import HtmlFormatter
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    29
    except ImportError:
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    30
        return s
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    31
3290
6e5e33a15a3d allowed lexer to be varied
andy
parents: 3289
diff changeset
    32
    from pygments.lexers import get_lexer_by_name
6e5e33a15a3d allowed lexer to be varied
andy
parents: 3289
diff changeset
    33
6e5e33a15a3d allowed lexer to be varied
andy
parents: 3289
diff changeset
    34
    l = get_lexer_by_name(language)
6e5e33a15a3d allowed lexer to be varied
andy
parents: 3289
diff changeset
    35
    
3289
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    36
    h = HtmlFormatter()
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    37
    from StringIO import StringIO
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    38
    out = StringIO()
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    39
    highlight(s,l,h,out)
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    40
    styles = [(cls, style.split(';')[0].split(':')[1].strip())
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    41
                for cls, (style, ttype, level) in h.class2style.iteritems()
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    42
                if cls and style and style.startswith('color:')]
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    43
    return _2xpre(out.getvalue(),styles)
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    44
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    45
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    46
def convertSourceFiles(filenames):
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    47
    "Helper function - makes minimal PDF document"
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    48
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    49
    from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer, XPreformatted
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    50
    from reportlab.lib.styles import getSampleStyleSheet
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    51
    styT=getSampleStyleSheet()["Title"]
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    52
    styC=getSampleStyleSheet()["Code"]
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    53
    doc = SimpleDocTemplate("pygments2xpre.pdf")
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    54
    S = [].append
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    55
    for filename in filenames:
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    56
        S(Paragraph(filename,style=styT))
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    57
        src = open(filename, 'r').read()
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    58
        fmt = pygments2xpre(src)
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    59
        S(XPreformatted(fmt, style=styC))
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    60
    doc.build(S.__self__)
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    61
    print 'saved pygments2xpre.pdf'
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    62
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    63
if __name__=='__main__':
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    64
    import sys
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    65
    filenames = sys.argv[1:]
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    66
    if not filenames:
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    67
        print 'usage:  pygments2xpre.py file1.py [file2.py] [...]'
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    68
        sys.exit(0)
58fcf065b4e0 added pygments2xpre.py
andy
parents:
diff changeset
    69
    convertSourceFiles(filenames)