summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeistungsabfall <Leistungsabfall@users.noreply.github.com>2022-04-16 08:36:49 +0200
committerGitHub <noreply@github.com>2022-04-16 08:36:49 +0200
commit29392ea678456a46d741e3f4f6e32b9e58b1c2cd (patch)
treeddd130f493137fe464a7d8875c8b814f6c69be6e
parent5b24b4ed57704955aab39d4d778cd6e8e57e3185 (diff)
downloadpygments-git-29392ea678456a46d741e3f4f6e32b9e58b1c2cd.tar.gz
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>
-rw-r--r--pygments/lexers/_mapping.py1
-rw-r--r--pygments/lexers/configs.py29
-rw-r--r--pygments/lexers/text.py3
-rw-r--r--tests/snippets/unixconfig/etc_group.txt45
-rw-r--r--tests/snippets/unixconfig/etc_passwd.txt86
-rw-r--r--tests/snippets/unixconfig/etc_shadow.txt74
6 files changed, 236 insertions, 2 deletions
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py
index 711ac32a..c23738fd 100644
--- a/pygments/lexers/_mapping.py
+++ b/pygments/lexers/_mapping.py
@@ -504,6 +504,7 @@ LEXERS = {
'UL4Lexer': ('pygments.lexers.ul4', 'UL4', ('ul4',), ('*.ul4',), ()),
'UcodeLexer': ('pygments.lexers.unicon', 'ucode', ('ucode',), ('*.u', '*.u1', '*.u2'), ()),
'UniconLexer': ('pygments.lexers.unicon', 'Unicon', ('unicon',), ('*.icn',), ('text/unicon',)),
+ 'UnixConfigLexer': ('pygments.lexers.configs', 'Unix/Linux config files', ('unixconfig', 'linuxconfig'), (), ()),
'UrbiscriptLexer': ('pygments.lexers.urbi', 'UrbiScript', ('urbiscript',), ('*.u',), ('application/x-urbiscript',)),
'UsdLexer': ('pygments.lexers.usd', 'USD', ('usd', 'usda'), ('*.usd', '*.usda'), ()),
'VBScriptLexer': ('pygments.lexers.basic', 'VBScript', ('vbscript',), ('*.vbs', '*.VBS'), ()),
diff --git a/pygments/lexers/configs.py b/pygments/lexers/configs.py
index 22132401..51aefd77 100644
--- a/pygments/lexers/configs.py
+++ b/pygments/lexers/configs.py
@@ -22,7 +22,7 @@ __all__ = ['IniLexer', 'RegeditLexer', 'PropertiesLexer', 'KconfigLexer',
'NginxConfLexer', 'LighttpdConfLexer', 'DockerLexer',
'TerraformLexer', 'TermcapLexer', 'TerminfoLexer',
'PkgConfigLexer', 'PacmanConfLexer', 'AugeasLexer', 'TOMLLexer',
- 'NestedTextLexer', 'SingularityLexer']
+ 'NestedTextLexer', 'SingularityLexer', 'UnixConfigLexer']
class IniLexer(RegexLexer):
@@ -1127,3 +1127,30 @@ class SingularityLexer(RegexLexer):
result += 0.49
return result
+
+
+class UnixConfigLexer(RegexLexer):
+ """
+ Lexer for Unix/Linux config files using colon-separated values, e.g.
+
+ * ``/etc/group``
+ * ``/etc/passwd``
+ * ``/etc/shadow``
+
+ .. versionadded:: 2.12
+ """
+
+ name = 'Unix/Linux config files'
+ aliases = ['unixconfig', 'linuxconfig']
+ filenames = []
+
+ tokens = {
+ 'root': [
+ (r'^#.*', Comment),
+ (r'\n', Whitespace),
+ (r':', Punctuation),
+ (r'[0-9]+', Number),
+ (r'((?!\n)[a-zA-Z0-9\_\-\s\(\),]){2,}', Text),
+ (r'[^:\n]+', String),
+ ],
+ }
diff --git a/pygments/lexers/text.py b/pygments/lexers/text.py
index 1a78190a..d9bf03dc 100644
--- a/pygments/lexers/text.py
+++ b/pygments/lexers/text.py
@@ -9,7 +9,8 @@
"""
from pygments.lexers.configs import ApacheConfLexer, NginxConfLexer, \
- SquidConfLexer, LighttpdConfLexer, IniLexer, RegeditLexer, PropertiesLexer
+ SquidConfLexer, LighttpdConfLexer, IniLexer, RegeditLexer, PropertiesLexer, \
+ UnixConfigLexer
from pygments.lexers.console import PyPyLogLexer
from pygments.lexers.textedit import VimLexer
from pygments.lexers.markup import BBCodeLexer, MoinWikiLexer, RstLexer, \
diff --git a/tests/snippets/unixconfig/etc_group.txt b/tests/snippets/unixconfig/etc_group.txt
new file mode 100644
index 00000000..3294ed5b
--- /dev/null
+++ b/tests/snippets/unixconfig/etc_group.txt
@@ -0,0 +1,45 @@
+---input---
+root:x:0:
+sudo:x:1:syslog,user
+syslog:x:2:
+#adm:x:3:
+
+user:x:1000
+
+---tokens---
+'root' Text
+':' Punctuation
+'x' Literal.String
+':' Punctuation
+'0' Literal.Number
+':' Punctuation
+'\n' Text.Whitespace
+
+'sudo' Text
+':' Punctuation
+'x' Literal.String
+':' Punctuation
+'1' Literal.Number
+':' Punctuation
+'syslog,user' Text
+'\n' Text.Whitespace
+
+'syslog' Text
+':' Punctuation
+'x' Literal.String
+':' Punctuation
+'2' Literal.Number
+':' Punctuation
+'\n' Text.Whitespace
+
+'#adm:x:3:' Comment
+'\n' Text.Whitespace
+
+'\n' Text.Whitespace
+
+'user' Text
+':' Punctuation
+'x' Literal.String
+':' Punctuation
+'1000' Literal.Number
+'\n' Text.Whitespace
diff --git a/tests/snippets/unixconfig/etc_passwd.txt b/tests/snippets/unixconfig/etc_passwd.txt
new file mode 100644
index 00000000..540e41fc
--- /dev/null
+++ b/tests/snippets/unixconfig/etc_passwd.txt
@@ -0,0 +1,86 @@
+---input---
+root:x:0:0:root:/root:/bin/bash
+daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
+#irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
+gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
+nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
+systemd-network:x:100:102:systemd Network Management,,,:/run/systemd:/usr/sbin/nologin
+
+---tokens---
+'root' Text
+':' Punctuation
+'x' Literal.String
+':' Punctuation
+'0' Literal.Number
+':' Punctuation
+'0' Literal.Number
+':' Punctuation
+'root' Text
+':' Punctuation
+'/root' Literal.String
+':' Punctuation
+'/bin/bash' Literal.String
+'\n' Text.Whitespace
+
+'daemon' Text
+':' Punctuation
+'x' Literal.String
+':' Punctuation
+'1' Literal.Number
+':' Punctuation
+'1' Literal.Number
+':' Punctuation
+'daemon' Text
+':' Punctuation
+'/usr/sbin' Literal.String
+':' Punctuation
+'/usr/sbin/nologin' Literal.String
+'\n' Text.Whitespace
+
+'#irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin' Comment
+'\n' Text.Whitespace
+
+'gnats' Text
+':' Punctuation
+'x' Literal.String
+':' Punctuation
+'41' Literal.Number
+':' Punctuation
+'41' Literal.Number
+':' Punctuation
+'Gnats Bug-Reporting System (admin)' Text
+':' Punctuation
+'/var/lib/gnats' Literal.String
+':' Punctuation
+'/usr/sbin/nologin' Literal.String
+'\n' Text.Whitespace
+
+'nobody' Text
+':' Punctuation
+'x' Literal.String
+':' Punctuation
+'65534' Literal.Number
+':' Punctuation
+'65534' Literal.Number
+':' Punctuation
+'nobody' Text
+':' Punctuation
+'/nonexistent' Literal.String
+':' Punctuation
+'/usr/sbin/nologin' Literal.String
+'\n' Text.Whitespace
+
+'systemd-network' Text
+':' Punctuation
+'x' Literal.String
+':' Punctuation
+'100' Literal.Number
+':' Punctuation
+'102' Literal.Number
+':' Punctuation
+'systemd Network Management,,,' Text
+':' Punctuation
+'/run/systemd' Literal.String
+':' Punctuation
+'/usr/sbin/nologin' Literal.String
+'\n' Text.Whitespace
diff --git a/tests/snippets/unixconfig/etc_shadow.txt b/tests/snippets/unixconfig/etc_shadow.txt
new file mode 100644
index 00000000..6b1d92a0
--- /dev/null
+++ b/tests/snippets/unixconfig/etc_shadow.txt
@@ -0,0 +1,74 @@
+---input---
+root:$6$L95fNbtS$IZ8affe7h2B.DF81HZ:17262:0:14600:14:::
+#nobody:*:18375:0:99999:7:::
+bin:*:17110:0:99999:7:::
+user:$6$KmghZnvbZs7f3SQ9$H6f0M61q5Cf8JLrS0kR3M97/o6GzD6FH3MbLs92CM/l9mHZ7FngBzRfa8D5NrWl.K8nM64affeWrY/L0U7nBt/:19097:0:99999:7:::
+linoadmin:!!:17289:0:99999:7:::
+
+---tokens---
+'root' Text
+':' Punctuation
+'$6$L95fNbtS$IZ8affe7h2B.DF81HZ' Literal.String
+':' Punctuation
+'17262' Literal.Number
+':' Punctuation
+'0' Literal.Number
+':' Punctuation
+'14600' Literal.Number
+':' Punctuation
+'14' Literal.Number
+':' Punctuation
+':' Punctuation
+':' Punctuation
+'\n' Text.Whitespace
+
+'#nobody:*:18375:0:99999:7:::' Comment
+'\n' Text.Whitespace
+
+'bin' Text
+':' Punctuation
+'*' Literal.String
+':' Punctuation
+'17110' Literal.Number
+':' Punctuation
+'0' Literal.Number
+':' Punctuation
+'99999' Literal.Number
+':' Punctuation
+'7' Literal.Number
+':' Punctuation
+':' Punctuation
+':' Punctuation
+'\n' Text.Whitespace
+
+'user' Text
+':' Punctuation
+'$6$KmghZnvbZs7f3SQ9$H6f0M61q5Cf8JLrS0kR3M97/o6GzD6FH3MbLs92CM/l9mHZ7FngBzRfa8D5NrWl.K8nM64affeWrY/L0U7nBt/' Literal.String
+':' Punctuation
+'19097' Literal.Number
+':' Punctuation
+'0' Literal.Number
+':' Punctuation
+'99999' Literal.Number
+':' Punctuation
+'7' Literal.Number
+':' Punctuation
+':' Punctuation
+':' Punctuation
+'\n' Text.Whitespace
+
+'linoadmin' Text
+':' Punctuation
+'!!' Literal.String
+':' Punctuation
+'17289' Literal.Number
+':' Punctuation
+'0' Literal.Number
+':' Punctuation
+'99999' Literal.Number
+':' Punctuation
+'7' Literal.Number
+':' Punctuation
+':' Punctuation
+':' Punctuation
+'\n' Text.Whitespace