asc.py 1.7 KB

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