plugin.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """
  2. pygments.plugin
  3. ~~~~~~~~~~~~~~~
  4. Pygments plugin interface.
  5. lexer plugins::
  6. [pygments.lexers]
  7. yourlexer = yourmodule:YourLexer
  8. formatter plugins::
  9. [pygments.formatters]
  10. yourformatter = yourformatter:YourFormatter
  11. /.ext = yourformatter:YourFormatter
  12. As you can see, you can define extensions for the formatter
  13. with a leading slash.
  14. syntax plugins::
  15. [pygments.styles]
  16. yourstyle = yourstyle:YourStyle
  17. filter plugin::
  18. [pygments.filter]
  19. yourfilter = yourfilter:YourFilter
  20. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  21. :license: BSD, see LICENSE for details.
  22. """
  23. from importlib.metadata import entry_points
  24. LEXER_ENTRY_POINT = 'pygments.lexers'
  25. FORMATTER_ENTRY_POINT = 'pygments.formatters'
  26. STYLE_ENTRY_POINT = 'pygments.styles'
  27. FILTER_ENTRY_POINT = 'pygments.filters'
  28. def iter_entry_points(group_name):
  29. groups = entry_points()
  30. if hasattr(groups, 'select'):
  31. # New interface in Python 3.10 and newer versions of the
  32. # importlib_metadata backport.
  33. return groups.select(group=group_name)
  34. else:
  35. # Older interface, deprecated in Python 3.10 and recent
  36. # importlib_metadata, but we need it in Python 3.8 and 3.9.
  37. return groups.get(group_name, [])
  38. def find_plugin_lexers():
  39. for entrypoint in iter_entry_points(LEXER_ENTRY_POINT):
  40. yield entrypoint.load()
  41. def find_plugin_formatters():
  42. for entrypoint in iter_entry_points(FORMATTER_ENTRY_POINT):
  43. yield entrypoint.name, entrypoint.load()
  44. def find_plugin_styles():
  45. for entrypoint in iter_entry_points(STYLE_ENTRY_POINT):
  46. yield entrypoint.name, entrypoint.load()
  47. def find_plugin_filters():
  48. for entrypoint in iter_entry_points(FILTER_ENTRY_POINT):
  49. yield entrypoint.name, entrypoint.load()