|
14 | 14 | from material import __version__ as material_version |
15 | 15 | from mkdocs.config.defaults import MkDocsConfig |
16 | 16 | from mkdocs.structure.pages import Page |
| 17 | +from mkdocs.utils import yaml_load |
17 | 18 | from mkdocs.utils.meta import get_data |
18 | 19 |
|
19 | 20 | # ########################################################################### |
@@ -138,3 +139,50 @@ def on_config(config: MkDocsConfig) -> MkDocsConfig: |
138 | 139 | logger.info( |
139 | 140 | log_prefix + "Contenus récents ajoutés à la configuration globale du site." |
140 | 141 | ) |
| 142 | + |
| 143 | + # -- LOAD SUBCONFIGS -- |
| 144 | + |
| 145 | + # list files to append |
| 146 | + configs_to_merge = Path("config").glob("*.yml") |
| 147 | + |
| 148 | + # load final config |
| 149 | + for cfg_file in configs_to_merge: |
| 150 | + logger.info(log_prefix + f"Chargement de la sous-configuration : {cfg_file}") |
| 151 | + dest_section = cfg_file.stem.split("_")[0] |
| 152 | + # print(dest_section) |
| 153 | + with cfg_file.open(mode="r") as part_config: |
| 154 | + cfg_data = yaml_load(part_config) |
| 155 | + out_section = config.get(dest_section) |
| 156 | + print( |
| 157 | + # out_section, |
| 158 | + type(out_section), |
| 159 | + isinstance(out_section, (dict, list)), |
| 160 | + ) |
| 161 | + if isinstance(out_section, list): |
| 162 | + out_section.append(cfg_data) |
| 163 | + elif isinstance(out_section, dict): |
| 164 | + out_section.update(cfg_data) |
| 165 | + else: |
| 166 | + print("toto") |
| 167 | + logging.info( |
| 168 | + log_prefix |
| 169 | + + f"La section '{cfg_file.stem}' n'existe pas et va donc être ajoutée." |
| 170 | + ) |
| 171 | + config[cfg_file.stem] = cfg_data |
| 172 | + break |
| 173 | + |
| 174 | + return config |
| 175 | + |
| 176 | + # # write merged final config file |
| 177 | + # with Path("mkdocs-generated-configuration.yml").open( |
| 178 | + # "w", encoding="UTF-8" |
| 179 | + # ) as out_file: |
| 180 | + # yaml.dump( |
| 181 | + # config.__dict__, |
| 182 | + # out_file, |
| 183 | + # sort_keys=False, |
| 184 | + # default_flow_style=False, |
| 185 | + # encoding="UTF8", |
| 186 | + # ) |
| 187 | + |
| 188 | + return config |
0 commit comments