@@ -57,7 +57,7 @@ class encryptContentPlugin(BasePlugin):
5757 ('tag_encrypted_page' , mkdocs .config .config_options .Type (bool , default = False )),
5858 ('password_button' , mkdocs .config .config_options .Type (bool , default = False )),
5959 ('password_button_text' , mkdocs .config .config_options .Type (string_types , default = str (settings ['password_button_text' ]))),
60- ('encrypted_toc ' , mkdocs .config .config_options .Type (bool , default = False )),
60+ ('encrypted_something ' , mkdocs .config .config_options .Type (list , default = [] )),
6161 )
6262
6363 def __hash_md5__ (self , text ):
@@ -102,7 +102,7 @@ def __encrypt_content__(self, content):
102102 'hljs' : self .hljs ,
103103 'remember_password' : self .remember_password ,
104104 'disable_cookie_protection' : self .disable_cookie_protection ,
105- 'encrypted_toc ' : self .encrypted_toc ,
105+ 'encrypted_something ' : self .encrypted_something ,
106106 })
107107 return decrypt_form
108108
@@ -167,7 +167,7 @@ def on_pre_build(self, config):
167167 if 'password_button_text' in plugin_config .keys ():
168168 password_button_text = plugin_config .get ('password_button_text' )
169169 setattr (self , 'password_button_text' , password_button_text )
170- # Check if encrypted_toc feature is enable: encrypt table of content (PoC)
170+ # Check if encrypted_something feature is enable: encrypt each elke
171171 setattr (self , 'encrypted_toc' , False )
172172 if 'encrypted_toc' in plugin_config .keys ():
173173 encrypted_toc = self .config .get ('encrypted_toc' )
@@ -237,23 +237,32 @@ def on_post_page(self, output_content, page, config, **kwargs):
237237 :param config: global configuration object
238238 :return: output of rendered template as string
239239 """
240- # limit this process only if encrypted_toc feature is enable *(speedup x4)*
240+ # Limit this process only if encrypted_toc feature is enable *(speedup x4)*
241241 if self .encrypted_toc and hasattr (page , 'encrypted' ):
242242 soup = BeautifulSoup (output_content , 'html.parser' )
243- toc_search = soup .find ("div" , { "id" : "mkdocs-encrypted-toc" })
244- if toc_search is not None and len (toc_search .contents ) > 0 :
245- # Remove '\n', ' ' useless content generated by bs4 parsing...
246- toc_search .contents = [content for content in toc_search .contents if not content in ['\n ' , ' ' ]]
247- # Select childs items on tags div and encrypt all content with page password
248- ciphertoc_bundle = self .__encrypt_text_aes__ (toc_search .contents [0 ], page .password )
249- encrypted_toc = b';' .join (ciphertoc_bundle ).decode ('ascii' )
250- toc_search .contents [0 ].replaceWith (encrypted_toc )
251- if toc_search .has_attr ('style' ):
252- if isinstance (toc_search ['style' ], list ):
253- toc_search ['style' ].append ("display:none" )
243+ toc_search = soup .findAll ("nav" , { "class" : "mkdocs-encrypted-toc" })
244+ if toc_search is not None and len (toc_search ) > 0 :
245+ # Loop for multi tags (a.k.a: Mobile and Desktop)
246+ for item in toc_search :
247+ # Remove '\n', ' ' useless content generated by bs4 parsing...
248+ item .contents = [content for content in item .contents if not content in ['\n ' , ' ' ]]
249+ # Merge the content in case there are several elements
250+ if len (item .contents ) > 1 :
251+ merge_item = '' .join ([str (s ) for s in item .contents ])
254252 else :
255- toc_search ['style' ] = toc_search ['class' ] + "display:none"
256- else :
257- toc_search ['style' ] = "display:none"
253+ merge_item = item .contents [0 ]
254+ # Select childs items on target tags and encrypt all content with page password
255+ ciphertoc_bundle = self .__encrypt_text_aes__ (merge_item , page .password )
256+ encrypted_toc = b';' .join (ciphertoc_bundle ).decode ('ascii' )
257+ # Replace initial content with encrypted one
258+ bs4_encrypted_content = BeautifulSoup (encrypted_toc , 'html.parser' )
259+ item .contents = [bs4_encrypted_content ]
260+ if item .has_attr ('style' ):
261+ if isinstance (item ['style' ], list ):
262+ item ['style' ].append ("display:none" )
263+ else :
264+ item ['style' ] = item ['class' ] + "display:none"
265+ else :
266+ item ['style' ] = "display:none"
258267 output_content = str (soup )
259268 return output_content
0 commit comments