Statistics
| Branch: | Tag: | Revision:

root / lib / build / shell_example_lexer.py @ 7142485a

History | View | Annotate | Download (2 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
      ],
51
    "input": [
52
      include("comments"),
53
      include("userinput"),
54
      (r"[^%\\\s]+", Generic.Strong),
55
      (r"\\\n", Generic.Strong),
56
      (r"\\", Generic.Strong),
57
      # switch to prev state at non-escaped new-line
58
      (r"\n", Text, "#pop"),
59
      (r"\s+", Text),
60
      ],
61
    "comments": [
62
      (r"#.*\n", Comment.Single),
63
      ],
64
    "userinput": [
65
      (r"\\%", Text),
66
      (r"(%)([^%]*)(%)", bygroups(None, Name.Variable, None)),
67
      ],
68
    }
69

    
70

    
71
def setup(app):
72
  app.add_lexer("shell-example", ShellExampleLexer())