diff options
author | Tim Hatch <tim@timhatch.com> | 2014-10-10 07:37:27 -0700 |
---|---|---|
committer | Tim Hatch <tim@timhatch.com> | 2014-10-10 07:37:27 -0700 |
commit | 9babe3a89768c713a87e68115ee39bf4fe1cc717 (patch) | |
tree | 974531925e67c112b72c827a1317d1bbfeccf945 | |
parent | 1cbd15aeb7818e0d905e5794199e7a8dbd14cb27 (diff) | |
download | pygments-9babe3a89768c713a87e68115ee39bf4fe1cc717.tar.gz |
Add Mozilla preprocessor lexers.
Replaces PR #239
-rw-r--r-- | pygments/lexers/_mapping.py | 7 | ||||
-rw-r--r-- | pygments/lexers/markup.py | 130 | ||||
-rw-r--r-- | tests/examplefiles/demo.css.in | 6 | ||||
-rw-r--r-- | tests/examplefiles/demo.js.in | 6 | ||||
-rw-r--r-- | tests/examplefiles/demo.xul.in | 7 |
5 files changed, 151 insertions, 5 deletions
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py index 6d7c94df..e3ab63f4 100644 --- a/pygments/lexers/_mapping.py +++ b/pygments/lexers/_mapping.py @@ -176,7 +176,7 @@ LEXERS = { 'JavascriptLexer': ('pygments.lexers.javascript', 'JavaScript', ('js', 'javascript'), ('*.js',), ('application/javascript', 'application/x-javascript', 'text/x-javascript', 'text/javascript')), 'JavascriptPhpLexer': ('pygments.lexers.templates', 'JavaScript+PHP', ('js+php', 'javascript+php'), (), ('application/x-javascript+php', 'text/x-javascript+php', 'text/javascript+php')), 'JavascriptSmartyLexer': ('pygments.lexers.templates', 'JavaScript+Smarty', ('js+smarty', 'javascript+smarty'), (), ('application/x-javascript+smarty', 'text/x-javascript+smarty', 'text/javascript+smarty')), - 'JsonLdLexer': ('pygments.lexers.data', 'JSON-LD', ('json-ld',), ('*.jsonld',), ('application/json-ld',)), + 'JsonLdLexer': ('pygments.lexers.data', 'JSON-LD', ('jsonld', 'json-ld'), ('*.jsonld',), ('application/ld+json',)), 'JsonLexer': ('pygments.lexers.data', 'JSON', ('json',), ('*.json',), ('application/json',)), 'JspLexer': ('pygments.lexers.templates', 'Java Server Page', ('jsp',), ('*.jsp',), ('application/x-jsp',)), 'JuliaConsoleLexer': ('pygments.lexers.julia', 'Julia console', ('jlcon',), (), ()), @@ -223,6 +223,11 @@ LEXERS = { 'MoinWikiLexer': ('pygments.lexers.markup', 'MoinMoin/Trac Wiki markup', ('trac-wiki', 'moin'), (), ('text/x-trac-wiki',)), 'MonkeyLexer': ('pygments.lexers.basic', 'Monkey', ('monkey',), ('*.monkey',), ('text/x-monkey',)), 'MoonScriptLexer': ('pygments.lexers.scripting', 'MoonScript', ('moon', 'moonscript'), ('*.moon',), ('text/x-moonscript', 'application/x-moonscript')), + 'MozPreprocCssLexer': ('pygments.lexers.markup', 'CSS+mozpreproc', ('css+mozpreproc',), ('*.css.in',), ()), + 'MozPreprocHashLexer': ('pygments.lexers.markup', 'mozhashpreproc', ('mozhashpreproc',), (), ()), + 'MozPreprocJavascriptLexer': ('pygments.lexers.markup', 'Javascript+mozpreproc', ('javascript+mozpreproc',), ('*.js.in',), ()), + 'MozPreprocPercentLexer': ('pygments.lexers.markup', 'mozpercentpreproc', ('mozpercentpreproc',), (), ()), + 'MozPreprocXulLexer': ('pygments.lexers.markup', 'XUL+mozpreproc', ('xul+mozpreproc',), ('*.xul.in',), ()), 'MqlLexer': ('pygments.lexers.c_like', 'MQL', ('mql', 'mq4', 'mq5', 'mql4', 'mql5'), ('*.mq4', '*.mq5', '*.mqh'), ('text/x-mql',)), 'MscgenLexer': ('pygments.lexers.dsls', 'Mscgen', ('mscgen', 'msc'), ('*.msc',), ()), 'MuPADLexer': ('pygments.lexers.algebra', 'MuPAD', ('mupad',), ('*.mu',), ()), diff --git a/pygments/lexers/markup.py b/pygments/lexers/markup.py index 71a9aa82..285c1350 100644 --- a/pygments/lexers/markup.py +++ b/pygments/lexers/markup.py @@ -11,13 +11,20 @@ import re -from pygments.lexer import RegexLexer, include, bygroups, using, this, \ - do_insertions, default +from pygments.lexers.html import HtmlLexer, XmlLexer +from pygments.lexers.javascript import JavascriptLexer +from pygments.lexers.css import CssLexer + +from pygments.lexer import RegexLexer, DelegatingLexer, include, bygroups, \ + using, this, do_insertions, default, words from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ - Number, Punctuation, Generic + Number, Punctuation, Generic, Other from pygments.util import get_bool_opt, ClassNotFound -__all__ = ['BBCodeLexer', 'MoinWikiLexer', 'RstLexer', 'TexLexer', 'GroffLexer'] +__all__ = ['BBCodeLexer', 'MoinWikiLexer', 'RstLexer', 'TexLexer', 'GroffLexer', + 'MozPreprocHashLexer', 'MozPreprocPercentLexer', + 'MozPreprocXulLexer', 'MozPreprocJavascriptLexer', + 'MozPreprocCssLexer'] class BBCodeLexer(RegexLexer): @@ -378,3 +385,118 @@ class GroffLexer(RegexLexer): return True if text[1:3].isalnum() and text[3].isspace(): return 0.9 + + +class MozPreprocHashLexer(RegexLexer): + """ + Lexer for Mozilla Preprocessor files (with '#' as the marker). + + Other data is left untouched. + + .. versionadded:: 2.0 + """ + name = 'mozhashpreproc' + aliases = [name] + filenames = [] + mimetypes = [] + + tokens = { + 'root': [ + (r'^#', Comment.Preproc, ('expr', 'exprstart')), + (r'.+', Other), + ], + 'exprstart': [ + (r'(literal)(.*)', bygroups(Comment.Preproc, Text), '#pop:2'), + (words(( + 'define', 'undef', 'if', 'ifdef', 'ifndef', 'else', 'elif', + 'elifdef', 'elifndef', 'endif', 'expand', 'filter', 'unfilter', + 'include', 'includesubst', 'error')), + Comment.Preproc, '#pop'), + ], + 'expr': [ + (words(('!', '!=', '==', '&&', '||')), Operator), + (r'(defined)(\()', bygroups(Keyword, Punctuation)), + (r'\)', Punctuation), + (r'[0-9]+', Number.Decimal), + (r'__\w+?__', Name.Variable), + (r'@\w+?@', Name.Class), + (r'\w+', Name), + (r'\n', Text, '#pop'), + (r'\s+', Text), + (r'\S', Punctuation), + ], + } + + +class MozPreprocPercentLexer(MozPreprocHashLexer): + """ + Lexer for Mozilla Preprocessor files (with '%' as the marker). + + Other data is left untouched. + + .. versionadded:: 2.0 + """ + name = 'mozpercentpreproc' + aliases = [name] + filenames = [] + mimetypes = [] + + + tokens = { + 'root': [ + (r'^%', Comment.Preproc, ('expr', 'exprstart')), + (r'.+', Other), + ], + } + + +class MozPreprocXulLexer(DelegatingLexer): + """ + Subclass of the `MozPreprocHashLexer` that highlights unlexed data with the + `XmlLexer`. + + .. versionadded:: 2.0 + """ + name = "XUL+mozpreproc" + aliases = ['xul+mozpreproc'] + filenames = ['*.xul.in'] + mimetypes = [] + + def __init__(self, **options): + super(MozPreprocXulLexer, self).__init__( + XmlLexer, MozPreprocHashLexer, **options) + + +class MozPreprocJavascriptLexer(DelegatingLexer): + """ + Subclass of the `MozPreprocHashLexer` that highlights unlexed data with the + `JavascriptLexer`. + + .. versionadded:: 2.0 + """ + name = "Javascript+mozpreproc" + aliases = ['javascript+mozpreproc'] + filenames = ['*.js.in'] + mimetypes = [] + + def __init__(self, **options): + super(MozPreprocJavascriptLexer, self).__init__( + JavascriptLexer, MozPreprocHashLexer, **options) + + +class MozPreprocCssLexer(DelegatingLexer): + """ + Subclass of the `MozPreprocHashLexer` that highlights unlexed data with the + `CssLexer`. + + .. versionadded:: 2.0 + """ + name = "CSS+mozpreproc" + aliases = ['css+mozpreproc'] + filenames = ['*.css.in'] + mimetypes = [] + + def __init__(self, **options): + super(MozPreprocCssLexer, self).__init__( + CssLexer, MozPreprocPercentLexer, **options) + diff --git a/tests/examplefiles/demo.css.in b/tests/examplefiles/demo.css.in new file mode 100644 index 00000000..36330a9d --- /dev/null +++ b/tests/examplefiles/demo.css.in @@ -0,0 +1,6 @@ +%if defined(__foo__) +.cls { + color: #fff; +} +%endif +%literal %foo diff --git a/tests/examplefiles/demo.js.in b/tests/examplefiles/demo.js.in new file mode 100644 index 00000000..f44fc53d --- /dev/null +++ b/tests/examplefiles/demo.js.in @@ -0,0 +1,6 @@ +window.foo = { +#if defined(__foo__) + 'key': 'value' +#endif +} +#literal #foo diff --git a/tests/examplefiles/demo.xul.in b/tests/examplefiles/demo.xul.in new file mode 100644 index 00000000..9e1f4938 --- /dev/null +++ b/tests/examplefiles/demo.xul.in @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="iso-8859-1"?> +<window + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> +#if defined(__foo__) +<description value="Text" /> +#endif +</window> |