plugin.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """
  2. pygments.plugin
  3. ~~~~~~~~~~~~~~~
  4. Pygments setuptools plugin interface. The methods defined
  5. here also work if setuptools isn't installed but they just
  6. return nothing.
  7. lexer plugins::
  8. [pygments.lexers]
  9. yourlexer = yourmodule:YourLexer
  10. formatter plugins::
  11. [pygments.formatters]
  12. yourformatter = yourformatter:YourFormatter
  13. /.ext = yourformatter:YourFormatter
  14. As you can see, you can define extensions for the formatter
  15. with a leading slash.
  16. syntax plugins::
  17. [pygments.styles]
  18. yourstyle = yourstyle:YourStyle
  19. filter plugin::
  20. [pygments.filter]
  21. yourfilter = yourfilter:YourFilter
  22. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  23. :license: BSD, see LICENSE for details.
  24. """
  25. LEXER_ENTRY_POINT = 'pygments.lexers'
  26. FORMATTER_ENTRY_POINT = 'pygments.formatters'
  27. STYLE_ENTRY_POINT = 'pygments.styles'
  28. FILTER_ENTRY_POINT = 'pygments.filters'
  29. def iter_entry_points(group_name):
  30. try:
  31. import pkg_resources
  32. except (ImportError, OSError):
  33. return []
  34. return pkg_resources.iter_entry_points(group_name)
  35. def find_plugin_lexers():
  36. for entrypoint in iter_entry_points(LEXER_ENTRY_POINT):
  37. yield entrypoint.load()
  38. def find_plugin_formatters():
  39. for entrypoint in iter_entry_points(FORMATTER_ENTRY_POINT):
  40. yield entrypoint.name, entrypoint.load()
  41. def find_plugin_styles():
  42. for entrypoint in iter_entry_points(STYLE_ENTRY_POINT):
  43. yield entrypoint.name, entrypoint.load()
  44. def find_plugin_filters():
  45. for entrypoint in iter_entry_points(FILTER_ENTRY_POINT):
  46. yield entrypoint.name, entrypoint.load()