Skip to content

Commit 9ef2b98

Browse files
committed
Include new function yaml_support() from super_collections (#258)
- The purpose is to prevent failures of yaml dumps
1 parent 86e31f7 commit 9ef2b98

File tree

3 files changed

+25
-45
lines changed

3 files changed

+25
-45
lines changed

mkdocs_macros/plugin.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
from jinja2 import (
1818
Environment, FileSystemLoader, Undefined, DebugUndefined, StrictUndefined,
1919
)
20-
from super_collections import SuperDict
20+
from super_collections import SuperDict, yaml_support
21+
yaml_support()
2122

2223
from mkdocs.config import config_options
2324
from mkdocs.config.config_options import Type as PluginType
@@ -29,7 +30,7 @@
2930
from mkdocs_macros.util import (
3031
install_package, parse_package, trace, debug,
3132
update, import_local_module, format_chatter, LOG, get_log_level,
32-
setup_directory, CustomEncoder,
33+
setup_directory
3334
# SuperDict,
3435
)
3536

@@ -881,16 +882,13 @@ def on_pre_build(self, *, config):
881882
after the execution of the `on_config()` of this plugin.
882883
"""
883884
trace("Config variables:", list(self.variables.keys()))
884-
debug("Config variables:\n", payload=json.dumps(self.variables,
885-
cls=CustomEncoder))
885+
debug("Config variables:\n", payload=SuperDict(self.variables).to_json())
886886
if self.macros:
887887
trace("Config macros:", list(self.macros.keys()))
888-
debug("Config macros:", payload=json.dumps(self.macros,
889-
cls=CustomEncoder))
888+
debug("Config macros:", payload=SuperDict(self.macros).to_json())
890889
if self.filters:
891890
trace("Config filters:", list(self.filters.keys()))
892-
debug("Config filters:", payload=json.dumps(self.filters,
893-
cls=CustomEncoder))
891+
debug("Config filters:", payload=SuperDict(self.filters).to_json())
894892

895893

896894
def on_nav(self, nav, config, files):

mkdocs_macros/util.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os, sys, importlib.util, shutil
1010
from typing import Literal
1111
from packaging.version import Version
12+
import pkg_resources
1213
import json
1314
import inspect
1415
from datetime import datetime
@@ -116,32 +117,7 @@ def format_chatter(*args, prefix:str, color:str=TRACE_COLOR):
116117

117118

118119

119-
from collections import UserDict
120120

121-
class CustomEncoder(json.JSONEncoder):
122-
"""
123-
Custom encoder for JSON serialization.
124-
Used for debugging purposes.
125-
"""
126-
def default(self, obj: Any) -> Any:
127-
if isinstance(obj, datetime):
128-
return obj.isoformat()
129-
if isinstance(obj, UserDict):
130-
# for objects used by MkDocs (config, plugin, etc.s)
131-
return dict(obj)
132-
133-
elif inspect.isfunction(obj):
134-
return f"Function: %s %s" % (inspect.signature(obj),
135-
obj.__doc__)
136-
try:
137-
return super().default(obj)
138-
except TypeError:
139-
debug(f"json: cannot encode {obj.__class__}")
140-
try:
141-
return str(obj)
142-
except Exception:
143-
# in case something happens along the line
144-
return f"!Non printable object: {obj.__class__}"
145121

146122

147123

@@ -167,6 +143,20 @@ def parse_package(package:str):
167143
source_name, package_name = l[:2]
168144
return source_name, package_name
169145

146+
147+
148+
def is_package_installed(source_name: str) -> bool:
149+
"""
150+
Check if a package is installed, with its source name
151+
(not it is Python import name).
152+
"""
153+
try:
154+
pkg_resources.get_distribution(source_name)
155+
return True
156+
except pkg_resources.DistributionNotFound:
157+
return False
158+
159+
170160
def install_package(package:str):
171161
"""
172162
Install a package from pip

pyproject.toml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,8 @@ description = "Unleash the power of MkDocs with macros and variables"
99
readme = "README.md"
1010
license = { text = "MIT" }
1111
requires-python = ">=3.8"
12-
authors = [
13-
{ name = "Laurent Franceschetti"},
14-
]
15-
keywords = [
16-
"macros",
17-
"markdown",
18-
"mkdocs",
19-
"python",
20-
]
12+
authors = [{ name = "Laurent Franceschetti" }]
13+
keywords = ["macros", "markdown", "mkdocs", "python"]
2114
classifiers = [
2215
"Development Status :: 5 - Production/Stable",
2316
"Intended Audience :: Developers",
@@ -35,7 +28,7 @@ dependencies = [
3528
"pathspec",
3629
"python-dateutil",
3730
"pyyaml",
38-
"super-collections",
31+
"super-collections >= 0.5.0",
3932
"termcolor",
4033
]
4134

@@ -48,12 +41,11 @@ test = [
4841
"mkdocs-macros-test",
4942
"mkdocs-material>=6.2",
5043
"mkdocs-test",
51-
"mkdocs-d2-plugin"
44+
"mkdocs-d2-plugin",
5245
]
5346

5447
[project.entry-points."mkdocs.plugins"]
5548
macros = "mkdocs_macros.plugin:MacrosPlugin"
5649

5750
[project.urls]
5851
Homepage = "https://github.com/fralau/mkdocs_macros_plugin"
59-

0 commit comments

Comments
 (0)