Skip to content

Commit 4143a49

Browse files
committed
Encrypt multi ToC
1 parent 24b5dcb commit 4143a49

File tree

2 files changed

+49
-36
lines changed

2 files changed

+49
-36
lines changed

encryptcontent/decrypt-form.tpl.html

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,34 @@ <h1>{{ summary }}</h1>
8686
decrypt_button = document.getElementById("mkdocs-decrypt-button"),
8787
{% endif %}
8888
{% if encrypted_toc %}
89-
encrypted_toc = document.getElementById('mkdocs-encrypted-toc'),
89+
//encrypted_toc = document.getElementById('mkdocs-encrypted-toc'),
90+
// HTMLCollection, cause we can have multiples ToC items ...
91+
encrypted_toc = document.getElementsByClassName('mkdocs-encrypted-toc'),
9092
{% endif %}
9193
decrypt_form = document.getElementById('mkdocs-decrypt-form');
9294
// Adjust password field width to placeholder length
9395
let input = document.getElementById("mkdocs-content-password");
9496
input.setAttribute('size', input.getAttribute('placeholder').length);
9597
{% if encrypted_toc %}
96-
// Decrypt Table Of Content
98+
// Decrypt all Table Of Content on the page
9799
var decrypt_toc = function() {
98-
// grab the cipher ToC bundle
99-
var parts = encrypted_toc.innerHTML.split(';');
100-
// decrypt it
101-
var content = decrypt_content(
102-
password_input.value,
103-
parts[0],
104-
parts[1],
105-
parts[2]
106-
);
107-
if (content) {
108-
// success; display the decrypted ToC
109-
encrypted_toc.innerHTML = content;
110-
encrypted_toc.style.display = null;
111-
// any post processing on the decrypted toc should be done here
112-
}
100+
for (i = 0; i < encrypted_toc.length; i++) {
101+
// grab the cipher ToC bundle
102+
var parts = encrypted_toc[i].innerHTML.split(';');
103+
// decrypt it
104+
var content = decrypt_content(
105+
password_input.value,
106+
parts[0],
107+
parts[1],
108+
parts[2]
109+
);
110+
if (content) {
111+
// success; display the decrypted ToC
112+
encrypted_toc[i].innerHTML = content;
113+
encrypted_toc[i].style.display = null;
114+
// any post processing on the decrypted toc should be done here
115+
}
116+
}
113117
}
114118
{% endif %}
115119
// Decrypt content
@@ -194,4 +198,4 @@ <h1>{{ summary }}</h1>
194198
};
195199
document.addEventListener('DOMContentLoaded', init_decryptor);
196200
})();
197-
</script>
201+
</script>

encryptcontent/plugin.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)