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