pygments.rb 0.3.7 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +9 -0
- data/CHANGELOG.md +6 -0
- data/LICENSE +1 -1
- data/README.md +1 -1
- data/lexers +0 -0
- data/lib/pygments/version.rb +1 -1
- data/test/test_pygments.rb +0 -1
- data/vendor/pygments-main/AUTHORS +14 -1
- data/vendor/pygments-main/CHANGES +56 -6
- data/vendor/pygments-main/LICENSE +1 -1
- data/vendor/pygments-main/Makefile +1 -1
- data/vendor/pygments-main/REVISION +1 -0
- data/vendor/pygments-main/docs/generate.py +1 -1
- data/vendor/pygments-main/docs/src/api.txt +1 -1
- data/vendor/pygments-main/docs/src/index.txt +1 -1
- data/vendor/pygments-main/docs/src/integrate.txt +5 -0
- data/vendor/pygments-main/docs/src/java.txt +70 -0
- data/vendor/pygments-main/external/autopygmentize +64 -0
- data/vendor/pygments-main/external/markdown-processor.py +2 -2
- data/vendor/pygments-main/external/moin-parser.py +1 -1
- data/vendor/pygments-main/external/rst-directive-old.py +1 -1
- data/vendor/pygments-main/external/rst-directive.py +1 -1
- data/vendor/pygments-main/pygments/__init__.py +2 -2
- data/vendor/pygments-main/pygments/cmdline.py +14 -6
- data/vendor/pygments-main/pygments/console.py +1 -1
- data/vendor/pygments-main/pygments/filter.py +1 -1
- data/vendor/pygments-main/pygments/filters/__init__.py +1 -1
- data/vendor/pygments-main/pygments/formatter.py +1 -1
- data/vendor/pygments-main/pygments/formatters/__init__.py +1 -1
- data/vendor/pygments-main/pygments/formatters/_mapping.py +1 -1
- data/vendor/pygments-main/pygments/formatters/bbcode.py +1 -1
- data/vendor/pygments-main/pygments/formatters/html.py +73 -2
- data/vendor/pygments-main/pygments/formatters/img.py +1 -1
- data/vendor/pygments-main/pygments/formatters/latex.py +1 -1
- data/vendor/pygments-main/pygments/formatters/other.py +1 -1
- data/vendor/pygments-main/pygments/formatters/rtf.py +1 -1
- data/vendor/pygments-main/pygments/formatters/svg.py +1 -1
- data/vendor/pygments-main/pygments/formatters/terminal.py +1 -1
- data/vendor/pygments-main/pygments/formatters/terminal256.py +1 -1
- data/vendor/pygments-main/pygments/lexer.py +76 -12
- data/vendor/pygments-main/pygments/lexers/__init__.py +2 -2
- data/vendor/pygments-main/pygments/lexers/_asybuiltins.py +1 -1
- data/vendor/pygments-main/pygments/lexers/_clbuiltins.py +1 -1
- data/vendor/pygments-main/pygments/lexers/_lassobuiltins.py +4 -1
- data/vendor/pygments-main/pygments/lexers/_luabuiltins.py +1 -1
- data/vendor/pygments-main/pygments/lexers/_mapping.py +26 -10
- data/vendor/pygments-main/pygments/lexers/_openedgebuiltins.py +11 -0
- data/vendor/pygments-main/pygments/lexers/_phpbuiltins.py +2 -2
- data/vendor/pygments-main/pygments/lexers/_postgres_builtins.py +4 -2
- data/vendor/pygments-main/pygments/lexers/_robotframeworklexer.py +14 -3
- data/vendor/pygments-main/pygments/lexers/_scilab_builtins.py +11 -0
- data/vendor/pygments-main/pygments/lexers/_sourcemodbuiltins.py +2 -2
- data/vendor/pygments-main/pygments/lexers/_stan_builtins.py +6 -6
- data/vendor/pygments-main/pygments/lexers/agile.py +6 -4
- data/vendor/pygments-main/pygments/lexers/asm.py +40 -2
- data/vendor/pygments-main/pygments/lexers/compiled.py +751 -438
- data/vendor/pygments-main/pygments/lexers/dalvik.py +2 -2
- data/vendor/pygments-main/pygments/lexers/dotnet.py +1 -1
- data/vendor/pygments-main/pygments/lexers/foxpro.py +428 -0
- data/vendor/pygments-main/pygments/lexers/functional.py +6 -5
- data/vendor/pygments-main/pygments/lexers/hdl.py +3 -3
- data/vendor/pygments-main/pygments/lexers/jvm.py +130 -16
- data/vendor/pygments-main/pygments/lexers/math.py +250 -9
- data/vendor/pygments-main/pygments/lexers/other.py +518 -13
- data/vendor/pygments-main/pygments/lexers/parsers.py +3 -2
- data/vendor/pygments-main/pygments/lexers/shell.py +48 -2
- data/vendor/pygments-main/pygments/lexers/special.py +1 -1
- data/vendor/pygments-main/pygments/lexers/sql.py +1 -1
- data/vendor/pygments-main/pygments/lexers/templates.py +7 -6
- data/vendor/pygments-main/pygments/lexers/text.py +9 -9
- data/vendor/pygments-main/pygments/lexers/web.py +264 -51
- data/vendor/pygments-main/pygments/plugin.py +1 -1
- data/vendor/pygments-main/pygments/scanner.py +1 -1
- data/vendor/pygments-main/pygments/style.py +1 -1
- data/vendor/pygments-main/pygments/styles/__init__.py +1 -1
- data/vendor/pygments-main/pygments/styles/autumn.py +1 -1
- data/vendor/pygments-main/pygments/styles/borland.py +1 -1
- data/vendor/pygments-main/pygments/styles/bw.py +1 -1
- data/vendor/pygments-main/pygments/styles/colorful.py +1 -1
- data/vendor/pygments-main/pygments/styles/default.py +1 -1
- data/vendor/pygments-main/pygments/styles/emacs.py +1 -1
- data/vendor/pygments-main/pygments/styles/friendly.py +1 -1
- data/vendor/pygments-main/pygments/styles/fruity.py +1 -1
- data/vendor/pygments-main/pygments/styles/manni.py +1 -1
- data/vendor/pygments-main/pygments/styles/monokai.py +1 -1
- data/vendor/pygments-main/pygments/styles/murphy.py +1 -1
- data/vendor/pygments-main/pygments/styles/native.py +1 -1
- data/vendor/pygments-main/pygments/styles/pastie.py +1 -1
- data/vendor/pygments-main/pygments/styles/perldoc.py +1 -1
- data/vendor/pygments-main/pygments/styles/rrt.py +1 -1
- data/vendor/pygments-main/pygments/styles/tango.py +1 -1
- data/vendor/pygments-main/pygments/styles/trac.py +1 -1
- data/vendor/pygments-main/pygments/styles/vim.py +1 -1
- data/vendor/pygments-main/pygments/styles/vs.py +1 -1
- data/vendor/pygments-main/pygments/token.py +1 -1
- data/vendor/pygments-main/pygments/unistring.py +1 -1
- data/vendor/pygments-main/pygments/util.py +1 -1
- data/vendor/pygments-main/scripts/check_sources.py +2 -2
- data/vendor/pygments-main/scripts/detect_missing_analyse_text.py +2 -0
- data/vendor/pygments-main/scripts/find_codetags.py +1 -1
- data/vendor/pygments-main/scripts/find_error.py +5 -6
- data/vendor/pygments-main/setup.cfg +1 -0
- data/vendor/pygments-main/setup.py +6 -4
- data/vendor/pygments-main/tests/examplefiles/BOM.js +1 -0
- data/vendor/pygments-main/tests/examplefiles/autoit_submit.au3 +25 -0
- data/vendor/pygments-main/tests/examplefiles/bigtest.nsi +308 -0
- data/vendor/pygments-main/tests/examplefiles/ca65_example +284 -0
- data/vendor/pygments-main/tests/examplefiles/cbmbas_example +9 -0
- data/vendor/pygments-main/tests/examplefiles/classes.dylan +89 -4
- data/vendor/pygments-main/tests/examplefiles/example.cob +3556 -0
- data/vendor/pygments-main/tests/examplefiles/example.prg +161 -0
- data/vendor/pygments-main/tests/examplefiles/example.shell-session +45 -0
- data/vendor/pygments-main/tests/examplefiles/example.ts +28 -0
- data/vendor/pygments-main/tests/examplefiles/inet_pton6.dg +2 -2
- data/vendor/pygments-main/tests/examplefiles/logos_example.xm +28 -0
- data/vendor/pygments-main/tests/examplefiles/mg_sample.pro +73 -0
- data/vendor/pygments-main/tests/examplefiles/minehunt.qml +112 -0
- data/vendor/pygments-main/tests/examplefiles/nanomsg.intr +95 -0
- data/vendor/pygments-main/tests/examplefiles/phpMyAdmin.spec +163 -0
- data/vendor/pygments-main/tests/examplefiles/rust_example.rs +191 -701
- data/vendor/pygments-main/tests/examplefiles/session.dylan-console +9 -0
- data/vendor/pygments-main/tests/examplefiles/test.opa +10 -0
- data/vendor/pygments-main/tests/examplefiles/test2.pypylog +120 -0
- data/vendor/pygments-main/tests/examplefiles/unix-io.lid +37 -0
- data/vendor/pygments-main/tests/old_run.py +1 -1
- data/vendor/pygments-main/tests/run.py +3 -2
- data/vendor/pygments-main/tests/support/tags +36 -0
- data/vendor/pygments-main/tests/test_basic_api.py +3 -2
- data/vendor/pygments-main/tests/test_clexer.py +1 -1
- data/vendor/pygments-main/tests/test_cmdline.py +1 -1
- data/vendor/pygments-main/tests/test_examplefiles.py +1 -1
- data/vendor/pygments-main/tests/test_html_formatter.py +17 -1
- data/vendor/pygments-main/tests/test_latex_formatter.py +1 -1
- data/vendor/pygments-main/tests/test_perllexer.py +1 -1
- data/vendor/pygments-main/tests/test_regexlexer.py +1 -1
- data/vendor/pygments-main/tests/test_token.py +1 -1
- data/vendor/pygments-main/tests/test_using_api.py +1 -1
- data/vendor/pygments-main/tests/test_util.py +5 -4
- metadata +26 -2
@@ -27,7 +27,7 @@
|
|
27
27
|
|
28
28
|
.. _Markdown: http://www.freewisdom.org/projects/python-markdown/
|
29
29
|
|
30
|
-
:copyright: Copyright 2006-
|
30
|
+
:copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
|
31
31
|
:license: BSD, see LICENSE for details.
|
32
32
|
"""
|
33
33
|
|
@@ -64,4 +64,4 @@ class CodeBlockPreprocessor(TextPreprocessor):
|
|
64
64
|
code = code.replace('\n\n', '\n \n').replace('\n', '<br />')
|
65
65
|
return '\n\n<div class="code">%s</div>\n\n' % code
|
66
66
|
return self.pattern.sub(
|
67
|
-
repl, lines)
|
67
|
+
repl, lines)
|
@@ -31,7 +31,7 @@
|
|
31
31
|
If you do not want to do that and are willing to accept larger HTML
|
32
32
|
output, you can set the INLINESTYLES option below to True.
|
33
33
|
|
34
|
-
:copyright: Copyright 2006-
|
34
|
+
:copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
|
35
35
|
:license: BSD, see LICENSE for details.
|
36
36
|
"""
|
37
37
|
|
@@ -31,7 +31,7 @@
|
|
31
31
|
.. _directive documentation:
|
32
32
|
http://docutils.sourceforge.net/docs/howto/rst-directives.html
|
33
33
|
|
34
|
-
:copyright: Copyright 2006-
|
34
|
+
:copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
|
35
35
|
:license: BSD, see LICENSE for details.
|
36
36
|
"""
|
37
37
|
|
@@ -31,7 +31,7 @@
|
|
31
31
|
.. _directive documentation:
|
32
32
|
http://docutils.sourceforge.net/docs/howto/rst-directives.html
|
33
33
|
|
34
|
-
:copyright: Copyright 2006-
|
34
|
+
:copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
|
35
35
|
:license: BSD, see LICENSE for details.
|
36
36
|
"""
|
37
37
|
|
@@ -22,11 +22,11 @@
|
|
22
22
|
.. _Pygments tip:
|
23
23
|
http://bitbucket.org/birkenfeld/pygments-main/get/tip.zip#egg=Pygments-dev
|
24
24
|
|
25
|
-
:copyright: Copyright 2006-
|
25
|
+
:copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
|
26
26
|
:license: BSD, see LICENSE for details.
|
27
27
|
"""
|
28
28
|
|
29
|
-
__version__ = '1.
|
29
|
+
__version__ = '1.6'
|
30
30
|
__docformat__ = 'restructuredtext'
|
31
31
|
|
32
32
|
__all__ = ['lex', 'format', 'highlight']
|
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Command line interface.
|
7
7
|
|
8
|
-
:copyright: Copyright 2006-
|
8
|
+
:copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
|
9
9
|
:license: BSD, see LICENSE for details.
|
10
10
|
"""
|
11
11
|
import sys
|
@@ -192,6 +192,14 @@ def main(args=sys.argv):
|
|
192
192
|
|
193
193
|
usage = USAGE % ((args[0],) * 6)
|
194
194
|
|
195
|
+
if sys.platform in ['win32', 'cygwin']:
|
196
|
+
try:
|
197
|
+
# Provide coloring under Windows, if possible
|
198
|
+
import colorama
|
199
|
+
colorama.init()
|
200
|
+
except ImportError:
|
201
|
+
pass
|
202
|
+
|
195
203
|
try:
|
196
204
|
popts, args = getopt.getopt(args[1:], "l:f:F:o:O:P:LS:a:N:hVHg")
|
197
205
|
except getopt.GetoptError, err:
|
@@ -219,7 +227,7 @@ def main(args=sys.argv):
|
|
219
227
|
return 0
|
220
228
|
|
221
229
|
if opts.pop('-V', None) is not None:
|
222
|
-
print 'Pygments version %s, (c) 2006-
|
230
|
+
print 'Pygments version %s, (c) 2006-2013 by Georg Brandl.' % __version__
|
223
231
|
return 0
|
224
232
|
|
225
233
|
# handle ``pygmentize -L``
|
@@ -370,9 +378,9 @@ def main(args=sys.argv):
|
|
370
378
|
except ClassNotFound, err:
|
371
379
|
if '-g' in opts:
|
372
380
|
try:
|
373
|
-
lexer = guess_lexer(code)
|
381
|
+
lexer = guess_lexer(code, **parsed_opts)
|
374
382
|
except ClassNotFound:
|
375
|
-
lexer = TextLexer()
|
383
|
+
lexer = TextLexer(**parsed_opts)
|
376
384
|
else:
|
377
385
|
print >>sys.stderr, 'Error:', err
|
378
386
|
return 1
|
@@ -384,9 +392,9 @@ def main(args=sys.argv):
|
|
384
392
|
if '-g' in opts:
|
385
393
|
code = sys.stdin.read()
|
386
394
|
try:
|
387
|
-
lexer = guess_lexer(code)
|
395
|
+
lexer = guess_lexer(code, **parsed_opts)
|
388
396
|
except ClassNotFound:
|
389
|
-
lexer = TextLexer()
|
397
|
+
lexer = TextLexer(**parsed_opts)
|
390
398
|
elif not lexer:
|
391
399
|
print >>sys.stderr, 'Error: no lexer name given and reading ' + \
|
392
400
|
'from stdin (try using -g or -l <lexer>)'
|
@@ -5,18 +5,23 @@
|
|
5
5
|
|
6
6
|
Formatter for HTML output.
|
7
7
|
|
8
|
-
:copyright: Copyright 2006-
|
8
|
+
:copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
|
9
9
|
:license: BSD, see LICENSE for details.
|
10
10
|
"""
|
11
11
|
|
12
12
|
import os
|
13
13
|
import sys
|
14
|
+
import os.path
|
14
15
|
import StringIO
|
15
16
|
|
16
17
|
from pygments.formatter import Formatter
|
17
18
|
from pygments.token import Token, Text, STANDARD_TYPES
|
18
19
|
from pygments.util import get_bool_opt, get_int_opt, get_list_opt, bytes
|
19
20
|
|
21
|
+
try:
|
22
|
+
import ctags
|
23
|
+
except ImportError:
|
24
|
+
ctags = None
|
20
25
|
|
21
26
|
__all__ = ['HtmlFormatter']
|
22
27
|
|
@@ -141,6 +146,12 @@ class HtmlFormatter(Formatter):
|
|
141
146
|
the style definitions inside a ``<style>`` tag, or in a separate file if
|
142
147
|
the `cssfile` option is given.
|
143
148
|
|
149
|
+
When `tagsfile` is set to the path of a ctags index file, it is used to
|
150
|
+
generate hyperlinks from names to their definition. You must enable
|
151
|
+
`anchorlines` and run ctags with the `-n` option for this to work. The
|
152
|
+
`python-ctags` module from PyPI must be installed to use this feature;
|
153
|
+
otherwise a `RuntimeError` will be raised.
|
154
|
+
|
144
155
|
The `get_style_defs(arg='')` method of a `HtmlFormatter` returns a string
|
145
156
|
containing CSS rules for the CSS classes used by the formatter. The
|
146
157
|
argument `arg` can be used to specify additional CSS selectors that
|
@@ -282,10 +293,27 @@ class HtmlFormatter(Formatter):
|
|
282
293
|
output line in an anchor tag with a ``name`` of ``foo-linenumber``.
|
283
294
|
This allows easy linking to certain lines. *New in Pygments 0.9.*
|
284
295
|
|
296
|
+
`linespans`
|
297
|
+
If set to a nonempty string, e.g. ``foo``, the formatter will wrap each
|
298
|
+
output line in a span tag with an ``id`` of ``foo-linenumber``.
|
299
|
+
This allows easy access to lines via javascript. *New in Pygments 1.6.*
|
300
|
+
|
285
301
|
`anchorlinenos`
|
286
302
|
If set to `True`, will wrap line numbers in <a> tags. Used in
|
287
303
|
combination with `linenos` and `lineanchors`.
|
288
304
|
|
305
|
+
`tagsfile`
|
306
|
+
If set to the path of a ctags file, wrap names in anchor tags that
|
307
|
+
link to their definitions. `lineanchors` should be used, and the
|
308
|
+
tags file should specify line numbers (see the `-n` option to ctags).
|
309
|
+
*New in Pygments 1.6.*
|
310
|
+
|
311
|
+
`tagurlformat`
|
312
|
+
A string formatting pattern used to generate links to ctags definitions.
|
313
|
+
Available variables are `%(path)s`, `%(fname)s` and `%(fext)s`.
|
314
|
+
Defaults to an empty string, resulting in just `#prefix-number` links.
|
315
|
+
*New in Pygments 1.6.*
|
316
|
+
|
289
317
|
|
290
318
|
**Subclassing the HTML formatter**
|
291
319
|
|
@@ -351,6 +379,14 @@ class HtmlFormatter(Formatter):
|
|
351
379
|
self.prestyles = self._decodeifneeded(options.get('prestyles', ''))
|
352
380
|
self.cssfile = self._decodeifneeded(options.get('cssfile', ''))
|
353
381
|
self.noclobber_cssfile = get_bool_opt(options, 'noclobber_cssfile', False)
|
382
|
+
self.tagsfile = self._decodeifneeded(options.get('tagsfile', ''))
|
383
|
+
self.tagurlformat = self._decodeifneeded(options.get('tagurlformat', ''))
|
384
|
+
|
385
|
+
if self.tagsfile:
|
386
|
+
if not ctags:
|
387
|
+
raise RuntimeError('The "ctags" package must to be installed '
|
388
|
+
'to be able to use the "tagsfile" feature.')
|
389
|
+
self._ctags = ctags.CTags(self.tagsfile)
|
354
390
|
|
355
391
|
linenos = options.get('linenos', False)
|
356
392
|
if linenos == 'inline':
|
@@ -366,6 +402,7 @@ class HtmlFormatter(Formatter):
|
|
366
402
|
self.nobackground = get_bool_opt(options, 'nobackground', False)
|
367
403
|
self.lineseparator = options.get('lineseparator', '\n')
|
368
404
|
self.lineanchors = options.get('lineanchors', '')
|
405
|
+
self.linespans = options.get('linespans', '')
|
369
406
|
self.anchorlinenos = options.get('anchorlinenos', False)
|
370
407
|
self.hl_lines = set()
|
371
408
|
for lineno in get_list_opt(options, 'hl_lines', []):
|
@@ -596,7 +633,8 @@ class HtmlFormatter(Formatter):
|
|
596
633
|
|
597
634
|
def _wrap_lineanchors(self, inner):
|
598
635
|
s = self.lineanchors
|
599
|
-
i = self.linenostart - 1 # subtract 1 since we have to increment i
|
636
|
+
i = self.linenostart - 1 # subtract 1 since we have to increment i
|
637
|
+
# *before* yielding
|
600
638
|
for t, line in inner:
|
601
639
|
if t:
|
602
640
|
i += 1
|
@@ -604,6 +642,16 @@ class HtmlFormatter(Formatter):
|
|
604
642
|
else:
|
605
643
|
yield 0, line
|
606
644
|
|
645
|
+
def _wrap_linespans(self, inner):
|
646
|
+
s = self.linespans
|
647
|
+
i = self.linenostart - 1
|
648
|
+
for t, line in inner:
|
649
|
+
if t:
|
650
|
+
i += 1
|
651
|
+
yield 1, '<span id="%s-%d">%s</span>' % (s, i, line)
|
652
|
+
else:
|
653
|
+
yield 0, line
|
654
|
+
|
607
655
|
def _wrap_div(self, inner):
|
608
656
|
style = []
|
609
657
|
if (self.noclasses and not self.nobackground and
|
@@ -643,6 +691,7 @@ class HtmlFormatter(Formatter):
|
|
643
691
|
getcls = self.ttype2class.get
|
644
692
|
c2s = self.class2style
|
645
693
|
escape_table = _escape_html_table
|
694
|
+
tagsfile = self.tagsfile
|
646
695
|
|
647
696
|
lspan = ''
|
648
697
|
line = ''
|
@@ -659,6 +708,19 @@ class HtmlFormatter(Formatter):
|
|
659
708
|
|
660
709
|
parts = value.translate(escape_table).split('\n')
|
661
710
|
|
711
|
+
if tagsfile and ttype in Token.Name:
|
712
|
+
filename, linenumber = self._lookup_ctag(value)
|
713
|
+
if linenumber:
|
714
|
+
base, filename = os.path.split(filename)
|
715
|
+
if base:
|
716
|
+
base += '/'
|
717
|
+
filename, extension = os.path.splitext(filename)
|
718
|
+
url = self.tagurlformat % {'path': base, 'fname': filename,
|
719
|
+
'fext': extension}
|
720
|
+
parts[0] = "<a href=\"%s#%s-%d\">%s" % \
|
721
|
+
(url, self.lineanchors, linenumber, parts[0])
|
722
|
+
parts[-1] = parts[-1] + "</a>"
|
723
|
+
|
662
724
|
# for all but the last line
|
663
725
|
for part in parts[:-1]:
|
664
726
|
if line:
|
@@ -688,6 +750,13 @@ class HtmlFormatter(Formatter):
|
|
688
750
|
if line:
|
689
751
|
yield 1, line + (lspan and '</span>') + lsep
|
690
752
|
|
753
|
+
def _lookup_ctag(self, token):
|
754
|
+
entry = ctags.TagEntry()
|
755
|
+
if self._ctags.find(entry, token, 0):
|
756
|
+
return entry['file'], entry['lineNumber']
|
757
|
+
else:
|
758
|
+
return None, None
|
759
|
+
|
691
760
|
def _highlight_lines(self, tokensource):
|
692
761
|
"""
|
693
762
|
Highlighted the lines specified in the `hl_lines` option by
|
@@ -740,6 +809,8 @@ class HtmlFormatter(Formatter):
|
|
740
809
|
source = self._wrap_inlinelinenos(source)
|
741
810
|
if self.lineanchors:
|
742
811
|
source = self._wrap_lineanchors(source)
|
812
|
+
if self.linespans:
|
813
|
+
source = self._wrap_linespans(source)
|
743
814
|
source = self.wrap(source, outfile)
|
744
815
|
if self.linenos == 1:
|
745
816
|
source = self._wrap_tablelinenos(source)
|
@@ -5,10 +5,10 @@
|
|
5
5
|
|
6
6
|
Base lexer classes.
|
7
7
|
|
8
|
-
:copyright: Copyright 2006-
|
8
|
+
:copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
|
9
9
|
:license: BSD, see LICENSE for details.
|
10
10
|
"""
|
11
|
-
import re
|
11
|
+
import re, itertools
|
12
12
|
|
13
13
|
from pygments.filter import apply_filters, Filter
|
14
14
|
from pygments.filters import get_filter_by_name
|
@@ -18,7 +18,7 @@ from pygments.util import get_bool_opt, get_int_opt, get_list_opt, \
|
|
18
18
|
|
19
19
|
|
20
20
|
__all__ = ['Lexer', 'RegexLexer', 'ExtendedRegexLexer', 'DelegatingLexer',
|
21
|
-
'LexerContext', 'include', 'bygroups', 'using', 'this']
|
21
|
+
'LexerContext', 'include', 'inherit', 'bygroups', 'using', 'this']
|
22
22
|
|
23
23
|
|
24
24
|
_encoding_map = [('\xef\xbb\xbf', 'utf-8'),
|
@@ -72,15 +72,18 @@ class Lexer(object):
|
|
72
72
|
#: Shortcuts for the lexer
|
73
73
|
aliases = []
|
74
74
|
|
75
|
-
#:
|
75
|
+
#: File name globs
|
76
76
|
filenames = []
|
77
77
|
|
78
|
-
#:
|
78
|
+
#: Secondary file name globs
|
79
79
|
alias_filenames = []
|
80
80
|
|
81
|
-
#:
|
81
|
+
#: MIME types
|
82
82
|
mimetypes = []
|
83
83
|
|
84
|
+
#: Priority, should multiple lexers match and no content is provided
|
85
|
+
priority = 0
|
86
|
+
|
84
87
|
__metaclass__ = LexerMeta
|
85
88
|
|
86
89
|
def __init__(self, **options):
|
@@ -164,9 +167,9 @@ class Lexer(object):
|
|
164
167
|
else:
|
165
168
|
text = text.decode(self.encoding)
|
166
169
|
else:
|
167
|
-
if text.startswith(u'\ufeff'):
|
170
|
+
if text.startswith(u'\ufeff'):
|
168
171
|
text = text[len(u'\ufeff'):]
|
169
|
-
|
172
|
+
|
170
173
|
# text now *is* a unicode string
|
171
174
|
text = text.replace('\r\n', '\n')
|
172
175
|
text = text.replace('\r', '\n')
|
@@ -242,6 +245,16 @@ class include(str):
|
|
242
245
|
pass
|
243
246
|
|
244
247
|
|
248
|
+
class _inherit(object):
|
249
|
+
"""
|
250
|
+
Indicates the a state should inherit from its superclass.
|
251
|
+
"""
|
252
|
+
def __repr__(self):
|
253
|
+
return 'inherit'
|
254
|
+
|
255
|
+
inherit = _inherit()
|
256
|
+
|
257
|
+
|
245
258
|
class combined(tuple):
|
246
259
|
"""
|
247
260
|
Indicates a state combined from multiple states.
|
@@ -432,6 +445,9 @@ class RegexLexerMeta(LexerMeta):
|
|
432
445
|
tokens.extend(cls._process_state(unprocessed, processed,
|
433
446
|
str(tdef)))
|
434
447
|
continue
|
448
|
+
if isinstance(tdef, _inherit):
|
449
|
+
# processed already
|
450
|
+
continue
|
435
451
|
|
436
452
|
assert type(tdef) is tuple, "wrong rule def %r" % tdef
|
437
453
|
|
@@ -460,6 +476,49 @@ class RegexLexerMeta(LexerMeta):
|
|
460
476
|
cls._process_state(tokendefs, processed, state)
|
461
477
|
return processed
|
462
478
|
|
479
|
+
def get_tokendefs(cls):
|
480
|
+
"""
|
481
|
+
Merge tokens from superclasses in MRO order, returning a single tokendef
|
482
|
+
dictionary.
|
483
|
+
|
484
|
+
Any state that is not defined by a subclass will be inherited
|
485
|
+
automatically. States that *are* defined by subclasses will, by
|
486
|
+
default, override that state in the superclass. If a subclass wishes to
|
487
|
+
inherit definitions from a superclass, it can use the special value
|
488
|
+
"inherit", which will cause the superclass' state definition to be
|
489
|
+
included at that point in the state.
|
490
|
+
"""
|
491
|
+
tokens = {}
|
492
|
+
inheritable = {}
|
493
|
+
for c in itertools.chain((cls,), cls.__mro__):
|
494
|
+
toks = c.__dict__.get('tokens', {})
|
495
|
+
|
496
|
+
for state, items in toks.iteritems():
|
497
|
+
curitems = tokens.get(state)
|
498
|
+
if curitems is None:
|
499
|
+
tokens[state] = items
|
500
|
+
try:
|
501
|
+
inherit_ndx = items.index(inherit)
|
502
|
+
except ValueError:
|
503
|
+
continue
|
504
|
+
inheritable[state] = inherit_ndx
|
505
|
+
continue
|
506
|
+
|
507
|
+
inherit_ndx = inheritable.pop(state, None)
|
508
|
+
if inherit_ndx is None:
|
509
|
+
continue
|
510
|
+
|
511
|
+
# Replace the "inherit" value with the items
|
512
|
+
curitems[inherit_ndx:inherit_ndx+1] = items
|
513
|
+
try:
|
514
|
+
new_inh_ndx = items.index(inherit)
|
515
|
+
except ValueError:
|
516
|
+
pass
|
517
|
+
else:
|
518
|
+
inheritable[state] = inherit_ndx + new_inh_ndx
|
519
|
+
|
520
|
+
return tokens
|
521
|
+
|
463
522
|
def __call__(cls, *args, **kwds):
|
464
523
|
"""Instantiate cls after preprocessing its token definitions."""
|
465
524
|
if '_tokens' not in cls.__dict__:
|
@@ -469,7 +528,7 @@ class RegexLexerMeta(LexerMeta):
|
|
469
528
|
# don't process yet
|
470
529
|
pass
|
471
530
|
else:
|
472
|
-
cls._tokens = cls.process_tokendef('', cls.
|
531
|
+
cls._tokens = cls.process_tokendef('', cls.get_tokendefs())
|
473
532
|
|
474
533
|
return type.__call__(cls, *args, **kwds)
|
475
534
|
|
@@ -610,7 +669,13 @@ class ExtendedRegexLexer(RegexLexer):
|
|
610
669
|
if new_state is not None:
|
611
670
|
# state transition
|
612
671
|
if isinstance(new_state, tuple):
|
613
|
-
|
672
|
+
for state in new_state:
|
673
|
+
if state == '#pop':
|
674
|
+
ctx.stack.pop()
|
675
|
+
elif state == '#push':
|
676
|
+
ctx.stack.append(statestack[-1])
|
677
|
+
else:
|
678
|
+
ctx.stack.append(state)
|
614
679
|
elif isinstance(new_state, int):
|
615
680
|
# pop
|
616
681
|
del ctx.stack[new_state:]
|
@@ -626,10 +691,10 @@ class ExtendedRegexLexer(RegexLexer):
|
|
626
691
|
break
|
627
692
|
if text[ctx.pos] == '\n':
|
628
693
|
# at EOL, reset state to "root"
|
629
|
-
ctx.pos += 1
|
630
694
|
ctx.stack = ['root']
|
631
695
|
statetokens = tokendefs['root']
|
632
696
|
yield ctx.pos, Text, u'\n'
|
697
|
+
ctx.pos += 1
|
633
698
|
continue
|
634
699
|
yield ctx.pos, Error, text[ctx.pos]
|
635
700
|
ctx.pos += 1
|
@@ -698,4 +763,3 @@ def do_insertions(insertions, tokens):
|
|
698
763
|
except StopIteration:
|
699
764
|
insleft = False
|
700
765
|
break # not strictly necessary
|
701
|
-
|