forked from donnemartin/gitsome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyghooks.py
67 lines (50 loc) · 2.09 KB
/
pyghooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Hooks for pygments syntax highlighting."""
from pygments.lexer import inherit, bygroups, using, this
from pygments.token import Name, Generic, Keyword, Text, String
from pygments.lexers.shell import BashLexer
from pygments.lexers.agile import PythonLexer
class XonshSubprocLexer(BashLexer):
"""Lexer for xonsh subproc mode."""
name = 'Xonsh subprocess lexer'
tokens = {'root': [(r'`[^`]*?`', String.Backtick), inherit, ]}
ROOT_TOKENS = [(r'\?', Keyword),
(r'\$\w+', Name.Variable),
(r'\$\{', Keyword, ('pymode', )),
(r'\$\(', Keyword, ('subproc', )),
(r'\$\[', Keyword, ('subproc', )),
(r'@\(', Keyword, ('pymode', )),
inherit, ]
PYMODE_TOKENS = [(r'(.+)(\))', bygroups(using(this), Keyword), '#pop'),
(r'(.+)(\})', bygroups(using(this), Keyword), '#pop'), ]
SUBPROC_TOKENS = [
(r'(.+)(\))', bygroups(using(XonshSubprocLexer), Keyword), '#pop'),
(r'(.+)(\])', bygroups(using(XonshSubprocLexer), Keyword), '#pop'),
]
class XonshLexer(PythonLexer):
"""Xonsh console lexer for pygments."""
name = 'Xonsh lexer'
aliases = ['xonsh', 'xsh']
filenames = ['*.xsh', '*xonshrc']
tokens = {
'root': list(ROOT_TOKENS),
'pymode': PYMODE_TOKENS,
'subproc': SUBPROC_TOKENS,
}
class XonshConsoleLexer(PythonLexer):
"""Xonsh console lexer for pygments."""
name = 'Xonsh console lexer'
aliases = ['xonshcon']
filenames = []
tokens = {
'root': [(r'^(>>>|\.\.\.) ', Generic.Prompt),
(r'\n(>>>|\.\.\.)', Generic.Prompt),
(r'\n(?![>.][>.][>.] )([^\n]*)', Generic.Output),
(r'\n(?![>.][>.][>.] )(.*?)$', Generic.Output)] + ROOT_TOKENS,
'pymode': PYMODE_TOKENS,
'subproc': SUBPROC_TOKENS,
}
# XonshLexer & XonshSubprocLexer have to refernce each other
XonshSubprocLexer.tokens['root'] = [
(r'(\$\{)(.*)(\})', bygroups(Keyword, using(XonshLexer), Keyword)),
(r'(@\()(.+)(\))', bygroups(Keyword, using(XonshLexer), Keyword)),
] + XonshSubprocLexer.tokens['root']