Skip to content

Commit 363d904

Browse files
author
Laurent Franceschetti
committed
Add three hooks register_variables/macros/filters (#237)
1 parent 2fb833e commit 363d904

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

mkdocs_macros/plugin.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ def _fail_with_undefined_error(self, *args, **kwargs):
6666
# Plugin
6767
# ------------------------------------------
6868

69+
# little utility for updating a dictionary from another
70+
def register_items(category:str, ref:dict, additional:dict):
71+
"""
72+
Register outside items (additional) into a ref dictionary.
73+
Fail with KeyError the key already exists.
74+
75+
E.g: register_items('macro', self.macros, items)
76+
"""
77+
for key, value in additional.items():
78+
if key in ref:
79+
raise KeyError("Registration error: "
80+
"%s %s already exists" % (category, key))
81+
ref[key] = value
82+
6983

7084
class MacrosPlugin(BasePlugin):
7185
"""
@@ -113,6 +127,15 @@ class MacrosPlugin(BasePlugin):
113127
('verbose', PluginType(bool, default=False))
114128
)
115129

130+
131+
132+
# these are lists of external items (loaded last)
133+
# in case they are declared before on_config is run
134+
# (i.e. other plugin is running before this one)
135+
_add_macros = {}
136+
_add_filters = {}
137+
_add_variables = {}
138+
116139
def start_chatting(self, prefix: str, color: str = 'yellow'):
117140
"Generate a chatter function (trace for macros)"
118141
def chatter(*args):
@@ -301,6 +324,51 @@ def raw_markdown(self, value):
301324
"as of 1.1.0; use env.markdown instead!")
302325
self.markdown = value
303326

327+
# ----------------------------------
328+
# Hooks for other applications
329+
# ----------------------------------
330+
@property
331+
def register_macro(self, items:dict):
332+
"""
333+
Register macros (hook for other plugins).
334+
These will be added last, and raise an exception if already present.
335+
"""
336+
try:
337+
# after on_config
338+
self._macros
339+
register_items('macro', self.macros, items)
340+
except AttributeError:
341+
# before on_config: store for later
342+
self._add_macros += items
343+
344+
@property
345+
def register_filters(self, items:dict):
346+
"""
347+
Register filters (hook for other plugins).
348+
These will be added last, and raise an exception if already present.
349+
"""
350+
try:
351+
# after on_config
352+
self._filters
353+
register_items('filter', self.filters, items)
354+
except AttributeError:
355+
# before on_config: store for later
356+
self._add_filters += items
357+
358+
@property
359+
def register_variables(self, items:dict):
360+
"""
361+
Register variables (hook for other plugins).
362+
These will be added last, and raise an exception if already present.
363+
"""
364+
try:
365+
# after on_config
366+
self._variables
367+
register_items('variables', self.variables, items)
368+
except AttributeError:
369+
# before on_config: store for later
370+
self._add_variables += items
371+
304372
# ----------------------------------
305373
# Function lists, for later events
306374
# ----------------------------------
@@ -632,6 +700,20 @@ def on_config(self, config):
632700
# by design, this MUST be the last step, so that programmers have
633701
# full control on what happened in the configuration files
634702
self._load_modules()
703+
704+
705+
# place where variables/macros/filters are registered
706+
# if they they were declared before Mkdocs-Macros in the config file.
707+
# self._add_variables['foo'] = 5
708+
# def bar(x):
709+
# "Dummy function"
710+
# return x + 5
711+
# self._add_macros['bar'] = bar
712+
# self._add_filters['baz'] = lambda s: s.upper()
713+
register_items('variable', self.variables, self._add_variables)
714+
register_items('macro' , self.macros , self._add_macros )
715+
register_items('filter' , self.filters , self._add_filters )
716+
635717
# Provide information:
636718
debug("Variables:", list(self.variables.keys()))
637719
if len(extra):

test/module/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def include_file(filename, start_line=0, end_line=None):
3131
@env.macro
3232
def doc_env():
3333
"Document the environment"
34-
return {name: getattr(env, name) for name in dir(env) if not name.startswith('_')}
34+
return {name: getattr(env, name)
35+
for name in dir(env) if not
36+
(name.startswith('_') or name.startswith('register'))}
3537

3638
# Optional: a special function for making relative urls point to root
3739
fix_url = env.macros.fix_url

0 commit comments

Comments
 (0)