diff options
| author | hhsprings <xwhhsprings@gmail.com> | 2015-06-19 21:10:21 +0900 |
|---|---|---|
| committer | hhsprings <xwhhsprings@gmail.com> | 2015-06-19 21:10:21 +0900 |
| commit | e05a882edeaa85de4f07e643bcf03c39ee6b18b6 (patch) | |
| tree | 98b2cd9282c8bc9a079d9fd2486439def0a19b0e /pygments | |
| parent | c8c042e3f06e4caea4f0e07318343dbc2eba8176 (diff) | |
| download | pygments-e05a882edeaa85de4f07e643bcf03c39ee6b18b6.tar.gz | |
add MSDOSSessionLexer and PowerShellSessionLexer.
Diffstat (limited to 'pygments')
| -rw-r--r-- | pygments/lexers/_mapping.py | 2 | ||||
| -rw-r--r-- | pygments/lexers/shell.py | 103 |
2 files changed, 104 insertions, 1 deletions
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py index 2b836ac6..9692051a 100644 --- a/pygments/lexers/_mapping.py +++ b/pygments/lexers/_mapping.py @@ -207,6 +207,7 @@ LEXERS = { 'LogtalkLexer': ('pygments.lexers.prolog', 'Logtalk', ('logtalk',), ('*.lgt', '*.logtalk'), ('text/x-logtalk',)), 'LuaLexer': ('pygments.lexers.scripting', 'Lua', ('lua',), ('*.lua', '*.wlua'), ('text/x-lua', 'application/x-lua')), 'MOOCodeLexer': ('pygments.lexers.scripting', 'MOOCode', ('moocode', 'moo'), ('*.moo',), ('text/x-moocode',)), + 'MSDOSSessionLexer': ('pygments.lexers.shell', 'MSDOS Session', ('doscon',), (), (),), 'MakefileLexer': ('pygments.lexers.make', 'Makefile', ('make', 'makefile', 'mf', 'bsdmake'), ('*.mak', '*.mk', 'Makefile', 'makefile', 'Makefile.*', 'GNUmakefile'), ('text/x-makefile',)), 'MakoCssLexer': ('pygments.lexers.templates', 'CSS+Mako', ('css+mako',), (), ('text/css+mako',)), 'MakoHtmlLexer': ('pygments.lexers.templates', 'HTML+Mako', ('html+mako',), (), ('text/html+mako',)), @@ -274,6 +275,7 @@ LEXERS = { 'PostgresLexer': ('pygments.lexers.sql', 'PostgreSQL SQL dialect', ('postgresql', 'postgres'), (), ('text/x-postgresql',)), 'PovrayLexer': ('pygments.lexers.graphics', 'POVRay', ('pov',), ('*.pov', '*.inc'), ('text/x-povray',)), 'PowerShellLexer': ('pygments.lexers.shell', 'PowerShell', ('powershell', 'posh', 'ps1', 'psm1'), ('*.ps1', '*.psm1'), ('text/x-powershell',)), + 'PowerShellSessionLexer': ('pygments.lexers.shell', 'PowerShell Session', ('ps1con',), (), (),), 'PrologLexer': ('pygments.lexers.prolog', 'Prolog', ('prolog',), ('*.ecl', '*.prolog', '*.pro', '*.pl'), ('text/x-prolog',)), 'PropertiesLexer': ('pygments.lexers.configs', 'Properties', ('properties', 'jproperties'), ('*.properties',), ('text/x-java-properties',)), 'ProtoBufLexer': ('pygments.lexers.dsls', 'Protocol Buffer', ('protobuf', 'proto'), ('*.proto',), ()), diff --git a/pygments/lexers/shell.py b/pygments/lexers/shell.py index a9c1e6b9..dbab390d 100644 --- a/pygments/lexers/shell.py +++ b/pygments/lexers/shell.py @@ -18,7 +18,8 @@ from pygments.util import shebang_matches __all__ = ['BashLexer', 'BashSessionLexer', 'TcshLexer', 'BatchLexer', - 'PowerShellLexer', 'ShellSessionLexer'] + 'MSDOSSessionLexer', + 'PowerShellLexer', 'ShellSessionLexer', 'PowerShellSessionLexer'] line_re = re.compile('.*?\n') @@ -273,6 +274,56 @@ class BatchLexer(RegexLexer): } +class MSDOSSessionLexer(Lexer): + """ + Lexer for simplistic MSDOS sessions. + + .. versionadded:: 2.0 + """ + + name = 'MSDOS Session' + aliases = ['doscon'] + filenames = [] + mimetypes = [] + + def get_tokens_unprocessed(self, text): + bashlexer = BatchLexer(**self.options) + + pos = 0 + curcode = '' + insertions = [] + + for match in line_re.finditer(text): + line = match.group() + m = re.match(r'^([^>]+>)(.*\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('More? '): + insertions.append((len(curcode), + [(0, Generic.Prompt, line[:len('More? ')])])) + curcode += line[len('More? '):] + else: + if insertions: + toks = bashlexer.get_tokens_unprocessed(curcode) + for i, t, v in do_insertions(insertions, toks): + 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 TcshLexer(RegexLexer): """ Lexer for tcsh scripts. @@ -436,3 +487,53 @@ class PowerShellLexer(RegexLexer): (r".", String.Heredoc), ] } + + +class PowerShellSessionLexer(Lexer): + """ + Lexer for simplistic Windows PowerShell sessions. + + .. versionadded:: 2.0 + """ + + name = 'PowerShell Session' + aliases = ['ps1con'] + filenames = [] + mimetypes = [] + + def get_tokens_unprocessed(self, text): + bashlexer = PowerShellLexer(**self.options) + + pos = 0 + curcode = '' + insertions = [] + + for match in line_re.finditer(text): + line = match.group() + m = re.match(r'^(PS [^>]+> )(.*\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[:len('>> ')])])) + curcode += line[len('>> '):] + else: + if insertions: + toks = bashlexer.get_tokens_unprocessed(curcode) + for i, t, v in do_insertions(insertions, toks): + 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 |
