ovasquez / mkdocs-merge

Merge the source of multiple MkDocs sites into a single one
https://ovasquez.github.io/mkdocs-merge/
MIT License
22 stars 6 forks source link

Fail to load `mkdocs.yml` #15

Open ThibsG opened 1 year ago

ThibsG commented 1 year ago

Hi @ovasquez , thank you for this useful tool!

I encounter an issue when in my mkdocs.yml of a sub-site I'm using special characters, such as !:

markdown_extensions:
  - pymdownx.emoji:
      emoji_index: !!python/name:materialx.emoji.twemoji
      emoji_generator: !!python/name:materialx.emoji.to_svg

for example when you setup this extension: https://github.com/facelessuser/mkdocs-material-extensions#inline-svg-icons

turns into:

❯ mkdocs-merge run master_site sub_site

Attempting to merge site: sub_site
Error loading the yaml file "sub_site/mkdocs.yml". This site will be skipped.

Sounds like the Ruamel Yaml parser is not working with such !! tag

ovasquez commented 1 year ago

Thanks for reporting this issue, it might be time to update the dependencies. I'll check if just by updating the tag is recognized.

ovasquez commented 1 year ago

@ThibsG I created a new release and updated the minimum version of ruamel.yaml, I quickly tested it locally and it worked to parse tags. Could you try it and let me know the results?

ThibsG commented 1 year ago

Hi @ovasquez , thanks a lot for being this responsive!

Unfortunately the new release didn't solve this issue. I dig further and found it is coming from this line: https://github.com/ovasquez/mkdocs-merge/blob/main/mkdocsmerge/merge.py#L68 The ruamel.yaml documentation says:

typ='safe' accomplishes the same as what safe_load() did before: loading of a document without resolving unknown tags

indeed in my case:

from ruamel.yaml import YAML
from pathlib import Path
yaml=YAML()
yaml.load(Path('./mkdocs.yml'))

works but

from ruamel.yaml import YAML
from pathlib import Path
yaml=YAML(typ='safe')
yaml.load(Path('./mkdocs.yml'))

ends up with an error:

~/.local/lib/python3.10/site-packages/ruamel/yaml/constructor.py in construct_undefined(self, node)
    688     def construct_undefined(self, node):
    689         # type: (Any) -> None
--> 690         raise ConstructorError(
    691             None,
    692             None,

ConstructorError: could not determine a constructor for the tag '!!python/name:materialx.emoji.twemoji'
  in "mkdocs.yml", line 40, column 18

To be honest I'm not familiar with tags in yaml and I don't really get why those are not resolved. The typ=safe seems a good approach, and I was wondering why it is not used to load the master yaml?

edit: found that people got the same issue and a solution with PyYaml

ovasquez commented 1 year ago

Thanks for noting that there is a solution, I will have to test if using something like that allows processing those tags, or if this is something that can be incorporated in the downstream repos (ruamel or PyYaml).