14
14
import yaml
15
15
from jinja2 import Environment , FileSystemLoader
16
16
from mkdocs .plugins import BasePlugin
17
+ from mkdocs .config import config_options
17
18
from mkdocs .config .config_options import Type as PluginType
18
19
20
+
19
21
from .util import trace , debug , update , SuperDict , import_local_module , format_chatter , LOG
20
22
from .context import define_env
21
23
@@ -64,6 +66,35 @@ class MacrosPlugin(BasePlugin):
64
66
)
65
67
66
68
69
+ def start_chatting (self , prefix :str , color :str = 'yellow' ):
70
+ "Generate a chatter function (trace for macros)"
71
+ def chatter (* args ):
72
+ """
73
+ Defines a tracer for the Verbose mode, to be used in macros.
74
+ If `verbose: true` in the YAML config file (under macros plugin),
75
+ it will start "chattering"
76
+ (talking a lot and in a friendly way,
77
+ about mostly unimportant things).
78
+ Otherwise, it will remain silent.
79
+
80
+ If you change the `verbose` while the local server is activated,
81
+ (`mkdocs server`) this should be instantly reflected.
82
+
83
+ Usage:
84
+ -----
85
+ chatter = env.make_chatter('MY_MODULE_NAME')
86
+ chatter("This is a dull debug message.")
87
+
88
+ Will result in:
89
+
90
+ INFO - [macros - Simple module] - This is a dull info message.
91
+ """
92
+ if self .config ['verbose' ]:
93
+ LOG .info (format_chatter (* args , prefix = prefix , color = color ))
94
+
95
+ return chatter
96
+
97
+
67
98
# ------------------------------------------------
68
99
# These properties are available in the env object
69
100
# ------------------------------------------------
@@ -168,6 +199,19 @@ def reverse(x):
168
199
return v
169
200
170
201
202
+ @property
203
+ def post_build_functions (self ):
204
+ """
205
+ List of post build functions contained in modules.
206
+ These are deferred to the on_post_build() event.
207
+ """
208
+ try :
209
+ return self ._post_build_functions
210
+ except AttributeError :
211
+ raise AttributeError ("You have called post_build_functions property "
212
+ "too early. Does not exist yet !" )
213
+
214
+
171
215
# ----------------------------------
172
216
# load elements
173
217
# ----------------------------------
@@ -216,6 +260,8 @@ def foobar(x):
216
260
...
217
261
218
262
"""
263
+ self ._post_build_functions = []
264
+
219
265
# installed modules (as in pip list)
220
266
modules = self .config ['modules' ]
221
267
if modules :
@@ -252,6 +298,12 @@ def foobar(x):
252
298
"module '%s'. Prefer the define_env() function "
253
299
"(see documentation)!" % local_module_name )
254
300
function_found = True
301
+ if hasattr (module , 'on_post_build' ):
302
+ # append the module to the list of functions
303
+ # `on_post_build_functions`
304
+ # NOTE: each of these functions requires
305
+ # self (the environment).
306
+ self ._post_build_functions .append (module .on_post_build )
255
307
if not function_found :
256
308
raise NameError ("No valid function found in module '%s'" %
257
309
local_module_name )
@@ -457,30 +509,10 @@ def on_page_markdown(self, markdown, page, config,
457
509
self .variables ["page" ] = page
458
510
return self .render (markdown )
459
511
460
- def start_chatting (self , prefix :str , color :str = 'yellow' ):
461
- "Generate a chatter function"
462
- def chatter (* args ):
463
- """
464
- Defines a tracer for the Verbose mode, to be used in macros.
465
- If `verbose: true` in the YAML config file (under macros plugin),
466
- it will start "chattering"
467
- (talking a lot and in a friendly way, about mostly unimportant things).
468
- Otherwise, it will remain silent.
469
-
470
- If you change the `verbose` while the local server is activated,
471
- (`mkdocs server`) this should be instantly reflected.
472
-
473
- Usage:
474
- -----
475
- chatter = env.make_chatter('MY_MODULE_NAME')
476
- chatter("This is a dull debug message.")
477
-
478
- Will result in:
479
-
480
- INFO - [macros - Simple module] - This is a dull info message.
481
- """
482
- if self .config ['verbose' ]:
483
- LOG .info (format_chatter (* args , prefix = prefix , color = color ))
484
-
485
- return chatter
486
-
512
+ def on_post_build (self , config : config_options .Config ):
513
+ """
514
+ Hook for post build actions, typically adding
515
+ raw files to the setup.
516
+ """
517
+ for func in self .post_build_functions :
518
+ func (self )
0 commit comments