summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthäus G. Chajdas <dev@anteru.net>2021-08-08 16:31:49 +0200
committerMatthäus G. Chajdas <dev@anteru.net>2021-08-08 16:31:49 +0200
commit5f1373b09514f0e3d8be61b78c629068e35acb0d (patch)
tree5f7c5189d08f5b77464f8a82626eb11900d403a6
parent698007e1f374b3603ce488685aaf120fa13382ff (diff)
parentcd71718647bce022aa8b3fb3f258032d2959edc3 (diff)
downloadpygments-git-5f1373b09514f0e3d8be61b78c629068e35acb0d.tar.gz
Merge branch 'scop-feat/ascii-armored'
-rw-r--r--AUTHORS1
-rw-r--r--CHANGES1
-rw-r--r--pygments/lexers/_mapping.py1
-rw-r--r--pygments/lexers/asc.py51
-rw-r--r--tests/examplefiles/asc/id_ecdsa9
-rw-r--r--tests/examplefiles/asc/id_ecdsa.output29
6 files changed, 92 insertions, 0 deletions
diff --git a/AUTHORS b/AUTHORS
index 743ac6ed..9fc03933 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -203,6 +203,7 @@ Other contributors, listed alphabetically, are:
* Robert Simmons -- Standard ML lexer
* Kirill Simonov -- YAML lexer
* Corbin Simpson -- Monte lexer
+* Ville Skyttä -- ASCII armored lexer
* Alexander Smishlajev -- Visual FoxPro lexer
* Steve Spigarelli -- XQuery lexer
* Jerome St-Louis -- eC lexer
diff --git a/CHANGES b/CHANGES
index 46965406..1672237a 100644
--- a/CHANGES
+++ b/CHANGES
@@ -13,6 +13,7 @@ Version 2.10.0
- Added lexers:
+ * ASC armored files (#1807)
* GSQL (#1809, #1866)
* Javascript REPL (#1825)
* procfile (#1808)
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py
index 4dc2615c..0e906c1d 100644
--- a/pygments/lexers/_mapping.py
+++ b/pygments/lexers/_mapping.py
@@ -41,6 +41,7 @@ LEXERS = {
'AppleScriptLexer': ('pygments.lexers.scripting', 'AppleScript', ('applescript',), ('*.applescript',), ()),
'ArduinoLexer': ('pygments.lexers.c_like', 'Arduino', ('arduino',), ('*.ino',), ('text/x-arduino',)),
'ArrowLexer': ('pygments.lexers.arrow', 'Arrow', ('arrow',), ('*.arw',), ()),
+ 'AscLexer': ('pygments.lexers.asc', 'ASCII armored', ('asc', 'pem'), ('*.asc', '*.pem', 'id_dsa', 'id_ecdsa', 'id_ecdsa_sk', 'id_ed25519', 'id_ed25519_sk', 'id_rsa'), ('application/pgp-keys', 'application/pgp-encrypted', 'application/pgp-signature')),
'AspectJLexer': ('pygments.lexers.jvm', 'AspectJ', ('aspectj',), ('*.aj',), ('text/x-aspectj',)),
'AsymptoteLexer': ('pygments.lexers.graphics', 'Asymptote', ('asymptote', 'asy'), ('*.asy',), ('text/x-asymptote',)),
'AugeasLexer': ('pygments.lexers.configs', 'Augeas', ('augeas',), ('*.aug',), ()),
diff --git a/pygments/lexers/asc.py b/pygments/lexers/asc.py
new file mode 100644
index 00000000..f775fd07
--- /dev/null
+++ b/pygments/lexers/asc.py
@@ -0,0 +1,51 @@
+"""
+ pygments.lexers.asc
+ ~~~~~~~~~~~~~~~~~~~
+
+ Lexer for various ASCII armored files.
+
+ :copyright: Copyright 2021 by the Pygments team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+import re
+
+from pygments.lexer import RegexLexer, bygroups
+from pygments.token import Comment, Generic, Name, Operator, String, Whitespace
+
+__all__ = ['AscLexer']
+
+
+class AscLexer(RegexLexer):
+ """
+ Lexer for ASCII armored files, containing `-----BEGIN/END ...-----` wrapped base64 data.
+
+ .. versionadded:: 2.10
+ """
+ name = 'ASCII armored'
+ aliases = ['asc', 'pem']
+ filenames = [
+ '*.asc', # PGP; *.gpg, *.pgp, and *.sig too, but those can be binary
+ '*.pem', # X.509; *.cer, *.crt, *.csr, and key etc too, but those can be binary
+ 'id_dsa', 'id_ecdsa', 'id_ecdsa_sk', 'id_ed25519', 'id_ed25519_sk', 'id_rsa', # SSH private keys
+ ]
+ mimetypes = ['application/pgp-keys', 'application/pgp-encrypted', 'application/pgp-signature']
+
+ flags = re.MULTILINE
+
+ tokens = {
+ 'root': [
+ (r'\s+', Whitespace),
+ (r'^-----BEGIN [^\n]+-----$', Generic.Heading, 'data'),
+ (r'\S+', Comment),
+ ],
+ 'data': [
+ (r'\s+', Whitespace),
+ (r'^([^:]+)(:)([ \t]+)(.*)', bygroups(Name.Attribute, Operator, Whitespace, String)),
+ (r'^-----END [^\n]+-----$', Generic.Heading, 'root'),
+ (r'\S+', String),
+ ],
+ }
+
+ def analyse_text(text):
+ if re.search(r'^-----BEGIN [^\n]+-----\r?\n', text):
+ return True
diff --git a/tests/examplefiles/asc/id_ecdsa b/tests/examplefiles/asc/id_ecdsa
new file mode 100644
index 00000000..b9245e82
--- /dev/null
+++ b/tests/examplefiles/asc/id_ecdsa
@@ -0,0 +1,9 @@
+-----BEGIN EC PRIVATE KEY-----
+Proc-Type: 4,ENCRYPTED
+DEK-Info: AES-128-CBC,IVIVIVIVIVIVIVIVIVIVIVIVIVIVIVIV
+
+ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
+ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
+ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
+ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopq=
+-----END EC PRIVATE KEY-----
diff --git a/tests/examplefiles/asc/id_ecdsa.output b/tests/examplefiles/asc/id_ecdsa.output
new file mode 100644
index 00000000..0c3ad48b
--- /dev/null
+++ b/tests/examplefiles/asc/id_ecdsa.output
@@ -0,0 +1,29 @@
+'-----BEGIN EC PRIVATE KEY-----' Generic.Heading
+'\n' Text.Whitespace
+
+'Proc-Type' Name.Attribute
+':' Operator
+' ' Text.Whitespace
+'4,ENCRYPTED' Literal.String
+'\n' Text.Whitespace
+
+'DEK-Info' Name.Attribute
+':' Operator
+' ' Text.Whitespace
+'AES-128-CBC,IVIVIVIVIVIVIVIVIVIVIVIVIVIVIVIV' Literal.String
+'\n\n' Text.Whitespace
+
+'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' Literal.String
+'\n' Text.Whitespace
+
+'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' Literal.String
+'\n' Text.Whitespace
+
+'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' Literal.String
+'\n' Text.Whitespace
+
+'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopq=' Literal.String
+'\n' Text.Whitespace
+
+'-----END EC PRIVATE KEY-----' Generic.Heading
+'\n' Text.Whitespace