root / lib / build / shell_example_lexer.py @ 0fe20731
History | View | Annotate | Download (2.1 kB)
1 | 7142485a | Iustin Pop | #
|
---|---|---|---|
2 | 7142485a | Iustin Pop | #
|
3 | 7142485a | Iustin Pop | |
4 | 7142485a | Iustin Pop | # Copyright (C) 2012 Google Inc.
|
5 | 7142485a | Iustin Pop | #
|
6 | 7142485a | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | 7142485a | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | 7142485a | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | 7142485a | Iustin Pop | # (at your option) any later version.
|
10 | 7142485a | Iustin Pop | #
|
11 | 7142485a | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | 7142485a | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 7142485a | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 7142485a | Iustin Pop | # General Public License for more details.
|
15 | 7142485a | Iustin Pop | #
|
16 | 7142485a | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | 7142485a | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | 7142485a | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 7142485a | Iustin Pop | # 02110-1301, USA.
|
20 | 7142485a | Iustin Pop | |
21 | 7142485a | Iustin Pop | |
22 | 7142485a | Iustin Pop | """Pygments lexer for our custom shell example sessions.
|
23 | 7142485a | Iustin Pop |
|
24 | 7142485a | Iustin Pop | The lexer support the following custom markup:
|
25 | 7142485a | Iustin Pop |
|
26 | 7142485a | Iustin Pop | - comments: # this is a comment
|
27 | 7142485a | Iustin Pop | - command lines: '$ ' at the beginning of a line denotes a command
|
28 | 7142485a | Iustin Pop | - variable input: %input% (works in both commands and screen output)
|
29 | 7142485a | Iustin Pop | - otherwise, regular text output from commands will be plain
|
30 | 7142485a | Iustin Pop |
|
31 | 7142485a | Iustin Pop | """
|
32 | 7142485a | Iustin Pop | |
33 | 7142485a | Iustin Pop | from pygments.lexer import RegexLexer, bygroups, include |
34 | 7142485a | Iustin Pop | from pygments.token import Name, Text, Generic, Comment |
35 | 7142485a | Iustin Pop | |
36 | 7142485a | Iustin Pop | |
37 | 7142485a | Iustin Pop | class ShellExampleLexer(RegexLexer): |
38 | 7142485a | Iustin Pop | name = "ShellExampleLexer"
|
39 | 7142485a | Iustin Pop | aliases = "shell-example"
|
40 | 7142485a | Iustin Pop | filenames = [] |
41 | 7142485a | Iustin Pop | |
42 | 7142485a | Iustin Pop | tokens = { |
43 | 7142485a | Iustin Pop | "root": [
|
44 | 7142485a | Iustin Pop | include("comments"),
|
45 | 7142485a | Iustin Pop | include("userinput"),
|
46 | 7142485a | Iustin Pop | # switch to state input on '$ ' at the start of the line
|
47 | 7142485a | Iustin Pop | (r"^\$ ", Text, "input"), |
48 | 7142485a | Iustin Pop | (r"\s+", Text),
|
49 | 63b4bb1e | Iustin Pop | (r"[^#%\s\\]+", Text),
|
50 | 63b4bb1e | Iustin Pop | (r"\\", Text),
|
51 | 7142485a | Iustin Pop | ], |
52 | 7142485a | Iustin Pop | "input": [
|
53 | 7142485a | Iustin Pop | include("comments"),
|
54 | 7142485a | Iustin Pop | include("userinput"),
|
55 | 63b4bb1e | Iustin Pop | (r"[^#%\s\\]+", Generic.Strong),
|
56 | 7142485a | Iustin Pop | (r"\\\n", Generic.Strong),
|
57 | 7142485a | Iustin Pop | (r"\\", Generic.Strong),
|
58 | 7142485a | Iustin Pop | # switch to prev state at non-escaped new-line
|
59 | 7142485a | Iustin Pop | (r"\n", Text, "#pop"), |
60 | 7142485a | Iustin Pop | (r"\s+", Text),
|
61 | 7142485a | Iustin Pop | ], |
62 | 7142485a | Iustin Pop | "comments": [
|
63 | 7142485a | Iustin Pop | (r"#.*\n", Comment.Single),
|
64 | 7142485a | Iustin Pop | ], |
65 | 7142485a | Iustin Pop | "userinput": [
|
66 | 63b4bb1e | Iustin Pop | (r"(\\)(%)", bygroups(None, Text)), |
67 | 7142485a | Iustin Pop | (r"(%)([^%]*)(%)", bygroups(None, Name.Variable, None)), |
68 | 7142485a | Iustin Pop | ], |
69 | 7142485a | Iustin Pop | } |
70 | 7142485a | Iustin Pop | |
71 | 7142485a | Iustin Pop | |
72 | 7142485a | Iustin Pop | def setup(app): |
73 | 7142485a | Iustin Pop | app.add_lexer("shell-example", ShellExampleLexer()) |