-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
112 lines (83 loc) · 3.7 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
# hint: you can get a lot of macro informations in your docs using:
# {{ macros_info() }}
def define_env(env):
# ==================================================================
@env.macro
def lorem():
"""Just print lorem ipsum."""
return """Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
"""
# ==================================================================
@env.macro
def csv2table(filename, dialect="excel"):
"""
Converts a csv file living relative to the current document
to a html table.
"""
import csv
try:
abs_filename = "/".join(env.page.file.abs_src_path.split("/")[0:-1] + [filename])
header = None
lines = []
with open(abs_filename, newline="") as csv_file:
my_reader = csv.reader(csv_file, dialect=dialect)
for row in my_reader:
if not header:
header = row
else:
lines.append(row)
header_html = "".join(f"<th>{s}</th>" for s in header)
lines_html = ""
for line in lines:
lines_html += "<tr>" + "".join(f"<td>{s.strip()}</td>" for s in line) + "</tr>"
return f"<table><thead><tr>{header_html}</tr></thead><tbody>{lines_html}</tbody></table>"
except:
return f"""<p style="color: black; background: red; padding: 1rem;"><b>Error reading CSV file {filename}.</b></p>"""
# ==================================================================
@env.macro
def xlsx2table(filename):
"""
Convert an excel file living relative to the current document
to a html table.
"""
try:
import openpyxl
from pathlib import Path
abs_filename = "/".join(env.page.file.abs_src_path.split("/")[0:-1] + [filename])
xlsx_file = Path(abs_filename)
workbook = openpyxl.load_workbook(xlsx_file)
sheet = workbook.active
# max_row = sheet.max_row
max_column = sheet.max_column
header = None
lines = []
for row in sheet.iter_rows():
if not header:
header = row
else:
lines.append(row)
header_html = "".join(f"<th>{s.value}</th>" for s in header[0:max_column])
lines_html = ""
for line in lines:
lines_html += "<tr>" + "".join(f"<td>{s.value}</td>" for s in line[0:max_column]) + "</tr>"
return f"<table><thead><tr>{header_html}</tr></thead><tbody>{lines_html}</tbody></table>"
except:
return f"""<p style="color: black; background: red; padding: 1rem;"><b>Error reading XLSX file {filename}.</b></p>"""
# ==================================================================
@env.macro
def begin_page():
"""
Opens a div.page the will be closed with a end().
There are some css rules to let the browser try to avoid page
breaks inside this div.page and put one behind, if need be.
"""
return '<div class="page" markdown="1">'
@env.macro
def end_page():
"""See begin()."""
return '</div><!-- .page -->'