unverbuggt / mkdocs-encryptcontent-plugin

A MkDocs plugin that encrypt/decrypt markdown content with AES
https://unverbuggt.github.io/mkdocs-encryptcontent-plugin/
MIT License
130 stars 16 forks source link

Small bug in encrypted_something #34

Closed unverbuggt closed 2 years ago

unverbuggt commented 2 years ago

Hi,

I use encrypted_something to encrypt the menu and and toc of an encrypted page like this:

        encrypted_something:
          myNav: [div, id]
          myToc: [div, id]
          myTocButton: [div, id]

these divs do not have much more functionality than to find and encrypt them:

<div id="myNav" style="font-weight: bold;"> ... </div>
<div id="myToc"> ... </div>
<div class="w3-dropdown-hover w3-hide-large headings" id="myTocButton"> ... </div>

mkdocs build exits like this:

Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Program Files\Python310\Scripts\mkdocs.exe\__main__.py", line 7, in <module>
  File "C:\Program Files\Python310\lib\site-packages\click\core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "C:\Program Files\Python310\lib\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "C:\Program Files\Python310\lib\site-packages\click\core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Program Files\Python310\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Program Files\Python310\lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "C:\Program Files\Python310\lib\site-packages\mkdocs\__main__.py", line 192, in build_command
    build.build(config.load_config(**kwargs), dirty=not clean)
  File "C:\Program Files\Python310\lib\site-packages\mkdocs\commands\build.py", line 314, in build
    _build_page(file.page, config, doc_files, nav, env, dirty)
  File "C:\Program Files\Python310\lib\site-packages\mkdocs\commands\build.py", line 220, in _build_page
    output = config['plugins'].run_event(
  File "C:\Program Files\Python310\lib\site-packages\mkdocs\plugins.py", line 102, in run_event
    result = method(item, **kwargs)
  File "C:\Program Files\Python310\lib\site-packages\encryptcontent\plugin.py", line 392, in on_post_page
    item['style'] = item['class'] + "display:none"
  File "C:\Program Files\Python310\lib\site-packages\bs4\element.py", line 1519, in __getitem__
    return self.attrs[key]
KeyError: 'class'

the line 392 in plugin.py reads:

                                item['style'] = item['class'] + "display:none"

and I do believe it is a bug and should be:

                                item['style'] = item['style'] + "display:none"

I do believe the div causing the trouble is the first one (myNav).

                        item.contents = [bs4_encrypted_content]
                        if item.has_attr('style'):
                            if isinstance(item['style'], list):
                                item['style'].append("display:none")
                            else:
                                item['style'] = item['style'] + "display:none"
                        else:
                            item['style'] = "display:none"

it has the attribure "style", but only one style defined (and beautifulsoup doesn't make a list out of it?).

CoinK0in commented 2 years ago

Hey,

Your diagnosis is correct. When there is only a single style BS4 returns a string and not a list. I couldn't found the commit related to inserting this bug, I think it has been present since the beginning...

I fixed the bug in version 2.3.1

Thanks for your repport :)