@@ -66,6 +66,20 @@ def _fail_with_undefined_error(self, *args, **kwargs):
66
66
# Plugin
67
67
# ------------------------------------------
68
68
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
+
69
83
70
84
class MacrosPlugin (BasePlugin ):
71
85
"""
@@ -113,6 +127,15 @@ class MacrosPlugin(BasePlugin):
113
127
('verbose' , PluginType (bool , default = False ))
114
128
)
115
129
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
+
116
139
def start_chatting (self , prefix : str , color : str = 'yellow' ):
117
140
"Generate a chatter function (trace for macros)"
118
141
def chatter (* args ):
@@ -301,6 +324,51 @@ def raw_markdown(self, value):
301
324
"as of 1.1.0; use env.markdown instead!" )
302
325
self .markdown = value
303
326
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
+
304
372
# ----------------------------------
305
373
# Function lists, for later events
306
374
# ----------------------------------
@@ -632,6 +700,20 @@ def on_config(self, config):
632
700
# by design, this MUST be the last step, so that programmers have
633
701
# full control on what happened in the configuration files
634
702
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
+
635
717
# Provide information:
636
718
debug ("Variables:" , list (self .variables .keys ()))
637
719
if len (extra ):
0 commit comments