root / lib / build / shell_example_lexer.py @ 63b4bb1e
History | View | Annotate | Download (2.1 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2012 Google Inc.
|
5 |
#
|
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.
|
10 |
#
|
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.
|
15 |
#
|
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
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Pygments lexer for our custom shell example sessions.
|
23 |
|
24 |
The lexer support the following custom markup:
|
25 |
|
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
|
30 |
|
31 |
"""
|
32 |
|
33 |
from pygments.lexer import RegexLexer, bygroups, include |
34 |
from pygments.token import Name, Text, Generic, Comment |
35 |
|
36 |
|
37 |
class ShellExampleLexer(RegexLexer): |
38 |
name = "ShellExampleLexer"
|
39 |
aliases = "shell-example"
|
40 |
filenames = [] |
41 |
|
42 |
tokens = { |
43 |
"root": [
|
44 |
include("comments"),
|
45 |
include("userinput"),
|
46 |
# switch to state input on '$ ' at the start of the line
|
47 |
(r"^\$ ", Text, "input"), |
48 |
(r"\s+", Text),
|
49 |
(r"[^#%\s\\]+", Text),
|
50 |
(r"\\", Text),
|
51 |
], |
52 |
"input": [
|
53 |
include("comments"),
|
54 |
include("userinput"),
|
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"), |
60 |
(r"\s+", Text),
|
61 |
], |
62 |
"comments": [
|
63 |
(r"#.*\n", Comment.Single),
|
64 |
], |
65 |
"userinput": [
|
66 |
(r"(\\)(%)", bygroups(None, Text)), |
67 |
(r"(%)([^%]*)(%)", bygroups(None, Name.Variable, None)), |
68 |
], |
69 |
} |
70 |
|
71 |
|
72 |
def setup(app): |
73 |
app.add_lexer("shell-example", ShellExampleLexer())
|