Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mkdocs : utilise un hook de configuration plutôt que des scripts externes (2/2) #1240

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions hooks/mkdocs/G000_load_subconfigs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from material import __version__ as material_version
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.structure.pages import Page
from mkdocs.utils import yaml_load
from mkdocs.utils.meta import get_data

# ###########################################################################
Expand Down Expand Up @@ -138,3 +139,50 @@ def on_config(config: MkDocsConfig) -> MkDocsConfig:
logger.info(
log_prefix + "Contenus récents ajoutés à la configuration globale du site."
)

# -- LOAD SUBCONFIGS --

# list files to append
configs_to_merge = Path("config").glob("*.yml")

# load final config
for cfg_file in configs_to_merge:
logger.info(log_prefix + f"Chargement de la sous-configuration : {cfg_file}")
dest_section = cfg_file.stem.split("_")[0]
# print(dest_section)
with cfg_file.open(mode="r") as part_config:
cfg_data = yaml_load(part_config)
out_section = config.get(dest_section)
print(
# out_section,
type(out_section),
isinstance(out_section, (dict, list)),
)
if isinstance(out_section, list):
out_section.append(cfg_data)
elif isinstance(out_section, dict):
out_section.update(cfg_data)
else:
print("toto")
logging.info(
log_prefix
+ f"La section '{cfg_file.stem}' n'existe pas et va donc être ajoutée."
)
config[cfg_file.stem] = cfg_data
break

return config

# # write merged final config file
# with Path("mkdocs-generated-configuration.yml").open(
# "w", encoding="UTF-8"
# ) as out_file:
# yaml.dump(
# config.__dict__,
# out_file,
# sort_keys=False,
# default_flow_style=False,
# encoding="UTF8",
# )

return config
Loading