Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lexers): add ASCII armored #1807

Merged
merged 5 commits into from Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -201,6 +201,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
Expand Down
1 change: 1 addition & 0 deletions pygments/lexers/_mapping.py
Expand Up @@ -41,6 +41,7 @@
'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',), ()),
Expand Down
51 changes: 51 additions & 0 deletions 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.
scop marked this conversation as resolved.
Show resolved Hide resolved

.. versionadded:: 2.10
"""
name = 'ASCII armored'
aliases = ['asc', 'pem']
scop marked this conversation as resolved.
Show resolved Hide resolved
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
9 changes: 9 additions & 0 deletions 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-----
29 changes: 29 additions & 0 deletions tests/examplefiles/asc/id_ecdsa.output

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.