-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test the hooks for external registration (#237)
- added the register_macros test case - reimplemented the .variables, .macros and .filters properties for MacrosDocProject - updated documentation, to document hooks scripts (including comparison with MkDocs-Macros modules)
- Loading branch information
Laurent Franceschetti
committed
Oct 10, 2024
1 parent
16be58d
commit 32cd528
Showing
12 changed files
with
350 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
message: Vive Zorglub | ||
--- | ||
|
||
# Main Page | ||
|
||
This project contains a registered macro | ||
|
||
## Variables | ||
I want to write 'foo': {{ foo }} | ||
|
||
x2 is also present: {{ x2 }} | ||
|
||
## Macros | ||
|
||
Calculation: {{ bar(2, 5) }} | ||
|
||
## Filters | ||
|
||
I want to scramble '{{ message }}': {{ message | scramble }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Second page | ||
|
||
This page shows the information on variables, | ||
to understand, how the functions, variables and filters | ||
have been registered. | ||
|
||
|
||
{{ macros_info() }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
def foo(x:int, y:str): | ||
"First macro" | ||
return f"{x} and {y}" | ||
|
||
def bar(x:int, y:int): | ||
"Second macro" | ||
return x + y | ||
|
||
def scramble(s:str, length:int=None): | ||
""" | ||
Dummy filter to reverse the string and swap the case of each character. | ||
Usage in Markdown page: | ||
{{ "Hello world" | scramble }} -> Dlrow Olleh | ||
{{ "Hello world" | scramble(6) }} -> Dlrow | ||
""" | ||
# Split the phrase into words | ||
words = s.split() | ||
# Reverse each word and then reverse the order of the words | ||
reversed_words = [word[::-1].capitalize() for word in words][::-1] | ||
# Join the reversed words to form the new phrase | ||
new_phrase = ' '.join(reversed_words) | ||
if length: | ||
new_phrase = new_phrase[length] | ||
return new_phrase | ||
|
||
|
||
MY_FUNCTIONS = {"foo": foo, "bar": bar} | ||
MY_VARIABLES = {"x1": 5, "x2": 'hello world'} | ||
MY_FILTERS = {"scramble": scramble} | ||
|
||
|
||
def on_config(config, **kwargs): | ||
"Add the functions variables and filters to the mix" | ||
# get MkdocsMacros plugin, but only if present | ||
macros_plugin = config.plugins.get("macros") | ||
if macros_plugin: | ||
macros_plugin.register_macros(MY_FUNCTIONS) | ||
macros_plugin.register_variables(MY_VARIABLES) | ||
macros_plugin.register_filters(MY_FILTERS) | ||
else: | ||
raise SystemError("Cannot find macros plugin!") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
site_name: Testing the hooks | ||
theme: readthedocs | ||
|
||
nav: | ||
- Home: index.md | ||
- Next page: second.md | ||
|
||
hooks: | ||
# Mkdocs hook for testing the Mkdocs-Macros hook | ||
- hooks.py | ||
|
||
plugins: | ||
- search | ||
- macros | ||
- test | ||
|
||
extra: | ||
greeting: Hello World! | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
""" | ||
Testing the project | ||
(C) Laurent Franceschetti 2024 | ||
""" | ||
|
||
|
||
import pytest | ||
|
||
from test.fixture import MacrosDocProject | ||
|
||
|
||
from .hooks import MY_VARIABLES, MY_FUNCTIONS, MY_FILTERS, bar, scramble | ||
|
||
|
||
def test_pages(): | ||
project = MacrosDocProject(".") | ||
build_result = project.build(strict=True) | ||
# did not fail | ||
return_code = project.build_result.returncode | ||
assert not return_code, f"Build returned with {return_code} {build_result.args})" | ||
|
||
# check the presence of variables in the environment | ||
print("Variables:", list(project.variables.keys())) | ||
for variable in MY_VARIABLES: | ||
assert variable in project.variables | ||
print(f"{variable}: {project.variables[variable]}") | ||
|
||
print("Macros:", list(project.macros.keys())) | ||
for macro in MY_FUNCTIONS: | ||
assert macro in project.macros | ||
print(f"{macro}: {project.macros[macro]}") | ||
|
||
print("Filters:", list(project.filters.keys())) | ||
for filter in MY_FILTERS: | ||
assert filter in project.filters | ||
print(f"{filter}: {project.filters[filter]}") | ||
|
||
# ---------------- | ||
# First page | ||
# ---------------- | ||
|
||
|
||
page = project.get_page('index') | ||
assert page.is_markdown_rendered() | ||
# variable | ||
value = MY_VARIABLES['x2'] | ||
print(f"Check if x2 ('{value}') is present") | ||
assert page.find(value, header="Variables") | ||
# macro | ||
print("Check macro: bar") | ||
assert page.find(bar(2, 5), header="Macros") | ||
# filter | ||
message = page.meta.message | ||
result = scramble(message) | ||
print(f"Check filter: scramble('{message}') --> '{result}'") | ||
assert page.find(result, header="Filters") | ||
|
||
|
||
|
||
|
||
# ---------------- | ||
# Second page | ||
# ---------------- | ||
# there is intentionally an error (`foo` does not exist) | ||
page = project.get_page('second') | ||
assert 'foo' not in project.config.extra | ||
assert page.is_markdown_rendered() | ||
assert not page.has_error() | ||
|
Oops, something went wrong.