forked from bildung6/bildung6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_hooks.py
139 lines (108 loc) · 4.33 KB
/
my_hooks.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
import re
import yaml
from bs4 import BeautifulSoup
from jinja2 import Environment, BaseLoader
class Global:
nav = None
config = None
cache = {}
def load_block(block_name):
content = ""
# check if file exists
if not os.path.isfile("docs/overrides/blocks/" + block_name):
block_name = "block.html"
with open("docs/overrides/blocks/" + block_name) as f:
content = f.read()
env = Environment(loader=BaseLoader)
env.filters['url'] = url
rtemplate = env.from_string(content)
return rtemplate
def on_nav(Navigation, config, files):
desired_order = ["", "Quests", "Chancen & Risiken", "Guides", "Leitlinien", "Tools", 'Über das Projekt', "AnwenderInnen", "Anwendungsfälle"]
for section in Navigation:
if section.title == "Chancenrisiken":
section.title = 'Chancen & Risiken'
if section.title == "Projekt":
section.title = 'Über das Projekt'
title_to_element = {}
for element in Navigation.items:
if element.title is None:
title_to_element[""] = element
else:
title_to_element[element.title] = element
reordered_list = []
for title in desired_order:
if title in title_to_element:
reordered_list.append(title_to_element[title])
Navigation.items = reordered_list
Global.nav = Navigation
Global.config = config
return Navigation
def on_page_markdown(markdown, page, config, files):
if page.meta.get('type') is not None:
if os.path.isfile("docs/overrides/blocks/single/" + page.meta.get('type') + ".html"):
page.meta["template"] = "blocks/single/" + page.meta.get('type') + ".html"
return markdown
def filter_entities(entities, filter_dict):
op_map = {
'equals': lambda x, y: x == y,
'greater_than': lambda x, y: x > y,
'less_than': lambda x, y: x < y,
'contains': lambda x, y: y in x,
'matches': lambda x, y: re.search(y, x)
}
def match(entity, rule):
prop = rule['property']
condition = rule.get('condition', 'and')
value = rule['value']
return op_map[condition](entity.meta.get(prop, ""), value)
def evaluate(entity, filter_group):
if 'property' in filter_group:
return match(entity, filter_group)
if len(filter_group['rules']) == 0:
return True
results = [evaluate(entity, rule) for rule in filter_group['rules']]
return all(results) if filter_group.get("condition", "and") == 'and' else any(results)
return [entity for entity in entities if
evaluate(entity, filter_dict) and entity.meta.get('type', '') == filter_dict.get('entityType')]
def on_post_page(html, page, config):
pages = get_children(Global.nav)
# use html parser to find all blocks in html
parsed = BeautifulSoup(html, 'html.parser')
blocks = parsed.find_all('code')
for block in blocks:
if block.text.startswith('yaml') and block.text.find('entityType') > -1:
parent = block.parent
# remove yaml from block text
text = block.text.replace('yaml\n', '')
# parse yaml
yaml_data = yaml.safe_load(text)
filtered_pages = filter_entities(pages, yaml_data)
block_template = load_block(yaml_data.get("entityType", "block") + '.html')
block_render = block_template.render(elements=filtered_pages)
parent.replace_with(BeautifulSoup(block_render, 'html.parser'))
elif block.text.startswith("yaml") and block.text.find("question") > -1:
parent = block.parent
text = block.text.replace('yaml\n', '')
yaml_data = yaml.safe_load(text)
block_template = load_block('question.html')
block_render = block_template.render(yaml_data)
parent.replace_with(BeautifulSoup(block_render, 'html.parser'))
return parsed.prettify()
def url(path):
return Global.config['site_url'] + path
def get_children(list):
pages = []
for item in list:
if item.children:
pages.extend(get_children(item.children))
else:
pages.append(item)
return pages
def filter_by_meta(pages, tag, value):
list = []
for page in pages:
if page.meta.get(tag) == value:
list.append(page)
return list