diff options
author | gbrandl <devnull@localhost> | 2007-08-15 09:23:51 +0200 |
---|---|---|
committer | gbrandl <devnull@localhost> | 2007-08-15 09:23:51 +0200 |
commit | 39524fd1a9125958e072590acdc7b955bf2e174a (patch) | |
tree | 938b2772408b2cb109c085080c77e45f81053e45 | |
parent | 77b8d3b7233ffb937451a19ca2ef78f0072426d0 (diff) | |
download | pygments-39524fd1a9125958e072590acdc7b955bf2e174a.tar.gz |
[svn] Apply a bunch of Tim Hatch's changes.
-rw-r--r-- | CHANGES | 10 | ||||
-rw-r--r-- | pygments/formatters/terminal256.py | 9 | ||||
-rw-r--r-- | pygments/lexers/compiled.py | 44 | ||||
-rw-r--r-- | pygments/lexers/functional.py | 99 | ||||
-rw-r--r-- | pygments/lexers/other.py | 37 | ||||
-rw-r--r-- | pygments/styles/vim.py | 61 |
6 files changed, 224 insertions, 36 deletions
@@ -3,7 +3,7 @@ Pygments changelog Version 0.9 ----------- -(codename not selected, released Jul XX, 2007) +(codename Strandkorb, released Aug XX, 2007) - The encoding handling of the command line mode (pygmentize) was enhanced. You shouldn't get UnicodeErrors from it anymore if you @@ -12,7 +12,7 @@ Version 0.9 - Added the ``lineanchors`` option to the HTML formatter, thanks to Ian Charnas for the idea. -- Give the line numbers table a CSS class in the HTML formatter. +- Gave the line numbers table a CSS class in the HTML formatter. - Added 256-color terminal formatter. @@ -20,7 +20,11 @@ Version 0.9 - Added Gettext catalog lexer. -- Greatly improved the Haskell lexer. +- Greatly improved the Haskell and OCaml lexers. + +- Improved the Bash lexer. + +- Added a Vim 7-like style. - Fixed bugs in the D and MiniD lexer. diff --git a/pygments/formatters/terminal256.py b/pygments/formatters/terminal256.py index 0d4a6f65..0516aa4e 100644 --- a/pygments/formatters/terminal256.py +++ b/pygments/formatters/terminal256.py @@ -16,8 +16,6 @@ """ # TODO: -# - 'nobold', 'nounderline' options to suppress bold and underline -# attributes respectively. # - Options to map style's bold/underline/italic/border attributes # to some ANSI attrbutes (something like 'italic=underline') # - An option to output "style RGB to xterm RGB/index" conversion table @@ -95,6 +93,9 @@ class Terminal256Formatter(Formatter): self.best_match = {} self.style_string = {} + self.usebold = 'nobold' not in options + self.useunderline = 'nounderline' not in options + self._build_color_table() # build an RGB-to-256 color conversion table self._setup_styles() # convert selected style's colors to term. colors @@ -173,9 +174,9 @@ class Terminal256Formatter(Formatter): escape.fg = self._color_index(ndef['color']) if ndef['bgcolor']: escape.bg = self._color_index(ndef['bgcolor']) - if ndef['bold']: + if self.usebold and ndef['bold']: escape.bold = True - if ndef['underline']: + if self.useunderline and ndef['underline']: escape.underline = True self.style_string[str(ttype)] = (escape.color_string(), escape.reset_string()) diff --git a/pygments/lexers/compiled.py b/pygments/lexers/compiled.py index 239a917f..fd192d75 100644 --- a/pygments/lexers/compiled.py +++ b/pygments/lexers/compiled.py @@ -24,6 +24,8 @@ from pygments.token import \ Text, Comment, Operator, Keyword, Name, String, Number, Punctuation, \ Error +# backwards compatibility +from pygments.lexers.functional import OcamlLexer __all__ = ['CLexer', 'CppLexer', 'DLexer', 'DelphiLexer', 'JavaLexer', 'DylanLexer', 'OcamlLexer', 'ObjectiveCLexer'] @@ -890,19 +892,17 @@ class OcamlLexer(RegexLexer): name = 'OCaml' aliases = ['ocaml'] - filenames = ['*.ml', '*.mli'] + filenames = ['*.ml', '*.mli', '*.mll', '*.mly'] mimetypes = ['text/x-ocaml'] keywords = [ - 'and', 'as', 'assert', 'asr', 'begin', 'class', - 'constraint', 'do', 'done', 'downto', 'else', 'end', - 'exception', 'external', 'false', 'for', 'fun', 'function', - 'functor', 'if', 'in', 'include', 'inherit', 'initializer', - 'land', 'lazy', 'let', 'lor', 'lsl', 'lsr', - 'lxor', 'match', 'method', 'mod', 'module', 'mutable', - 'new', 'object', 'of', 'open', 'or', 'private', - 'rec', 'sig', 'struct', 'then', 'to', 'true', - 'try', 'type', 'val', 'virtual', 'when', 'while', 'with' + 'as', 'assert', 'begin', 'class', 'constraint', 'do', 'done', + 'downto', 'else', 'end', 'exception', 'external', 'false', + 'for', 'fun', 'function', 'functor', 'if', 'in', 'include', + 'inherit', 'initializer', 'lazy', 'let', 'match', 'method', + 'module', 'mutable', 'new', 'object', 'of', 'open', 'private', + 'raise', 'rec', 'sig', 'struct', 'then', 'to', 'true', 'try', + 'type', 'val', 'virtual', 'when', 'while', 'with' ] keyopts = [ '!=','#','&','&&','\(','\)','\*','\+',',','-', @@ -912,8 +912,10 @@ class OcamlLexer(RegexLexer): ] operators = r'[!$%&*+\./:<=>?@^|~-]' + word_operators = ['and', 'asr', 'land', 'lor', 'lsl', 'lxor', 'mod', 'or'] prefix_syms = r'[!?~]' infix_syms = r'[=<>@^|&+\*/$%-]' + primitives = ['unit', 'int', 'float', 'bool', 'string', 'char', 'list', 'array'] tokens = { 'escape-sequence': [ @@ -923,18 +925,23 @@ class OcamlLexer(RegexLexer): ], 'root': [ (r'\s+', Text), + (r'false|true|\(\)|\[\]', Name.Builtin.Pseudo), + (r'\b([A-Z][A-Za-z0-9_\']*)(?=\s*\.)', + Name.Namespace, 'dotted'), + (r'\b([A-Z][A-Za-z0-9_\']*)', Name.Class), (r'\(\*', Comment, 'comment'), - (r'\b(%s)\b' % '|'.join(keywords), Keyword.Reserved), - (r'(%s)' % '|'.join(keyopts), Keyword), - (r'false|true|\(\)|\[\]', Name.Constant), + (r'\b(%s)\b' % '|'.join(keywords), Keyword), + (r'(%s)' % '|'.join(keyopts), Operator), (r'(%s|%s)?%s' % (infix_syms, prefix_syms, operators), Operator), + (r'\b(%s)\b' % '|'.join(word_operators), Operator.Word), + (r'\b(%s)\b' % '|'.join(primitives), Keyword.Type), (r"[^\W\d][\w']*", Name), (r'\d[\d_]*', Number.Integer), (r'0[xX][\da-fA-F][\da-fA-F_]*', Number.Hex), (r'0[oO][0-7][0-7_]*', Number.Oct), - (r'0[bB][01][01_]*', Number), + (r'0[bB][01][01_]*', Number.Binary), (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float), (r"'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'", @@ -957,7 +964,14 @@ class OcamlLexer(RegexLexer): include('escape-sequence'), (r'\\\n', String.Double), (r'"', String.Double, '#pop'), - ] + ], + 'dotted': [ + (r'\s+', Text), + (r'\.', Punctuation), + (r'[A-Z][A-Za-z0-9_\']*(?=\s*\.)', Name.Namespace), + (r'[A-Z][A-Za-z0-9_\']*', Name.Class, '#pop'), + (r'[a-z][a-z0-9_\']*', Name, '#pop'), + ], } diff --git a/pygments/lexers/functional.py b/pygments/lexers/functional.py index 32e48eda..8bd4afff 100644 --- a/pygments/lexers/functional.py +++ b/pygments/lexers/functional.py @@ -21,7 +21,7 @@ from pygments.token import Text, Comment, Operator, Keyword, Name, \ String, Number, Punctuation -__all__ = ['SchemeLexer', 'HaskellLexer'] +__all__ = ['SchemeLexer', 'HaskellLexer', 'OCamlLexer'] class SchemeLexer(RegexLexer): @@ -39,7 +39,7 @@ class SchemeLexer(RegexLexer): *New in Pygments 0.6.* """ name = 'Scheme' - aliases = ['scheme'] + aliases = ['scheme', 'scm'] filenames = ['*.scm'] mimetypes = ['text/x-scheme', 'application/x-scheme'] @@ -159,7 +159,7 @@ class HaskellLexer(RegexLexer): *New in Pygments 0.8.* """ name = 'Haskell' - aliases = ['haskell'] + aliases = ['haskell', 'hs'] filenames = ['*.hs'] reserved = ['case','class','data','default','deriving','do','else', @@ -199,6 +199,7 @@ class HaskellLexer(RegexLexer): (r'"', String, 'string'), # Special (r'\[\]', Keyword.Type), + (r'\(\)', Name.Builtin), (r'[][(),;`{}]', Punctuation), ], 'import': [ @@ -262,3 +263,95 @@ class HaskellLexer(RegexLexer): (r'\d+', String.Escape, '#pop'), ], } + + +class OcamlLexer(RegexLexer): + """ + For the OCaml language. + + *New in Pygments 0.7.* + """ + + name = 'OCaml' + aliases = ['ocaml'] + filenames = ['*.ml', '*.mli', '*.mll', '*.mly'] + mimetypes = ['text/x-ocaml'] + + keywords = [ + 'as', 'assert', 'begin', 'class', 'constraint', 'do', 'done', + 'downto', 'else', 'end', 'exception', 'external', 'false', + 'for', 'fun', 'function', 'functor', 'if', 'in', 'include', + 'inherit', 'initializer', 'lazy', 'let', 'match', 'method', + 'module', 'mutable', 'new', 'object', 'of', 'open', 'private', + 'raise', 'rec', 'sig', 'struct', 'then', 'to', 'true', 'try', + 'type', 'val', 'virtual', 'when', 'while', 'with' + ] + keyopts = [ + '!=','#','&','&&','\(','\)','\*','\+',',','-', + '-\.','->','\.','\.\.',':','::',':=',':>',';',';;','<', + '<-','=','>','>]','>}','\?','\?\?','\[','\[<','\[>','\[\|', + ']','_','`','{','{<','\|','\|]','}','~' + ] + + operators = r'[!$%&*+\./:<=>?@^|~-]' + word_operators = ['and', 'asr', 'land', 'lor', 'lsl', 'lxor', 'mod', 'or'] + prefix_syms = r'[!?~]' + infix_syms = r'[=<>@^|&+\*/$%-]' + primitives = ['unit', 'int', 'float', 'bool', 'string', 'char', 'list', 'array'] + + tokens = { + 'escape-sequence': [ + (r'\\[\"\'ntbr]', String.Escape), + (r'\\[0-9]{3}', String.Escape), + (r'\\x[0-9a-fA-F]{2}', String.Escape), + ], + 'root': [ + (r'\s+', Text), + (r'false|true|\(\)|\[\]', Name.Builtin.Pseudo), + (r'\b([A-Z][A-Za-z0-9_\']*)(?=\s*\.)', + Name.Namespace, 'dotted'), + (r'\b([A-Z][A-Za-z0-9_\']*)', Name.Class), + (r'\(\*', Comment, 'comment'), + (r'\b(%s)\b' % '|'.join(keywords), Keyword), + (r'(%s)' % '|'.join(keyopts), Operator), + (r'(%s|%s)?%s' % (infix_syms, prefix_syms, operators), Operator), + (r'\b(%s)\b' % '|'.join(word_operators), Operator.Word), + (r'\b(%s)\b' % '|'.join(primitives), Keyword.Type), + + (r"[^\W\d][\w']*", Name), + + (r'\d[\d_]*', Number.Integer), + (r'0[xX][\da-fA-F][\da-fA-F_]*', Number.Hex), + (r'0[oO][0-7][0-7_]*', Number.Oct), + (r'0[bB][01][01_]*', Number.Binary), + (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float), + + (r"'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'", + String.Char), + (r"'.'", String.Char), + (r"'", Keyword), # a stray quote is another syntax element + + (r'"', String.Double, 'string'), + + (r'[~?][a-z][\w\']*:', Name.Variable), + ], + 'comment': [ + (r'[^(*)]', Comment), + (r'\(\*', Comment, '#push'), + (r'\*\)', Comment, '#pop'), + (r'[(*)]', Comment), + ], + 'string': [ + (r'[^\\"]', String.Double), + include('escape-sequence'), + (r'\\\n', String.Double), + (r'"', String.Double, '#pop'), + ], + 'dotted': [ + (r'\s+', Text), + (r'\.', Punctuation), + (r'[A-Z][A-Za-z0-9_\']*(?=\s*\.)', Name.Namespace), + (r'[A-Z][A-Za-z0-9_\']*', Name.Class, '#pop'), + (r'[a-z][a-z0-9_\']*', Name, '#pop'), + ], + } diff --git a/pygments/lexers/other.py b/pygments/lexers/other.py index 345f53fa..8eec375c 100644 --- a/pygments/lexers/other.py +++ b/pygments/lexers/other.py @@ -209,12 +209,17 @@ class BashLexer(RegexLexer): filenames = ['*.sh'] mimetypes = ['application/x-sh', 'application/x-shellscript'] - # TODO: heredocs and perhaps some other niceties - tokens = { 'root': [ + include('basic'), + (r'\$\(', Keyword, 'paren'), + (r'\${#?', Keyword, 'curly'), + (r'`', String.Backtick, 'backticks'), + include('data'), + ], + 'basic': [ (r'\b(if|fi|else|while|do|done|for|then|return|function|case|' - r'select|continue|until)\s*\b', + r'select|continue|until|esac|elif)\s*\b', Keyword), (r'\b(alias|bg|bind|break|builtin|caller|cd|command|compgen|' r'complete|declare|dirs|disown|echo|enable|eval|exec|exit|' @@ -224,24 +229,34 @@ class BashLexer(RegexLexer): r'ulimit|umask|unalias|unset|wait)\s*\b', Name.Builtin), (r'#.*\n', Comment), + (r'\\[\w\W]', String.Escape), (r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)), (r'[\[\]{}()=]+', Operator), - (r'(\$\()(.*?)(\))', bygroups(Keyword, using(this), Keyword)), - (r'\${#?', Keyword, 'curly'), - (r'`.+`', String), - (r'\d+(?= |\Z)', Number), - (r'\$#?(\w+|.)', Name.Variable), + (r'<<\s*(\'?)\\?(\w+)[\w\W]+?\2', String), + ], + 'data': [ (r'"(\\\\|\\[0-7]+|\\.|[^"])*"', String.Double), (r"'(\\\\|\\[0-7]+|\\.|[^'])*'", String.Single), (r'\s+', Text), - (r'[^=\s\n\[\]{}()$"\']+', Text), + (r'[^=\s\n\[\]{}()$"\'`\\]+', Text), + (r'\d+(?= |\Z)', Number), + (r'\$#?(\w+|.)', Name.Variable), ], 'curly': [ (r'}', Keyword, '#pop'), (r':-', Keyword), - (r'[a-zA-Z0-9]+', Name.Variable), - (r'[^}:]+', Punctuation), + (r'[a-zA-Z0-9_]+', Name.Variable), + (r'[^}:"\'`$]+', Punctuation), (r':', Punctuation), + include('root'), + ], + 'paren': [ + (r'\)', Keyword, '#pop'), + include('root'), + ], + 'backticks': [ + (r'`', String.Backtick, '#pop'), + include('root'), ], } diff --git a/pygments/styles/vim.py b/pygments/styles/vim.py new file mode 100644 index 00000000..680af28f --- /dev/null +++ b/pygments/styles/vim.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +""" + pygments.styles.vim + ~~~~~~~~~~~~~~~~~~~~~ + + A highlighting style for Pygments, inspired by vim. + + :copyright: 2006-2007 by Tim Hatch. + :license: BSD, see LICENSE for more details. +""" + +from pygments.style import Style +from pygments.token import Keyword, Name, Comment, String, Error, \ + Number, Operator, Generic, Whitespace, Token + + +class VimStyle(Style): + """ + Styles somewhat like vim 7.0 + """ + + background_color = "#000000" + default_style = "#cccccc" + + styles = { + Token: "#cccccc", + Whitespace: "", + Comment: "#00cdcd", + Comment.Preproc: "", + Comment.Special: "bold #cd0000", + + Keyword: "#cdcd00", + Keyword.Pseudo: "", + Keyword.Type: "#00cd00", + + Operator: "#3399cc", + Operator.Word: "#cdcd00", + + Name: "", + Name.Class: "#cd00cd", + Name.Builtin: "#cd00cd", + Name.Namespace: "bold #5c5cff", + Name.Exception: "bold #666699", + Name.Variable: "#00cdcd", + + String: "#cd0000", + Number: "#cd00cd", + + Generic.Heading: "bold #000080", + Generic.Subheading: "bold #800080", + Generic.Deleted: "#A00000", + Generic.Inserted: "#00A000", + Generic.Error: "#FF0000", + Generic.Emph: "italic", + Generic.Strong: "bold", + Generic.Prompt: "bold #000080", + Generic.Output: "#888", + Generic.Traceback: "#04D", + + Error: "border:#FF0000" + } |