diff options
-rw-r--r-- | CHANGES | 1 | ||||
-rw-r--r-- | pygments/lexers/_mapping.py | 1 | ||||
-rw-r--r-- | pygments/lexers/other.py | 57 | ||||
-rw-r--r-- | tests/examplefiles/example.sh-session | 17 |
4 files changed, 74 insertions, 2 deletions
@@ -19,6 +19,7 @@ Version 1.1 * GLSL (#369) * Erlang shell + * (Ba)sh shell Version 1.0 diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py index bf67e78d..4db345e7 100644 --- a/pygments/lexers/_mapping.py +++ b/pygments/lexers/_mapping.py @@ -21,6 +21,7 @@ LEXERS = { 'BBCodeLexer': ('pygments.lexers.text', 'BBCode', ('bbcode',), (), ('text/x-bbcode',)), 'BaseMakefileLexer': ('pygments.lexers.text', 'Makefile', ('basemake',), (), ()), 'BashLexer': ('pygments.lexers.other', 'Bash', ('bash', 'sh'), ('*.sh',), ('application/x-sh', 'application/x-shellscript')), + 'BashSessionLexer': ('pygments.lexers.other', 'Bash Session', ('console',), ('*.sh-session',), ('application/x-shell-session',)), 'BatchLexer': ('pygments.lexers.other', 'Batchfile', ('bat',), ('*.bat', '*.cmd'), ('application/x-dos-batch',)), 'BefungeLexer': ('pygments.lexers.other', 'Befunge', ('befunge',), ('*.befunge',), ('application/x-befunge',)), 'BooLexer': ('pygments.lexers.dotnet', 'Boo', ('boo',), ('*.boo',), ('text/x-boo',)), diff --git a/pygments/lexers/other.py b/pygments/lexers/other.py index 6b78a5ce..a2a24cef 100644 --- a/pygments/lexers/other.py +++ b/pygments/lexers/other.py @@ -13,7 +13,8 @@ import re -from pygments.lexer import Lexer, RegexLexer, include, bygroups, do_insertions +from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, \ + this, do_insertions from pygments.token import Error, Punctuation, \ Text, Comment, Operator, Keyword, Name, String, Number, Generic from pygments.util import shebang_matches @@ -22,7 +23,8 @@ from pygments.util import shebang_matches __all__ = ['SqlLexer', 'MySqlLexer', 'SqliteConsoleLexer', 'BrainfuckLexer', 'BashLexer', 'BatchLexer', 'BefungeLexer', 'RedcodeLexer', 'MOOCodeLexer', 'SmalltalkLexer', 'TcshLexer', 'LogtalkLexer', - 'GnuplotLexer', 'PovrayLexer', 'AppleScriptLexer'] + 'GnuplotLexer', 'PovrayLexer', 'AppleScriptLexer', + 'BashSessionLexer'] line_re = re.compile('.*?\n') @@ -316,6 +318,7 @@ class BefungeLexer(RegexLexer): } + class BashLexer(RegexLexer): """ Lexer for (ba)sh shell scripts. @@ -393,6 +396,56 @@ class BashLexer(RegexLexer): return shebang_matches(text, r'(ba|z|)sh') +class BashSessionLexer(Lexer): + """ + Lexer for simplistic shell sessions. + + *New in Pygments 1.1* + """ + + name = 'Bash Session' + aliases = ['console'] + filenames = ['*.sh-session'] + mimetypes = ['application/x-shell-session'] + + def get_tokens_unprocessed(self, text): + bashlexer = BashLexer(**self.options) + + pos = 0 + curcode = '' + insertions = [] + + for match in line_re.finditer(text): + line = match.group() + m = re.match(r'^((?:|sh\S*?|\w+\S+[@:]\S+(?:\s+\S+)?|\[\S+[@:][^\n]+\].+)[$#%])(.*\n?)', line) + if m: + # To support output lexers (say diff output), the output + # needs to be broken by prompts whenever the output lexer + # changes. + if not insertions: + pos = match.start() + + insertions.append((len(curcode), + [(0, Generic.Prompt, m.group(1))])) + curcode += m.group(2) + elif line.startswith('>'): + insertions.append((len(curcode), + [(0, Generic.Prompt, line[:1])])) + curcode += line[1:] + else: + if insertions: + for i, t, v in do_insertions(insertions, + bashlexer.get_tokens_unprocessed(curcode)): + yield pos+i, t, v + yield match.start(), Generic.Output, line + insertions = [] + curcode = '' + if insertions: + for i, t, v in do_insertions(insertions, + bashlexer.get_tokens_unprocessed(curcode)): + yield pos+i, t, v + + class BatchLexer(RegexLexer): """ Lexer for the DOS/Windows Batch file format. diff --git a/tests/examplefiles/example.sh-session b/tests/examplefiles/example.sh-session new file mode 100644 index 00000000..35b81ebb --- /dev/null +++ b/tests/examplefiles/example.sh-session @@ -0,0 +1,17 @@ +user@host:~/path$ ls -a +. .. a b c +user@host:~/path$ diff -u a b +--- a 2008-07-26 17:10:07.000000000 -0700 ++++ b 2008-07-26 17:10:10.000000000 -0700 +@@ -1,3 +1,3 @@ + a +-b ++x + c +user@host:~/path$ echo \ +> a +a +user@host:~/path$ su +root@host:~# +sh-3.1$ # on hardy +sh$ # on etch |