This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_pipe.py
68 lines (49 loc) · 2.02 KB
/
shell_pipe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import subprocess
from pelican import signals
from pelican.generators import ArticlesGenerator, PagesGenerator
def initialized(pelican):
from pelican.settings import DEFAULT_CONFIG
DEFAULT_CONFIG.setdefault('SHELL_BEGIN',
'<!-- SHELL_BEGIN -->')
DEFAULT_CONFIG.setdefault('SHELL_END',
'<!-- SHELL_END -->')
if pelican:
pelican.settings.setdefault('SHELL_BEGIN',
'<!-- SHELL_BEGIN -->')
pelican.settings.setdefault('SHELL_END',
'<!-- SHELL_END -->')
def run_pipes(instance):
if not instance._content:
return
content = instance._update_content(instance._content, instance.settings['SITEURL'])
shell_begin_index = content.find(instance.settings['SHELL_BEGIN'])
shell_end_index = content.find(instance.settings['SHELL_END'])
code_start = shell_begin_index + len(instance.settings['SHELL_BEGIN'])
code_end = shell_end_index
if code_start < 0 or code_end < 0:
return
code_str = content[code_start: code_end].strip()
if not len(code_str):
return
output = subprocess.check_output(code_str, shell=True).decode('utf-8')
instance._content = content.replace(
content[
shell_begin_index:
shell_end_index + len(instance.settings['SHELL_END'])
],
"<div class=\"shell-pipe\"><p class=\"code\">%s</p><p class=\"output\">%s</p>"% (code_str, output)
)
def run_plugin(generators):
for generator in generators:
if isinstance(generator, ArticlesGenerator):
for article in generator.articles:
run_pipes(article)
elif isinstance(generator, PagesGenerator):
for page in generator.pages:
run_pipes(page)
def register():
signals.initialized.connect(initialized)
try:
signals.all_generators_finalized.connect(run_plugin)
except AttributeError:
signals.content_object_init.connect(run_pipes)