4 # Copyright (C) 2012 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Pygments lexer for our custom shell example sessions.
24 The lexer support the following custom markup:
26 - comments: # this is a comment
27 - command lines: '$ ' at the beginning of a line denotes a command
28 - variable input: %input% (works in both commands and screen output)
29 - otherwise, regular text output from commands will be plain
33 from pygments.lexer import RegexLexer, bygroups, include
34 from pygments.token import Name, Text, Generic, Comment
37 class ShellExampleLexer(RegexLexer):
38 name = "ShellExampleLexer"
39 aliases = "shell-example"
46 # switch to state input on '$ ' at the start of the line
47 (r"^\$ ", Text, "input"),
49 (r"[^#%\s\\]+", Text),
55 (r"[^#%\s\\]+", Generic.Strong),
56 (r"\\\n", Generic.Strong),
57 (r"\\", Generic.Strong),
58 # switch to prev state at non-escaped new-line
59 (r"\n", Text, "#pop"),
63 (r"#.*\n", Comment.Single),
66 (r"(\\)(%)", bygroups(None, Text)),
67 (r"(%)([^%]*)(%)", bygroups(None, Name.Variable, None)),
73 app.add_lexer("shell-example", ShellExampleLexer())