plugin.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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-present by the Pygments team, see AUTHORS.
  21. :license: BSD, see LICENSE for details.
  22. """
  23. import functools
  24. from importlib.metadata import entry_points
  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. @functools.cache
  30. def iter_entry_points(group_name):
  31. groups = entry_points()
  32. if hasattr(groups, 'select'):
  33. # New interface in Python 3.10 and newer versions of the
  34. # importlib_metadata backport.
  35. return groups.select(group=group_name)
  36. else:
  37. # Older interface, deprecated in Python 3.10 and recent
  38. # importlib_metadata, but we need it in Python 3.8 and 3.9.
  39. return groups.get(group_name, [])
  40. def find_plugin_lexers():
  41. for entrypoint in iter_entry_points(LEXER_ENTRY_POINT):
  42. yield entrypoint.load()
  43. def find_plugin_formatters():
  44. for entrypoint in iter_entry_points(FORMATTER_ENTRY_POINT):
  45. yield entrypoint.name, entrypoint.load()
  46. def find_plugin_styles():
  47. for entrypoint in iter_entry_points(STYLE_ENTRY_POINT):
  48. yield entrypoint.name, entrypoint.load()
  49. def find_plugin_filters():
  50. for entrypoint in iter_entry_points(FILTER_ENTRY_POINT):
  51. yield entrypoint.name, entrypoint.load()