Skip to content

Commit 5f1373b

Browse files
committed
Merge branch 'scop-feat/ascii-armored'
2 parents 698007e + cd71718 commit 5f1373b

6 files changed

Lines changed: 92 additions & 0 deletions

File tree

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ Other contributors, listed alphabetically, are:
203203
* Robert Simmons -- Standard ML lexer
204204
* Kirill Simonov -- YAML lexer
205205
* Corbin Simpson -- Monte lexer
206+
* Ville Skyttä -- ASCII armored lexer
206207
* Alexander Smishlajev -- Visual FoxPro lexer
207208
* Steve Spigarelli -- XQuery lexer
208209
* Jerome St-Louis -- eC lexer

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Version 2.10.0
1313

1414
- Added lexers:
1515

16+
* ASC armored files (#1807)
1617
* GSQL (#1809, #1866)
1718
* Javascript REPL (#1825)
1819
* procfile (#1808)

pygments/lexers/_mapping.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
'AppleScriptLexer': ('pygments.lexers.scripting', 'AppleScript', ('applescript',), ('*.applescript',), ()),
4242
'ArduinoLexer': ('pygments.lexers.c_like', 'Arduino', ('arduino',), ('*.ino',), ('text/x-arduino',)),
4343
'ArrowLexer': ('pygments.lexers.arrow', 'Arrow', ('arrow',), ('*.arw',), ()),
44+
'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')),
4445
'AspectJLexer': ('pygments.lexers.jvm', 'AspectJ', ('aspectj',), ('*.aj',), ('text/x-aspectj',)),
4546
'AsymptoteLexer': ('pygments.lexers.graphics', 'Asymptote', ('asymptote', 'asy'), ('*.asy',), ('text/x-asymptote',)),
4647
'AugeasLexer': ('pygments.lexers.configs', 'Augeas', ('augeas',), ('*.aug',), ()),

pygments/lexers/asc.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
pygments.lexers.asc
3+
~~~~~~~~~~~~~~~~~~~
4+
5+
Lexer for various ASCII armored files.
6+
7+
:copyright: Copyright 2021 by the Pygments team, see AUTHORS.
8+
:license: BSD, see LICENSE for details.
9+
"""
10+
import re
11+
12+
from pygments.lexer import RegexLexer, bygroups
13+
from pygments.token import Comment, Generic, Name, Operator, String, Whitespace
14+
15+
__all__ = ['AscLexer']
16+
17+
18+
class AscLexer(RegexLexer):
19+
"""
20+
Lexer for ASCII armored files, containing `-----BEGIN/END ...-----` wrapped base64 data.
21+
22+
.. versionadded:: 2.10
23+
"""
24+
name = 'ASCII armored'
25+
aliases = ['asc', 'pem']
26+
filenames = [
27+
'*.asc', # PGP; *.gpg, *.pgp, and *.sig too, but those can be binary
28+
'*.pem', # X.509; *.cer, *.crt, *.csr, and key etc too, but those can be binary
29+
'id_dsa', 'id_ecdsa', 'id_ecdsa_sk', 'id_ed25519', 'id_ed25519_sk', 'id_rsa', # SSH private keys
30+
]
31+
mimetypes = ['application/pgp-keys', 'application/pgp-encrypted', 'application/pgp-signature']
32+
33+
flags = re.MULTILINE
34+
35+
tokens = {
36+
'root': [
37+
(r'\s+', Whitespace),
38+
(r'^-----BEGIN [^\n]+-----$', Generic.Heading, 'data'),
39+
(r'\S+', Comment),
40+
],
41+
'data': [
42+
(r'\s+', Whitespace),
43+
(r'^([^:]+)(:)([ \t]+)(.*)', bygroups(Name.Attribute, Operator, Whitespace, String)),
44+
(r'^-----END [^\n]+-----$', Generic.Heading, 'root'),
45+
(r'\S+', String),
46+
],
47+
}
48+
49+
def analyse_text(text):
50+
if re.search(r'^-----BEGIN [^\n]+-----\r?\n', text):
51+
return True

tests/examplefiles/asc/id_ecdsa

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
Proc-Type: 4,ENCRYPTED
3+
DEK-Info: AES-128-CBC,IVIVIVIVIVIVIVIVIVIVIVIVIVIVIVIV
4+
5+
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
6+
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
7+
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
8+
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopq=
9+
-----END EC PRIVATE KEY-----

tests/examplefiles/asc/id_ecdsa.output

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)