Skip to content

Commit

Permalink
Add three hooks register_variables/macros/filters (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent Franceschetti committed Sep 5, 2024
1 parent 2fb833e commit 363d904
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
82 changes: 82 additions & 0 deletions mkdocs_macros/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ def _fail_with_undefined_error(self, *args, **kwargs):
# Plugin
# ------------------------------------------

# little utility for updating a dictionary from another
def register_items(category:str, ref:dict, additional:dict):
"""
Register outside items (additional) into a ref dictionary.
Fail with KeyError the key already exists.
E.g: register_items('macro', self.macros, items)
"""
for key, value in additional.items():
if key in ref:
raise KeyError("Registration error: "
"%s %s already exists" % (category, key))
ref[key] = value


class MacrosPlugin(BasePlugin):
"""
Expand Down Expand Up @@ -113,6 +127,15 @@ class MacrosPlugin(BasePlugin):
('verbose', PluginType(bool, default=False))
)



# these are lists of external items (loaded last)
# in case they are declared before on_config is run
# (i.e. other plugin is running before this one)
_add_macros = {}
_add_filters = {}
_add_variables = {}

def start_chatting(self, prefix: str, color: str = 'yellow'):
"Generate a chatter function (trace for macros)"
def chatter(*args):
Expand Down Expand Up @@ -301,6 +324,51 @@ def raw_markdown(self, value):
"as of 1.1.0; use env.markdown instead!")
self.markdown = value

# ----------------------------------
# Hooks for other applications
# ----------------------------------
@property
def register_macro(self, items:dict):

This comment has been minimized.

Copy link
@timvink

timvink Sep 11, 2024

Contributor

Probably you want register_macros here? The others are also plural

"""
Register macros (hook for other plugins).
These will be added last, and raise an exception if already present.
"""
try:
# after on_config
self._macros
register_items('macro', self.macros, items)
except AttributeError:
# before on_config: store for later
self._add_macros += items

@property

This comment has been minimized.

Copy link
@timvink

timvink Sep 11, 2024

Contributor

This is not a property, as you are using self. Please remove. Otherwise you get this:

image

This comment has been minimized.

Copy link
@timvink

timvink Sep 11, 2024

Contributor

Actually, I'll make a PR for macros later, then I can test + update both sides

def register_filters(self, items:dict):
"""
Register filters (hook for other plugins).
These will be added last, and raise an exception if already present.
"""
try:
# after on_config
self._filters
register_items('filter', self.filters, items)
except AttributeError:
# before on_config: store for later
self._add_filters += items

@property
def register_variables(self, items:dict):
"""
Register variables (hook for other plugins).
These will be added last, and raise an exception if already present.
"""
try:
# after on_config
self._variables
register_items('variables', self.variables, items)
except AttributeError:
# before on_config: store for later
self._add_variables += items

# ----------------------------------
# Function lists, for later events
# ----------------------------------
Expand Down Expand Up @@ -632,6 +700,20 @@ def on_config(self, config):
# by design, this MUST be the last step, so that programmers have
# full control on what happened in the configuration files
self._load_modules()


# place where variables/macros/filters are registered
# if they they were declared before Mkdocs-Macros in the config file.
# self._add_variables['foo'] = 5
# def bar(x):
# "Dummy function"
# return x + 5
# self._add_macros['bar'] = bar
# self._add_filters['baz'] = lambda s: s.upper()
register_items('variable', self.variables, self._add_variables)
register_items('macro' , self.macros , self._add_macros )
register_items('filter' , self.filters , self._add_filters )

# Provide information:
debug("Variables:", list(self.variables.keys()))
if len(extra):
Expand Down
4 changes: 3 additions & 1 deletion test/module/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def include_file(filename, start_line=0, end_line=None):
@env.macro
def doc_env():
"Document the environment"
return {name: getattr(env, name) for name in dir(env) if not name.startswith('_')}
return {name: getattr(env, name)
for name in dir(env) if not
(name.startswith('_') or name.startswith('register'))}

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

0 comments on commit 363d904

Please sign in to comment.