asc.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """
  2. pygments.lexers.asc
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexer for various ASCII armored files.
  5. :copyright: Copyright 2006-2023 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. .. versionadded:: 2.10
  17. """
  18. name = 'ASCII armored'
  19. aliases = ['asc', 'pem']
  20. filenames = [
  21. '*.asc', # PGP; *.gpg, *.pgp, and *.sig too, but those can be binary
  22. '*.pem', # X.509; *.cer, *.crt, *.csr, and key etc too, but those can be binary
  23. 'id_dsa', 'id_ecdsa', 'id_ecdsa_sk', 'id_ed25519', 'id_ed25519_sk',
  24. 'id_rsa', # SSH private keys
  25. ]
  26. mimetypes = ['application/pgp-keys', 'application/pgp-encrypted',
  27. 'application/pgp-signature', 'application/pem-certificate-chain']
  28. flags = re.MULTILINE
  29. tokens = {
  30. 'root': [
  31. (r'\s+', Whitespace),
  32. (r'^-----BEGIN [^\n]+-----$', Generic.Heading, 'data'),
  33. (r'\S+', Comment),
  34. ],
  35. 'data': [
  36. (r'\s+', Whitespace),
  37. (r'^([^:]+)(:)([ \t]+)(.*)',
  38. bygroups(Name.Attribute, Operator, Whitespace, String)),
  39. (r'^-----END [^\n]+-----$', Generic.Heading, 'root'),
  40. (r'\S+', String),
  41. ],
  42. }
  43. def analyse_text(text):
  44. if re.search(r'^-----BEGIN [^\n]+-----\r?\n', text):
  45. return True