mkdocs-merge icon indicating copy to clipboard operation
mkdocs-merge copied to clipboard

Fail to load `mkdocs.yml`

Open ThibsG opened this issue 2 years ago • 4 comments

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

ThibsG avatar Nov 15 '22 14:11 ThibsG

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 avatar Nov 15 '22 23:11 ovasquez

@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?

ovasquez avatar Nov 17 '22 04:11 ovasquez

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

ThibsG avatar Nov 17 '22 09:11 ThibsG

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).

ovasquez avatar Jan 19 '23 05:01 ovasquez