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