diff --git a/AUTHORS b/AUTHORS index 743ac6ed15..9fc03933a0 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/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py index 4dc2615c49..0e906c1d5f 100644 --- a/pygments/lexers/_mapping.py +++ b/pygments/lexers/_mapping.py @@ -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',), ()), diff --git a/pygments/lexers/asc.py b/pygments/lexers/asc.py new file mode 100644 index 0000000000..f775fd0791 --- /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 0000000000..b9245e829c --- /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 0000000000..0c3ad48b3f --- /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