Skip to content

Commit

Permalink
Merge branch 'feat/ascii-armored' of https://github.com/scop/pygments
Browse files Browse the repository at this point in the history
…into scop-feat/ascii-armored
  • Loading branch information
Anteru committed Aug 8, 2021
2 parents 698007e + 6e60767 commit 215eeef
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -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
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.
.. 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
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.

0 comments on commit 215eeef

Please sign in to comment.