~mcfletch/eric/update-to-4.5.13

« back to all changes in this revision

Viewing changes to eric/ThirdParty/Pygments/pygments/formatters/latex.py

Tags: 4.5.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
    Formatter for LaTeX fancyvrb output.
7
7
 
8
 
    :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
 
8
    :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
9
9
    :license: BSD, see LICENSE for details.
10
10
"""
11
11
 
18
18
 
19
19
 
20
20
def escape_tex(text, commandprefix):
21
 
    return text.replace('@', '\x00').    \
22
 
                replace('[', '\x01').    \
23
 
                replace(']', '\x02').    \
24
 
                replace('\x00', '@%sZat[]' % commandprefix).\
25
 
                replace('\x01', '@%sZlb[]' % commandprefix).\
26
 
                replace('\x02', '@%sZrb[]' % commandprefix)
 
21
    return text.replace('\\', '\x00'). \
 
22
                replace('{', '\x01'). \
 
23
                replace('}', '\x02'). \
 
24
                replace('^', '\x03'). \
 
25
                replace('_', '\x04'). \
 
26
                replace('\x00', r'\%sZbs{}' % commandprefix). \
 
27
                replace('\x01', r'\%sZob{}' % commandprefix). \
 
28
                replace('\x02', r'\%sZcb{}' % commandprefix). \
 
29
                replace('\x03', r'\%sZca{}' % commandprefix). \
 
30
                replace('\x04', r'\%sZus{}' % commandprefix)
27
31
 
28
32
 
29
33
DOC_TEMPLATE = r'''
92
96
 
93
97
%(styles)s
94
98
 
 
99
\def\%(cp)sZbs{\char`\\}
 
100
\def\%(cp)sZus{\char`\_}
 
101
\def\%(cp)sZob{\char`\{}
 
102
\def\%(cp)sZcb{\char`\}}
 
103
\def\%(cp)sZca{\char`\^}
 
104
%% for compatibility with earlier versions
95
105
\def\%(cp)sZat{@}
96
106
\def\%(cp)sZlb{[}
97
107
\def\%(cp)sZrb{]}
177
187
        *New in Pygments 0.7.*
178
188
 
179
189
        *New in Pygments 0.10:* the default is now ``'PY'`` instead of ``'C'``.
 
190
 
 
191
    `texcomments`
 
192
        If set to ``True``, enables LaTeX comment lines.  That is, LaTex markup
 
193
        in comment tokens is not escaped so that LaTeX can render it (default:
 
194
        ``False``).  *New in Pygments 1.2.*
 
195
 
 
196
    `mathescape`
 
197
        If set to ``True``, enables LaTeX math mode escape in comments. That
 
198
        is, ``'$...$'`` inside a comment will trigger math mode (default:
 
199
        ``False``).  *New in Pygments 1.2.*
180
200
    """
181
201
    name = 'LaTeX'
182
202
    aliases = ['latex', 'tex']
192
212
        self.verboptions = options.get('verboptions', '')
193
213
        self.nobackground = get_bool_opt(options, 'nobackground', False)
194
214
        self.commandprefix = options.get('commandprefix', 'PY')
 
215
        self.texcomments = get_bool_opt(options, 'texcomments', False)
 
216
        self.mathescape = get_bool_opt(options, 'mathescape', False)
195
217
 
196
218
        self._create_stylesheet()
197
219
 
260
282
            realoutfile = outfile
261
283
            outfile = StringIO()
262
284
 
263
 
        outfile.write(r'\begin{Verbatim}[commandchars=@\[\]')
 
285
        outfile.write(r'\begin{Verbatim}[commandchars=\\\{\}')
264
286
        if self.linenos:
265
287
            start, step = self.linenostart, self.linenostep
266
288
            outfile.write(',numbers=left' +
267
289
                          (start and ',firstnumber=%d' % start or '') +
268
290
                          (step and ',stepnumber=%d' % step or ''))
 
291
        if self.mathescape or self.texcomments:
 
292
            outfile.write(r',codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8}')
269
293
        if self.verboptions:
270
294
            outfile.write(',' + self.verboptions)
271
295
        outfile.write(']\n')
272
296
 
273
297
        for ttype, value in tokensource:
274
 
            value = escape_tex(value, self.commandprefix)
 
298
            if ttype in Token.Comment:
 
299
                if self.texcomments:
 
300
                    # Try to guess comment starting lexeme and escape it ...
 
301
                    start = value[0:1]
 
302
                    for i in xrange(1, len(value)):
 
303
                        if start[0] != value[i]:
 
304
                            break
 
305
                        start += value[i]
 
306
 
 
307
                    value = value[len(start):]
 
308
                    start = escape_tex(start, self.commandprefix)
 
309
 
 
310
                    # ... but do not escape inside comment.
 
311
                    value = start + value
 
312
                elif self.mathescape:
 
313
                    # Only escape parts not inside a math environment.
 
314
                    parts = value.split('$')
 
315
                    in_math = False
 
316
                    for i, part in enumerate(parts):
 
317
                        if not in_math:
 
318
                            parts[i] = escape_tex(part, self.commandprefix)
 
319
                        in_math = not in_math
 
320
                    value = '$'.join(parts)
 
321
                else:
 
322
                    value = escape_tex(value, self.commandprefix)
 
323
            else:
 
324
                value = escape_tex(value, self.commandprefix)
275
325
            styles = []
276
326
            while ttype is not Token:
277
327
                try:
285
335
                spl = value.split('\n')
286
336
                for line in spl[:-1]:
287
337
                    if line:
288
 
                        outfile.write("@%s[%s][%s]" % (cp, styleval, line))
 
338
                        outfile.write("\\%s{%s}{%s}" % (cp, styleval, line))
289
339
                    outfile.write('\n')
290
340
                if spl[-1]:
291
 
                    outfile.write("@%s[%s][%s]" % (cp, styleval, spl[-1]))
 
341
                    outfile.write("\\%s{%s}{%s}" % (cp, styleval, spl[-1]))
292
342
            else:
293
343
                outfile.write(value)
294
344