-
Notifications
You must be signed in to change notification settings - Fork 87
/
main.py
116 lines (107 loc) · 4.12 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
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
import os
import pprint
import re
import urllib.request
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", force_version=False):
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()
version = force_version or version
if isinstance(pages, str):
pages = [pages]
cards = []
for page in pages:
match = re.search("https://[^@/]+.ibexa.co", page)
if match:
with urllib.request.urlopen(page) as file:
content = file.read().decode('utf-8')
match = re.search("<meta property=\"og:title\" content=\"(.*)\"", content, re.MULTILINE)
if match:
title = match.groups()[0]
else:
match = re.search("<title>(.*)</title>", content, re.MULTILINE)
if match:
title = match.groups()[0]
else:
title = ""
match = re.search("<meta property=\"og:description\" content=\"(.*)\"", content, re.MULTILINE)
if match:
description = match.groups()[0]
else:
match = re.search("<meta name=\"description\" content=\"(.*)\"", content, re.MULTILINE)
if match:
description = match.groups()[0]
else:
description = ""
href = page
else:
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]
}
href = '/'.join((
'/',
site,
language,
version,
page
))
title = doc_meta['short'] or doc_meta['title']
description = doc_meta['description'] or " "
cards.append(
CARDS_TEMPLATE % (
href,
title,
description
)
)
return """<div class="%s col-%s">%s</div>""" % (style, columns, "\n".join(cards))