diff options
author | Tim Hatch <tim@timhatch.com> | 2016-06-01 00:49:28 -0700 |
---|---|---|
committer | Tim Hatch <tim@timhatch.com> | 2016-06-01 00:49:28 -0700 |
commit | 7c9e7f5d18971e5cc5efe14bff3fd429480a0140 (patch) | |
tree | 5a710f10b9841e45708cf19adcb5913a0733fbb2 | |
parent | 0ed2df2cf1625f22d0ee8cfad7d0c711a2ee9fb6 (diff) | |
download | pygments-7c9e7f5d18971e5cc5efe14bff3fd429480a0140.tar.gz |
Add backslash-continuation support to console lexer.
Fixes #1237
-rw-r--r-- | pygments/lexers/shell.py | 8 | ||||
-rw-r--r-- | tests/test_shell.py | 23 |
2 files changed, 29 insertions, 2 deletions
diff --git a/pygments/lexers/shell.py b/pygments/lexers/shell.py index b9368ade..ae790b9e 100644 --- a/pygments/lexers/shell.py +++ b/pygments/lexers/shell.py @@ -137,11 +137,15 @@ class ShellSessionBaseLexer(Lexer): pos = 0 curcode = '' insertions = [] + backslash_continuation = False for match in line_re.finditer(text): line = match.group() m = re.match(self._ps1rgx, line) - if m: + if backslash_continuation: + curcode += line + backslash_continuation = curcode.endswith('\\\n') + elif m: # To support output lexers (say diff output), the output # needs to be broken by prompts whenever the output lexer # changes. @@ -151,10 +155,12 @@ class ShellSessionBaseLexer(Lexer): insertions.append((len(curcode), [(0, Generic.Prompt, m.group(1))])) curcode += m.group(2) + backslash_continuation = curcode.endswith('\\\n') elif line.startswith(self._ps2): insertions.append((len(curcode), [(0, Generic.Prompt, line[:len(self._ps2)])])) curcode += line[len(self._ps2):] + backslash_continuation = curcode.endswith('\\\n') else: if insertions: toks = innerlexer.get_tokens_unprocessed(curcode) diff --git a/tests/test_shell.py b/tests/test_shell.py index 76065caa..6faac9fd 100644 --- a/tests/test_shell.py +++ b/tests/test_shell.py @@ -10,7 +10,7 @@ import unittest from pygments.token import Token -from pygments.lexers import BashLexer +from pygments.lexers import BashLexer, BashSessionLexer class BashTest(unittest.TestCase): @@ -119,3 +119,24 @@ class BashTest(unittest.TestCase): ] self.assertEqual(tokens, list(self.lexer.get_tokens(fragment))) +class BashSessionTest(unittest.TestCase): + + def setUp(self): + self.lexer = BashSessionLexer() + self.maxDiff = None + + def testNeedsName(self): + fragment = u'$ echo \\\nhi\nhi\n' + tokens = [ + (Token.Text, u''), + (Token.Generic.Prompt, u'$'), + (Token.Text, u' '), + (Token.Name.Builtin, u'echo'), + (Token.Text, u' '), + (Token.Literal.String.Escape, u'\\\n'), + (Token.Text, u'hi'), + (Token.Text, u'\n'), + (Token.Generic.Output, u'hi\n'), + ] + self.assertEqual(tokens, list(self.lexer.get_tokens(fragment))) + |