forked from ibexa/documentation-developer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
88 lines (79 loc) · 2.76 KB
/
main.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
import os
import pprint
import re
from mkdocs.structure.pages import Page
from mkdocs.utils import meta
CARDS_TEMPLATE = """
<div class="card-wrapper">
<div>
<a href="/%s" class="card">
<div>
<p class="title">%s</p>
<p class="description">%s</p>
</div>
</a>
</div>
</div>
"""
def define_env(env):
"""
This is the hook for defining variables, macros and filters
- variables: the dictionary that contains the environment variables
- macro: a decorator function, to declare a macro.
"""
@env.macro
def include_file(filename, start_line=0, end_line=None, glue=''):
"""
Include a file,
optionally indicating start_line and end_line (start counting from 0)
optionally set a glue string to lead every string except the first one (can be used for indent)
The path is relative to the top directory of the documentation
project.
"""
full_filename = os.path.join(env.project_dir, filename)
with open(full_filename, 'r') as f:
lines = f.readlines()
line_range = lines[start_line:end_line]
return glue.join(line_range)
@env.macro
def cards(pages, columns=1, style="cards"):
current_page = env.variables.page
absolute_url = current_page.abs_url
canonical = current_page.canonical_url
url_parts = re.search("//([^/]+)/([^/]+)/([^/]+)/", canonical)
(site, language, version) = url_parts.groups()
if isinstance(pages, str):
pages = [pages]
cards = []
for page in pages:
with open("docs/%s.md" % page, "r") as doc_file:
doc = doc_file.read()
match = re.search("^# (.*)", doc, re.MULTILINE)
if match:
header = match.groups()[0]
else:
header = ""
default_meta = {
"title": header,
"short": "",
"description": ""
}
doc_meta = {
**default_meta,
**meta.get_data(doc)[1]
}
cards.append(
CARDS_TEMPLATE % (
'/'.join((
'',
site,
language,
version,
page
)),
doc_meta['short'] or doc_meta['title'],
doc_meta['description'] or " "
# site_url
)
)
return """<div class="%s col-%s">%s</div>""" % (style, columns, "\n".join(cards))