Skip to content

Commit 309c28f

Browse files
committed
Added inline test using Terminal256Formatter
1 parent 033de6d commit 309c28f

File tree

1 file changed

+105
-57
lines changed

1 file changed

+105
-57
lines changed

csvlexer/csv.py

100644100755
Lines changed: 105 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,105 @@
1-
# @Author: SashaChernykh
2-
# @Editor: fish2000
3-
# @Date: 2018-06-04 08:01:23
4-
# @Last Modified time: 2019-03-11 14:39:28 +0500
5-
""" Pygments CSV Lexer csvlexer/csv.py
6-
7-
* http://pygments.org/docs/lexerdevelopment/
8-
* https://github.com/FSund/pygments-custom-cpplexer
9-
"""
10-
from __future__ import print_function
11-
12-
from pygments.lexer import RegexLexer, bygroups
13-
from pygments.token import Keyword, Literal, Name, Operator, Punctuation
14-
15-
class CsvLexer(RegexLexer):
16-
17-
""" Simple CSV lexer for Pygments.
18-
19-
Extends:
20-
pygments.lexer.RegexLexer
21-
22-
Class Variables:
23-
name {str} -- name of lexer:
24-
* http://pygments.org/docs/api/#pygments.lexer.Lexer.name
25-
aliases {list} – languages, against whose GFM block names CsvLexer will apply
26-
* https://git.io/fhjla
27-
filenames {list} – file name patterns, for whose contents CsvLexer will apply
28-
tokens {dict} – regular expressions internally matching CsvLexer’s components
29-
30-
Based on StackOverflow user Adobe’s code:
31-
* https://stackoverflow.com/a/25508711/298171
32-
"""
33-
34-
name = 'Csv'
35-
aliases = ['csv', 'comma-separated', 'comma-separated-values']
36-
filenames = ['*.csv']
37-
38-
tokens = {
39-
'root': [
40-
(r'^[^,\n]*', Operator, 'second'),
41-
],
42-
'second': [
43-
(r'(,)([^,\n]*)', bygroups(Punctuation, Literal.Number), 'third'),
44-
],
45-
'third': [
46-
(r'(,)([^,\n]*)', bygroups(Punctuation, Keyword), 'fourth'),
47-
],
48-
'fourth': [
49-
(r'(,)([^,\n]*)', bygroups(Punctuation, Name.Constant), 'fifth'),
50-
],
51-
'fifth': [
52-
(r'(,)([^,\n]*)', bygroups(Punctuation, Literal.String.Single), 'unsupported'),
53-
],
54-
'unsupported': [
55-
(r'.+', Punctuation),
56-
],
57-
}
1+
#!/usr/bin/env python
2+
# @Author: SashaChernykh
3+
# @Editor: fish2000
4+
# @Date: 2018-06-04 08:01:23
5+
# @Last Modified time: 2019-03-11 14:39:28 +0500
6+
""" Pygments CSV Lexer csvlexer/csv.py
7+
8+
* http://pygments.org/docs/lexerdevelopment/
9+
* https://github.com/FSund/pygments-custom-cpplexer
10+
"""
11+
from __future__ import print_function
12+
13+
from pygments.lexer import RegexLexer, bygroups
14+
from pygments.token import Keyword, Literal, Name, Operator, Punctuation
15+
16+
class CsvLexer(RegexLexer):
17+
18+
""" Simple CSV lexer for Pygments.
19+
20+
Extends:
21+
pygments.lexer.RegexLexer
22+
23+
Class Variables:
24+
name {str} -- name of lexer:
25+
* http://pygments.org/docs/api/#pygments.lexer.Lexer.name
26+
aliases {list} – languages, against whose GFM block names CsvLexer will apply
27+
* https://git.io/fhjla
28+
filenames {list} – file name patterns, for whose contents CsvLexer will apply
29+
tokens {dict} – regular expressions internally matching CsvLexer’s components
30+
31+
Based on StackOverflow user Adobe’s code:
32+
* https://stackoverflow.com/a/25508711/298171
33+
"""
34+
35+
name = 'Csv'
36+
aliases = ['csv', 'comma-separated', 'comma-separated-values']
37+
filenames = ['*.csv']
38+
39+
tokens = {
40+
'root': [
41+
(r'^[^,\n]*', Operator, 'second'),
42+
],
43+
'second': [
44+
(r'(,)([^,\n]*)', bygroups(Punctuation, Literal.Number), 'third'),
45+
],
46+
'third': [
47+
(r'(,)([^,\n]*)', bygroups(Punctuation, Keyword), 'fourth'),
48+
],
49+
'fourth': [
50+
(r'(,)([^,\n]*)', bygroups(Punctuation, Name.Constant), 'fifth'),
51+
],
52+
'fifth': [
53+
(r'(,)([^,\n]*)', bygroups(Punctuation, Literal.String.Single), 'sixth'),
54+
],
55+
'sixth': [
56+
(r'(,)([^,\n]*)', bygroups(Punctuation, Literal.Number), 'seventh'),
57+
],
58+
'seventh': [
59+
(r'(,)([^,\n]*)', bygroups(Punctuation, Keyword), 'eighth'),
60+
],
61+
'eighth': [
62+
(r'(,)([^,\n]*)', bygroups(Punctuation, Name.Constant), 'ninth'),
63+
],
64+
'ninth': [
65+
(r'(,)([^,\n]*)', bygroups(Punctuation, Literal.String.Single), 'unsupported'),
66+
],
67+
'unsupported': [
68+
(r'(.+)', bygroups(Punctuation)),
69+
],
70+
}
71+
72+
sample_csv_material = """
73+
trailer,125
74+
header,125,11,session start,0,Wed Mar 6 11:04:43 2019, + 414 msec
75+
argument,1,0x0,sflags
76+
argument,2,0x0,am_success
77+
argument,3,0x0,am_failure
78+
subject,-1,root,wheel,root,wheel,0,100339,0,0.0.0.0
79+
return,success,0
80+
trailer,125
81+
header,125,11,session start,0,Wed Mar 6 18:23:18 2019, + 301 msec
82+
argument,1,0x0,sflags
83+
argument,2,0x0,am_success
84+
argument,3,0x0,am_failure
85+
subject,-1,root,wheel,root,wheel,0,100340,0,0.0.0.0
86+
return,success,0
87+
trailer,125
88+
header,153,11,user authentication,0,Wed Mar 6 18:24:36 2019, + 169 msec
89+
subject,-1,root,wheel,root,wheel,290,100000,0,0.0.0.0
90+
text,Touch ID authentication
91+
return,success,0
92+
"""
93+
94+
def test():
95+
from pygments.formatters import Terminal256Formatter
96+
from pygments import highlight
97+
98+
print(highlight(sample_csv_material, CsvLexer(), Terminal256Formatter()))
99+
100+
if __name__ == '__main__':
101+
import os, sys
102+
if os.environ.get('TM_PYTHON'):
103+
print("Inline test won’t work in TextMate, aborting…", file=sys.stderr)
104+
sys.exit(1)
105+
test()

0 commit comments

Comments
 (0)