Skip to content

Commit 78ab4be

Browse files
committed
Add script reload functionality
1 parent ae99267 commit 78ab4be

File tree

4 files changed

+79
-33
lines changed

4 files changed

+79
-33
lines changed

README.md

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Install the package from source with pip:
3232
```bash
3333
cd mkdocs-encryptcontent-plugin/
3434
python3 setup.py sdist bdist_wheel
35-
pip3 install dist/mkdocs_encryptcontent_plugin-1.1.0-py3-none-any.whl
35+
pip3 install dist/mkdocs_encryptcontent_plugin-1.2.0-py3-none-any.whl
3636
```
3737

3838
Enable the plugin in your `mkdocs.yml`:
@@ -214,8 +214,10 @@ Child elements of `encrypted_something` are build with a key `<unique name>` in
214214
The list have to be contructed with the name of an HTML element `<html tag>` as first item and `id` or `class` as the second item.
215215

216216
```yaml
217-
encrypted_something:
218-
<uniq name>: [<html tag>, <'class' or 'id'>]
217+
plugins:
218+
- encryptcontent:
219+
encrypted_something:
220+
<uniq name>: [<html tag>, <'class' or 'id'>]
219221
```
220222

221223
The `<unique name>` key identifies the name of a specific element of the page that will be searched by beautifulSoup.
@@ -250,8 +252,10 @@ Then add these elements in the format of a yaml dictionary under the variable `e
250252
Set your configuration like this :
251253

252254
```yaml
253-
encrypted_something:
254-
mkdocs-encrypted-toc: [div, id]
255+
plugins:
256+
- encryptcontent:
257+
encrypted_something:
258+
mkdocs-encrypted-toc: [div, id]
255259
```
256260

257261
2. Other example, with multiples target. In you Material Theme, you want to encrypt ToC content and Footer.
@@ -275,10 +279,12 @@ After modification, your template looks like this :
275279

276280
Your configuration like this :
277281
```yaml
278-
encrypted_something:
279-
mkdocs-encrypted-toc: [nav, class]
280-
mkdocs-encrypted-footer: [div, id]
281-
mkdocs-encrypted-footer-meta: [div, id]
282+
plugins:
283+
- encryptcontent:
284+
encrypted_something:
285+
mkdocs-encrypted-toc: [nav, class]
286+
mkdocs-encrypted-footer: [div, id]
287+
mkdocs-encrypted-footer-meta: [div, id]
282288
```
283289

284290

@@ -305,6 +311,28 @@ It becomes possible again to make searches on all the pages, even if the content
305311
If you still want to protect some pages, even though the search index is not encrypted, you can use [mkdocs-exclude-search](https://github.com/chrieke/mkdocs-exclude-search) to exclude parts or complete articles from the search index.
306312

307313

314+
### Reload scripts
315+
316+
Related to [issue #14](https://github.com/CoinK0in/mkdocs-encryptcontent-plugin/issues/14)
317+
318+
You can set `reload_scripts:` in your `mkdocs.yml` with list of script source, to reload and execute some js lib after decryption process.
319+
320+
```yaml
321+
plugins:
322+
reload_scripts:
323+
- "./js/example.js"
324+
```
325+
326+
This feature use the following JQuery function to remove, add and reload javascripts.
327+
328+
```javascript
329+
var reload_js = function(src) {
330+
$('script[src="' + src + '"]').remove();
331+
$('<script>').attr('src', src).appendTo('head');
332+
};
333+
```
334+
335+
308336
## Contributing
309337

310338
From reporting a bug to submitting a pull request: every contribution is appreciated and welcome.

encryptcontent/decrypt-form.tpl.html

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ <h1>{{ summary }}</h1>
4444
return false;
4545
}
4646
};
47-
{% if remember_password %}
47+
{% if remember_password -%}
4848
var setCookie = function(name,value,days,path) {
4949
/* Set local cookie to store password */
5050
var expires = "";
@@ -57,13 +57,13 @@ <h1>{{ summary }}</h1>
5757
if (path) {
5858
current_path = "; path=" + path
5959
}
60-
{% if disable_cookie_protection %}
60+
{% if disable_cookie_protection -%}
6161
encryptcontent = name + "=" + encodeURIComponent(value || "") + expires + current_path;
62-
{% else %}
62+
{%- else -%}
6363
encryptcontent = name + "=" + encodeURIComponent(value || "") + expires + current_path;
6464
// add security flag on cookie
6565
encryptcontent = encryptcontent + "; SameSite=Strict; Secure";
66-
{% endif %}
66+
{%- endif %}
6767
return encryptcontent;
6868
};
6969
var getCookie = function(name) {
@@ -77,19 +77,24 @@ <h1>{{ summary }}</h1>
7777
}
7878
return null;
7979
};
80-
{% endif %}
80+
{%- endif %}
81+
// Reload scripts src after decryption process
82+
var reload_js = function(src) {
83+
$('script[src="' + src + '"]').remove();
84+
$('<script>').attr('src', src).appendTo('head');
85+
};
8186
var init_decryptor = function() {
8287
var password_input = document.getElementById('mkdocs-content-password'),
8388
encrypted_content = document.getElementById('mkdocs-encrypted-content'),
8489
decrypted_content = document.getElementById('mkdocs-decrypted-content'),
85-
{% if password_button %}
90+
{% if password_button -%}
8691
decrypt_button = document.getElementById("mkdocs-decrypt-button"),
87-
{% endif %}
92+
{%- endif -%}
8893
decrypt_form = document.getElementById('mkdocs-decrypt-form');
8994
// Adjust password field width to placeholder length
9095
let input = document.getElementById("mkdocs-content-password");
9196
input.setAttribute('size', input.getAttribute('placeholder').length);
92-
{% if encrypted_something %}
97+
{% if encrypted_something -%}
9398
var encrypted_something = {{ encrypted_something }}
9499
// Decrypt all others elements
95100
var decrypt_somethings = function() {
@@ -121,7 +126,7 @@ <h1>{{ summary }}</h1>
121126
}
122127
}
123128
}
124-
{% endif %}
129+
{%- endif %}
125130
// Decrypt content
126131
var decrypt_action = function() {
127132
// grab the ciphertext bundle
@@ -138,14 +143,18 @@ <h1>{{ summary }}</h1>
138143
decrypted_content.innerHTML = content;
139144
encrypted_content.parentNode.removeChild(encrypted_content);
140145
// any post processing on the decrypted content should be done here
141-
{% if arithmatex %}
146+
{% if arithmatex -%}
142147
MathJax.typesetPromise()
143-
{% endif %}
144-
{% if hljs %}
148+
{%- endif %}
149+
{% if hljs -%}
145150
document.getElementById("mkdocs-decrypted-content").querySelectorAll('pre code').forEach((block) => {
146151
hljs.highlightBlock(block);
147152
});
148-
{% endif %}
153+
{%- endif %}
154+
{% if reload_scripts | length > 0 -%}
155+
let reload_scripts = {{ reload_scripts }}
156+
for (i = 0; i < reload_scripts.length; i++) { reload_js(reload_scripts[i]); };
157+
{%- endif %}
149158
} else {
150159
// create HTML element for the inform message
151160
var decrypt_msg = document.createElement('p');
@@ -162,7 +171,7 @@ <h1>{{ summary }}</h1>
162171
password_input.focus();
163172
}
164173
}
165-
{% if remember_password %}
174+
{% if remember_password -%}
166175
/* If remember_password is set, try to use cookie to decrypt content when page is loaded */
167176
var password_cookie = getCookie('encryptcontent')
168177
if (password_cookie) {
@@ -172,8 +181,8 @@ <h1>{{ summary }}</h1>
172181
decrypt_somethings();
173182
{% endif %}
174183
}
175-
{% endif %}
176-
{% if password_button %}
184+
{%- endif %}
185+
{% if password_button -%}
177186
if (decrypt_button) {
178187
decrypt_button.onclick = function(event) {
179188
event.preventDefault();
@@ -183,10 +192,10 @@ <h1>{{ summary }}</h1>
183192
{% endif %}
184193
};
185194
}
186-
{% endif %}
195+
{%- endif %}
187196
password_input.addEventListener('keydown', function(event) {
188197
if (event.key === "Enter") {
189-
{% if remember_password %}
198+
{% if remember_password -%}
190199
if (event.ctrlKey) {
191200
// set password on cookie with default path=/ (Overwrite specific cookie)
192201
// this cookie can by use on all page of your site
@@ -196,12 +205,12 @@ <h1>{{ summary }}</h1>
196205
// this cookie can only be use on this specific page of your site
197206
document.cookie = setCookie("encryptcontent", password_input.value, 1, document.location.pathname)
198207
}
199-
{% endif %}
208+
{%- endif %}
200209
event.preventDefault();
201210
decrypt_action();
202-
{% if encrypted_something %}
211+
{% if encrypted_something -%}
203212
decrypt_somethings();
204-
{% endif %}
213+
{%- endif %}
205214
}
206215
});
207216
};

encryptcontent/plugin.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class encryptContentPlugin(BasePlugin):
6262
('password_button_text', mkdocs.config.config_options.Type(string_types, default=str(settings['password_button_text']))),
6363
('encrypted_something', mkdocs.config.config_options.Type(dict, default={})),
6464
('decrypt_search', mkdocs.config.config_options.Type(bool, default=False)),
65+
('reload_scripts', mkdocs.config.config_options.Type(list, default=[])),
6566
)
6667

6768
def __hash_md5__(self, text):
@@ -108,6 +109,7 @@ def __encrypt_content__(self, content):
108109
'remember_password': self.remember_password,
109110
'disable_cookie_protection': self.disable_cookie_protection,
110111
'encrypted_something': self.encrypted_something,
112+
'reload_scripts': self.reload_scripts,
111113
})
112114
return decrypt_form
113115

@@ -185,7 +187,12 @@ def on_pre_build(self, config):
185187
setattr(self, 'decrypt_search', False)
186188
if 'decrypt_search' in plugin_config.keys():
187189
decrypt_search = self.config.get('decrypt_search')
188-
setattr(self, 'decrypt_search', decrypt_search)
190+
setattr(self, 'decrypt_search', decrypt_search)
191+
# Check if some script need to be reloaded after decryption process
192+
setattr(self, 'reload_scripts', [])
193+
if 'reload_scripts' in plugin_config.keys():
194+
reload_scripts = self.config.get('reload_scripts')
195+
setattr(self, 'reload_scripts', reload_scripts)
189196

190197
def on_page_markdown(self, markdown, page, config, **kwargs):
191198
"""

setup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def read(fname):
1111

1212
setup(
1313
name='mkdocs-encryptcontent-plugin',
14-
version='1.1.0',
14+
version='1.2.0',
1515
author='CoinK0in',
1616
author_email='[email protected]',
1717
description='A MkDocs plugin that encrypt/decrypt markdown content with AES',
@@ -35,7 +35,9 @@ def read(fname):
3535
'Programming Language :: Python',
3636
'Programming Language :: Python :: 3.5',
3737
'Programming Language :: Python :: 3.6',
38-
'Programming Language :: Python :: 3.7'
38+
'Programming Language :: Python :: 3.7',
39+
'Programming Language :: Python :: 3.8',
40+
'Programming Language :: Python :: 3.9'
3941
],
4042
packages=find_packages(exclude=['*.tests']),
4143
entry_points={

0 commit comments

Comments
 (0)