summaryrefslogtreecommitdiff
path: root/pygments/formatters/html.py
blob: 97e49161465080401efeacb38e74147d41a8d789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-
"""
    pygments.formatters.html
    ~~~~~~~~~~~~~~~~~~~~~~~~

    Formatter for HTML output.

    :copyright: 2006 by Georg Brandl, Armin Ronacher.
    :license: GNU LGPL, see LICENSE for more details.
"""
import StringIO

from pygments.formatter import Formatter
from pygments.token import Token, Text, STANDARD_TYPES
from pygments.util import get_bool_opt, get_int_opt, get_list_opt


__all__ = ['HtmlFormatter']


def escape_html(text):
    """Escape &, <, > as well as single and double quotes for HTML."""
    return text.replace('&', '&amp;').  \
                replace('<', '&lt;').   \
                replace('>', '&gt;').   \
                replace('"', '&quot;'). \
                replace("'", '&#39;')


def get_random_id():
    """Return a random id for javascript fields."""
    from random import random
    from time import time
    try:
        from hashlib import sha1 as sha
    except ImportError:
        import sha
        sha = sha.new
    return sha('%s|%s' % (random(), time())).hexdigest()


def _get_ttype_class(ttype):
    fname = STANDARD_TYPES.get(ttype)
    if fname: return fname
    aname = ''
    while fname is None:
        aname = '-' + ttype[-1] + aname
        ttype = ttype.parent
        fname = STANDARD_TYPES.get(ttype)
    return fname + aname


DOC_TEMPLATE = '''\
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
  <title>%(title)s</title>
  <style type="text/css">
td.linenos { background-color: #f0f0f0; padding-right: 10px; }
%(styledefs)s
  </style>
</head>
<body>
<h2>%(title)s</h2>

%(code)s

</body>
</html>
'''


class HtmlFormatter(Formatter):
    """
    Output HTML <span> tags with appropriate classes.

    Additional options accepted:

    ``nowrap``
        If set to true, don't wrap the tokens at all. This disables
        all other options (default: False).
    ``noclasses``
        If set to true, token <span>s will not use CSS classes, but
        inline styles.
    ``classprefix``
        Prefix for token CSS classes, is prepended to all token style
        classes (e.g. class="o" -> class="_o" if classprefix == '_')
        (default: '').
    ``cssclass``
        CSS class for the wrapping <div> (default: 'highlight').
    ``cssstyles``
        Inline CSS styles for the wrapping <div>. (default: '').
    ``linenos``
        If set to ``True``, output line numbers (default: False).
    ``linenostart``
        The line number for the first line (default: 1).
    ``linenostep``
        If set to a number n > 1, only every nth line number is printed
        (default: 1).
    ``linenospecial``
        If set to a number n > 0, every nth line number is given a special
        CSS class ``special`` (default: 0).
    ``nobackground``
        If set to ``True`` the formatter won't output the background color
        for the overall element (this automatically defaults to ``False``
        when there is no overall element [eg: no argument for the
        `get_syntax_defs` method given]) (default: ``False``)
    """

    def __init__(self, **options):
        Formatter.__init__(self, **options)
        self.nowrap = get_bool_opt(options, 'nowrap', False)
        self.noclasses = get_bool_opt(options, 'noclasses', False)
        self.classprefix = options.get('classprefix', '')
        self.cssclass = options.get('cssclass', 'highlight')
        self.cssstyles = options.get('cssstyles', '')
        self.linenos = get_bool_opt(options, 'linenos', False)
        self.linenostart = abs(get_int_opt(options, 'linenostart', 1))
        self.linenostep = abs(get_int_opt(options, 'linenostep', 1))
        self.linenospecial = abs(get_int_opt(options, 'linenospecial', 0))
        self.nobackground = get_bool_opt(options, 'nobackground', False)

        self._class_cache = {}
        self._create_stylesheet()

    def _get_css_class(self, ttype):
        """Return the css class of this token type prefixed with
        the classprefix option."""
        if ttype in self._class_cache:
            return self._class_cache[ttype]

        return self.classprefix + STANDARD_TYPES.get(ttype) or _get_ttype_class(ttype)

    def _create_stylesheet(self):
        t2c = self.ttype2class = {Token: ''}
        c2s = self.class2style = {}
        cp = self.classprefix
        for ttype, ndef in self.style:
            name = cp + _get_ttype_class(ttype)
            style = ''
            if ndef['color']:
                style += 'color: #%s; ' % ndef['color']
            if ndef['bold']:
                style += 'font-weight: bold; '
            if ndef['italic']:
                style += 'font-style: italic; '
            if ndef['underline']:
                style += 'text-decoration: underline; '
            if ndef['bgcolor']:
                style += 'background-color: #%s; ' % ndef['bgcolor']
            if ndef['border']:
                style += 'border: 1px solid #%s; ' % ndef['border']
            if style:
                t2c[ttype] = name
                # save len(ttype) to enable ordering the styles by
                # hierarchy (necessary for CSS cascading rules!)
                c2s[name] = (style[:-2], ttype, len(ttype))

    def get_style_defs(self, arg=''):
        """
        Return CSS style definitions for the classes produced by the
        current highlighting style. ``arg`` can be a string of selectors
        to insert before the token type classes.
        """
        if arg:
            arg += ' '
        styles = [(level, ttype, cls, style)
                  for cls, (style, ttype, level) in self.class2style.iteritems()
                  if cls and style]
        styles.sort()
        lines = ['%s.%s { %s } /* %s */' % (arg, cls, style, repr(ttype)[6:])
                 for level, ttype, cls, style in styles]
        if arg and not self.nobackground and \
           self.style.background_color is not None:
            text_style = ''
            if Text in self.ttype2class:
                text_style = ' ' + self.class2style[self.ttype2class[Text]][0]
            lines.insert(0, '%s{ background: %s;%s }' %
                         (arg, self.style.background_color, text_style))
        return '\n'.join(lines)

    def _format_nowrap(self, tokensource, outfile, lnos=False):
        lncount = 0
        nocls = self.noclasses
        # for <span style=""> lookup only
        getcls = self.ttype2class.get
        c2s = self.class2style

        write = outfile.write
        lspan = ''
        for ttype, value in tokensource:
            htmlvalue = escape_html(value)
            if lnos:
                lncount += value.count("\n")

            if nocls:
                cclass = getcls(ttype)
                while cclass is None:
                    ttype = ttype.parent
                    cclass = getcls(ttype)
                cspan = cclass and '<span style="%s">' % c2s[cclass][0]
            else:
                cls = self._get_css_class(ttype)
                cspan = cls and '<span class="%s">' % cls

            if cspan == lspan:
                if not cspan:
                    write(htmlvalue)
                else:
                    write(htmlvalue.replace('\n', '</span>\n' + cspan))
            elif htmlvalue: # if no value, leave old span open
                if lspan:
                    write('</span>')
                lspan = cspan
                if cspan:
                    htmlvalue = htmlvalue.replace('\n', '</span>\n' + cspan)
                    write(cspan + htmlvalue)
                else:
                    write(htmlvalue)
        if lspan:
            write('</span>')
        return lncount

    def format(self, tokensource, outfile):
        if self.nowrap:
            self._format_nowrap(tokensource, outfile)
            return

        realoutfile = outfile
        lnos = self.linenos
        full = self.full

        div = ('<div' + (self.cssclass and ' class="%s" ' % self.cssclass)
               + (self.cssstyles and ' style="%s"' % self.cssstyles) + '>')
        if full or lnos:
            outfile = StringIO.StringIO()
        else:
            outfile.write(div)

        outfile.write('<pre>')
        lncount = self._format_nowrap(tokensource, outfile, lnos)
        outfile.write('</pre>')

        ret = ''
        if lnos:
            fl = self.linenostart
            mw = len(str(lncount + fl - 1))
            sp = self.linenospecial
            st = self.linenostep
            if sp:
                ls = '\n'.join([(i%st == 0 and
                                 (i%sp == 0 and '<span class="special">%*d</span>'
                                  or '%*d') % (mw, i)
                                 or '')
                                for i in range(fl, fl + lncount)])
            else:
                ls = '\n'.join([(i%st == 0 and ('%*d' % (mw, i)) or '')
                                for i in range(fl, fl + lncount)])

            ret = div + ('<table><tr>'
                   '<td class="linenos" title="click to toggle" '
                   'onclick="with (this.firstChild.style) { display = '
                   '''(display == '') ? 'none' : '' }"><pre>'''
                   + ls + '</pre></td><td class="code">')
            ret += outfile.getvalue()
            ret += '</td></tr></table>'

        if full:
            if not ret:
                ret = div + outfile.getvalue() + '</div>\n'
            realoutfile.write(DOC_TEMPLATE %
                dict(title     = self.title,
                     styledefs = self.get_style_defs('body'),
                     code      = ret))
        elif lnos:
            realoutfile.write(ret + '</div>\n')
        else:
            realoutfile.write('</div>\n')