Skip to content

Commit 29392ea

Browse files
LeistungsabfalljeanasLeistungsabfall
authored
Add lexer for colon-separated value config files like /etc/passwd, /etc/shadow and /etc/group (#2112)
* add PasswdLexer and ShadowLexer for lexing /etc/passwd and /etc/shadow * fix regex * Update pygments/lexers/configs.py Co-authored-by: Jean Abou-Samra <jean@abou-samra.fr> * address review comments * update _mapping.py * Create united lexer UnixConfigLexer for config files using colon-separated values, typically used in Unix/Linux system config files. * format docstring * UnixConfigLexer: add whitespace detection * add test snippets for UnixConfigLexer * address review comment Co-authored-by: Jean Abou-Samra <jean@abou-samra.fr> Co-authored-by: Leistungsabfall <�Leistungsabfall@users.noreply.github.com>
1 parent 5b24b4e commit 29392ea

6 files changed

Lines changed: 236 additions & 2 deletions

File tree

pygments/lexers/_mapping.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@
504504
'UL4Lexer': ('pygments.lexers.ul4', 'UL4', ('ul4',), ('*.ul4',), ()),
505505
'UcodeLexer': ('pygments.lexers.unicon', 'ucode', ('ucode',), ('*.u', '*.u1', '*.u2'), ()),
506506
'UniconLexer': ('pygments.lexers.unicon', 'Unicon', ('unicon',), ('*.icn',), ('text/unicon',)),
507+
'UnixConfigLexer': ('pygments.lexers.configs', 'Unix/Linux config files', ('unixconfig', 'linuxconfig'), (), ()),
507508
'UrbiscriptLexer': ('pygments.lexers.urbi', 'UrbiScript', ('urbiscript',), ('*.u',), ('application/x-urbiscript',)),
508509
'UsdLexer': ('pygments.lexers.usd', 'USD', ('usd', 'usda'), ('*.usd', '*.usda'), ()),
509510
'VBScriptLexer': ('pygments.lexers.basic', 'VBScript', ('vbscript',), ('*.vbs', '*.VBS'), ()),

pygments/lexers/configs.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
'NginxConfLexer', 'LighttpdConfLexer', 'DockerLexer',
2323
'TerraformLexer', 'TermcapLexer', 'TerminfoLexer',
2424
'PkgConfigLexer', 'PacmanConfLexer', 'AugeasLexer', 'TOMLLexer',
25-
'NestedTextLexer', 'SingularityLexer']
25+
'NestedTextLexer', 'SingularityLexer', 'UnixConfigLexer']
2626

2727

2828
class IniLexer(RegexLexer):
@@ -1127,3 +1127,30 @@ def analyse_text(text):
11271127
result += 0.49
11281128

11291129
return result
1130+
1131+
1132+
class UnixConfigLexer(RegexLexer):
1133+
"""
1134+
Lexer for Unix/Linux config files using colon-separated values, e.g.
1135+
1136+
* ``/etc/group``
1137+
* ``/etc/passwd``
1138+
* ``/etc/shadow``
1139+
1140+
.. versionadded:: 2.12
1141+
"""
1142+
1143+
name = 'Unix/Linux config files'
1144+
aliases = ['unixconfig', 'linuxconfig']
1145+
filenames = []
1146+
1147+
tokens = {
1148+
'root': [
1149+
(r'^#.*', Comment),
1150+
(r'\n', Whitespace),
1151+
(r':', Punctuation),
1152+
(r'[0-9]+', Number),
1153+
(r'((?!\n)[a-zA-Z0-9\_\-\s\(\),]){2,}', Text),
1154+
(r'[^:\n]+', String),
1155+
],
1156+
}

pygments/lexers/text.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"""
1010

1111
from pygments.lexers.configs import ApacheConfLexer, NginxConfLexer, \
12-
SquidConfLexer, LighttpdConfLexer, IniLexer, RegeditLexer, PropertiesLexer
12+
SquidConfLexer, LighttpdConfLexer, IniLexer, RegeditLexer, PropertiesLexer, \
13+
UnixConfigLexer
1314
from pygments.lexers.console import PyPyLogLexer
1415
from pygments.lexers.textedit import VimLexer
1516
from pygments.lexers.markup import BBCodeLexer, MoinWikiLexer, RstLexer, \
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---input---
2+
root:x:0:
3+
sudo:x:1:syslog,user
4+
syslog:x:2:
5+
#adm:x:3:
6+
7+
user:x:1000
8+
9+
---tokens---
10+
'root' Text
11+
':' Punctuation
12+
'x' Literal.String
13+
':' Punctuation
14+
'0' Literal.Number
15+
':' Punctuation
16+
'\n' Text.Whitespace
17+
18+
'sudo' Text
19+
':' Punctuation
20+
'x' Literal.String
21+
':' Punctuation
22+
'1' Literal.Number
23+
':' Punctuation
24+
'syslog,user' Text
25+
'\n' Text.Whitespace
26+
27+
'syslog' Text
28+
':' Punctuation
29+
'x' Literal.String
30+
':' Punctuation
31+
'2' Literal.Number
32+
':' Punctuation
33+
'\n' Text.Whitespace
34+
35+
'#adm:x:3:' Comment
36+
'\n' Text.Whitespace
37+
38+
'\n' Text.Whitespace
39+
40+
'user' Text
41+
':' Punctuation
42+
'x' Literal.String
43+
':' Punctuation
44+
'1000' Literal.Number
45+
'\n' Text.Whitespace
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---input---
2+
root:x:0:0:root:/root:/bin/bash
3+
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
4+
#irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
5+
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
6+
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
7+
systemd-network:x:100:102:systemd Network Management,,,:/run/systemd:/usr/sbin/nologin
8+
9+
---tokens---
10+
'root' Text
11+
':' Punctuation
12+
'x' Literal.String
13+
':' Punctuation
14+
'0' Literal.Number
15+
':' Punctuation
16+
'0' Literal.Number
17+
':' Punctuation
18+
'root' Text
19+
':' Punctuation
20+
'/root' Literal.String
21+
':' Punctuation
22+
'/bin/bash' Literal.String
23+
'\n' Text.Whitespace
24+
25+
'daemon' Text
26+
':' Punctuation
27+
'x' Literal.String
28+
':' Punctuation
29+
'1' Literal.Number
30+
':' Punctuation
31+
'1' Literal.Number
32+
':' Punctuation
33+
'daemon' Text
34+
':' Punctuation
35+
'/usr/sbin' Literal.String
36+
':' Punctuation
37+
'/usr/sbin/nologin' Literal.String
38+
'\n' Text.Whitespace
39+
40+
'#irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin' Comment
41+
'\n' Text.Whitespace
42+
43+
'gnats' Text
44+
':' Punctuation
45+
'x' Literal.String
46+
':' Punctuation
47+
'41' Literal.Number
48+
':' Punctuation
49+
'41' Literal.Number
50+
':' Punctuation
51+
'Gnats Bug-Reporting System (admin)' Text
52+
':' Punctuation
53+
'/var/lib/gnats' Literal.String
54+
':' Punctuation
55+
'/usr/sbin/nologin' Literal.String
56+
'\n' Text.Whitespace
57+
58+
'nobody' Text
59+
':' Punctuation
60+
'x' Literal.String
61+
':' Punctuation
62+
'65534' Literal.Number
63+
':' Punctuation
64+
'65534' Literal.Number
65+
':' Punctuation
66+
'nobody' Text
67+
':' Punctuation
68+
'/nonexistent' Literal.String
69+
':' Punctuation
70+
'/usr/sbin/nologin' Literal.String
71+
'\n' Text.Whitespace
72+
73+
'systemd-network' Text
74+
':' Punctuation
75+
'x' Literal.String
76+
':' Punctuation
77+
'100' Literal.Number
78+
':' Punctuation
79+
'102' Literal.Number
80+
':' Punctuation
81+
'systemd Network Management,,,' Text
82+
':' Punctuation
83+
'/run/systemd' Literal.String
84+
':' Punctuation
85+
'/usr/sbin/nologin' Literal.String
86+
'\n' Text.Whitespace
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---input---
2+
root:$6$L95fNbtS$IZ8affe7h2B.DF81HZ:17262:0:14600:14:::
3+
#nobody:*:18375:0:99999:7:::
4+
bin:*:17110:0:99999:7:::
5+
user:$6$KmghZnvbZs7f3SQ9$H6f0M61q5Cf8JLrS0kR3M97/o6GzD6FH3MbLs92CM/l9mHZ7FngBzRfa8D5NrWl.K8nM64affeWrY/L0U7nBt/:19097:0:99999:7:::
6+
linoadmin:!!:17289:0:99999:7:::
7+
8+
---tokens---
9+
'root' Text
10+
':' Punctuation
11+
'$6$L95fNbtS$IZ8affe7h2B.DF81HZ' Literal.String
12+
':' Punctuation
13+
'17262' Literal.Number
14+
':' Punctuation
15+
'0' Literal.Number
16+
':' Punctuation
17+
'14600' Literal.Number
18+
':' Punctuation
19+
'14' Literal.Number
20+
':' Punctuation
21+
':' Punctuation
22+
':' Punctuation
23+
'\n' Text.Whitespace
24+
25+
'#nobody:*:18375:0:99999:7:::' Comment
26+
'\n' Text.Whitespace
27+
28+
'bin' Text
29+
':' Punctuation
30+
'*' Literal.String
31+
':' Punctuation
32+
'17110' Literal.Number
33+
':' Punctuation
34+
'0' Literal.Number
35+
':' Punctuation
36+
'99999' Literal.Number
37+
':' Punctuation
38+
'7' Literal.Number
39+
':' Punctuation
40+
':' Punctuation
41+
':' Punctuation
42+
'\n' Text.Whitespace
43+
44+
'user' Text
45+
':' Punctuation
46+
'$6$KmghZnvbZs7f3SQ9$H6f0M61q5Cf8JLrS0kR3M97/o6GzD6FH3MbLs92CM/l9mHZ7FngBzRfa8D5NrWl.K8nM64affeWrY/L0U7nBt/' Literal.String
47+
':' Punctuation
48+
'19097' Literal.Number
49+
':' Punctuation
50+
'0' Literal.Number
51+
':' Punctuation
52+
'99999' Literal.Number
53+
':' Punctuation
54+
'7' Literal.Number
55+
':' Punctuation
56+
':' Punctuation
57+
':' Punctuation
58+
'\n' Text.Whitespace
59+
60+
'linoadmin' Text
61+
':' Punctuation
62+
'!!' Literal.String
63+
':' Punctuation
64+
'17289' Literal.Number
65+
':' Punctuation
66+
'0' Literal.Number
67+
':' Punctuation
68+
'99999' Literal.Number
69+
':' Punctuation
70+
'7' Literal.Number
71+
':' Punctuation
72+
':' Punctuation
73+
':' Punctuation
74+
'\n' Text.Whitespace

0 commit comments

Comments
 (0)