diff options
author | gbrandl <devnull@localhost> | 2006-12-10 12:07:57 +0100 |
---|---|---|
committer | gbrandl <devnull@localhost> | 2006-12-10 12:07:57 +0100 |
commit | e0e37bfe227c281f00de6a07e3384b3f53b0055e (patch) | |
tree | e632816ff30fdd9ef3d3dfbb90c537d9ac23822f /pygments/lexers | |
parent | 74d7585b086181a8cd62205938970b80fe7aa4df (diff) | |
download | pygments-e0e37bfe227c281f00de6a07e3384b3f53b0055e.tar.gz |
[svn] Add Groff lexer by Tim Hatch, find_error script, CSS lexer improvement.
Add analyse_text methods to some lexers.
Diffstat (limited to 'pygments/lexers')
-rw-r--r-- | pygments/lexers/_mapping.py | 1 | ||||
-rw-r--r-- | pygments/lexers/text.py | 77 | ||||
-rw-r--r-- | pygments/lexers/web.py | 14 |
3 files changed, 88 insertions, 4 deletions
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py index ab56d8c0..ff82b891 100644 --- a/pygments/lexers/_mapping.py +++ b/pygments/lexers/_mapping.py @@ -31,6 +31,7 @@ LEXERS = { 'ErbLexer': ('pygments.lexers.templates', 'ERB', ('erb',), (), ()), 'GenshiLexer': ('pygments.lexers.templates', 'Genshi', ('genshi', 'kid', 'xml+genshi', 'xml+kid'), ('*.kid',), ()), 'GenshiTextLexer': ('pygments.lexers.templates', 'Genshi Text', ('genshitext',), (), ()), + 'GroffLexer': ('pygments.lexers.text', 'Groff', ('groff', 'nroff', 'man'), ('*.[1234567]', '*.man'), ('application/x-troff',)), 'HtmlDjangoLexer': ('pygments.lexers.templates', 'HTML+Django/Jinja', ('html+django', 'html+jinja'), (), ()), 'HtmlGenshiLexer': ('pygments.lexers.templates', 'HTML+Genshi', ('html+genshi', 'html+kid'), (), ()), 'HtmlLexer': ('pygments.lexers.web', 'HTML', ('html',), ('*.html', '*.htm', '*.xhtml'), ('text/html', 'application/xhtml+xml')), diff --git a/pygments/lexers/text.py b/pygments/lexers/text.py index c322badf..8e9a2f13 100644 --- a/pygments/lexers/text.py +++ b/pygments/lexers/text.py @@ -5,7 +5,8 @@ Lexers for non-source code file types: Diff, Makefiles, Ini configs etc. - :copyright: 2006 by Armin Ronacher, Georg Brandl. + :copyright: 2006 by Armin Ronacher, Georg Brandl, + Tim Hatch <tim@timhatch.com>. :license: BSD, see LICENSE for more details. """ @@ -17,7 +18,7 @@ from pygments.token import \ __all__ = ['IniLexer', 'MakefileLexer', 'DiffLexer', 'IrcLogsLexer', - 'TexLexer'] + 'TexLexer', 'GroffLexer'] class IniLexer(RegexLexer): @@ -35,6 +36,12 @@ class IniLexer(RegexLexer): ] } + def analyse_text(text): + npos = text.find('\n') + if npos < 3: + return False + return text[0] == '[' and text[npos-1] == ']' + class MakefileLexer(RegexLexer): name = 'Makefile' @@ -98,6 +105,14 @@ class DiffLexer(RegexLexer): ] } + def analyse_text(text): + if text[:7] == 'Index: ': + return True + if text[:5] == 'diff ': + return True + if text[:4] == '--- ': + return 0.9 + class IrcLogsLexer(RegexLexer): name = 'IRC logs' @@ -184,3 +199,61 @@ class TexLexer(RegexLexer): (r'', Text, '#pop'), ], } + + def analyse_text(text): + for start in ("\\documentclass", "\\input", "\\documentstyle", + "\\relax"): + if text[:len(start)] == start: + return True + + +class GroffLexer(RegexLexer): + """ + Lexer for the roff format, supporting groff extensions. Mainly useful + for highlighting manpages. + """ + name = 'Groff' + aliases = ['groff', 'nroff', 'man'] + filenames = ['*.[1234567]', '*.man'] + mimetypes = ['application/x-troff', 'text/troff'] + + tokens = { + 'root': [ + (r'(?i)(\.)(\w+)', bygroups(Text, Keyword), 'request'), + (r'\.', Text, 'request'), + # Regular characters, slurp till we find a backslash or newline + (r'[^\\\n]*', Text, 'textline'), + ], + 'textline': [ + include('escapes'), + (r'[^\\\n]+', Text), + (r'\n', Text, '#pop'), + ], + 'escapes': [ + # groff has many ways to write escapes. + (r'\\"[^\n]*', Comment), + (r'\\[fn]\w', String.Escape), + (r'\\\(..', String.Escape), + (r'\\.\[.*\]', String.Escape), + (r'\\.', String.Escape), + (r'\\\n', Text, 'request'), + ], + 'request': [ + (r'\n', Text, '#pop'), + include('escapes'), + (r'"[^\n"]+"', String.Double), + (r'\d+', Number), + (r'\S+', String), + (r'\s+', Text), + ], + } + + def analyse_text(text): + if text[0] != '.': + return False + if text[:3] == '.\\"': + return True + if text[:4] == '.TH ': + return True + if text[1:3].isalnum() and text[3].isspace(): + return 0.9 diff --git a/pygments/lexers/web.py b/pygments/lexers/web.py index 904c8c11..28320308 100644 --- a/pygments/lexers/web.py +++ b/pygments/lexers/web.py @@ -5,7 +5,8 @@ Lexers for web-related languages: JavaScript, CSS, HTML, XML, PHP. - :copyright: 2006 by Georg Brandl, Armin Ronacher. + :copyright: 2006 by Georg Brandl, Armin Ronacher, + Tim Hatch <tim@timhatch.com>. :license: BSD, see LICENSE for more details. """ @@ -15,7 +16,7 @@ try: except NameError: from sets import Set as set -from pygments.lexer import Lexer, RegexLexer, bygroups, using +from pygments.lexer import Lexer, RegexLexer, bygroups, using, include from pygments.token import \ Text, Comment, Operator, Keyword, Name, String, Number, Other from pygments.util import get_bool_opt, get_list_opt, looks_like_xml, \ @@ -65,6 +66,11 @@ class CssLexer(RegexLexer): tokens = { 'root': [ + (r'(@media)(\s+)(\w+)(\s*)({)', bygroups(Keyword, Text, String, + Text, Operator), 'media'), + include('basics'), + ], + 'basics': [ (r'\s+', Text), (r'/\*(?:.|\n)*?\*/', Comment), (r'{', Operator, 'content'), @@ -76,6 +82,10 @@ class CssLexer(RegexLexer): (r'"(\\\\|\\"|[^"])*"', String.Double), (r"'(\\\\|\\'|[^'])*'", String.Single) ], + 'media': [ + include('basics'), + (r'}', Operator, '#pop') + ], 'content': [ (r'\s+', Text), (r'}', Operator, '#pop'), |