forked from moppermonster/kiosky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
144 lines (126 loc) · 4.12 KB
/
utils.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
140
141
142
143
'''renderers, channels and homepage'''
from markdown import markdown
import requests
from flask import make_response, jsonify
def make_html(url, time):
"""
Return browser readable html that loads url in an iframe and refreshes after time seconds
"""
html = f"""
<!doctype html>
<html>
<head>
<title>Kiosk</title>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<meta http-equiv="refresh" content={time}>
</head>
<body style="margin:0px;padding:0px;overflow:hidden">
<iframe src= {url} frameborder="0" style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:100%;width:100%;position:absolute;top:0px;left:0px;right:0px;bottom:0px" height="100%" width="100%"></iframe>
</body>
</html>
"""
return html
def make_config(name, time, position, pages):
"""
Return channel config page as browser readable html
"""
html = f"""
<!doctype html>
<html>
<head>
<title>Kiosk {name} config</title>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
</head>
<body>
Channel name/time/pages<br>
<form method="POST">
<input name="channel-name", value={name}>
<input name="channel-time", value={time}>
<input name="channel-pages", value={str(pages)}, size=300>
<input type="submit">
</form>
Channel position<br>
{position} <br>
</body>
</html>
"""
return html
def make_new():
"""
Return channel config page as browser readable html
"""
html = f"""
<!doctype html>
<html>
<head>
<title>Kiosk new page</title>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
</head>
<body>
Channel name<br>
<form method="POST">
<input name="channel-name">
<input type="submit">
</form>
</body>
</html>
"""
return html
def build_markdown(title, content):
'''
Return the content as markdown formatted html with title title
'''
content = markdown(content, extensions=['markdown.extensions.extra'])
fonts = '<link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Source+Code+Pro" />'
head = f'<html><head><title>{title}</title> {fonts} <link rel="stylesheet" href="/static/css/kiosky.css"></head>'
page = head + '\n\n<body>\n' + content + '\n</body>\n</html>'
return page
def home(channels):
'''
Returns the homepage as html
'''
raw = '# Kiosky\n\n'
if not channels:
raw = raw + '\n\n' + '> No available channels!' + '\n\n'
return build_markdown('kiosky', raw)
raw = raw + '\n'
raw = raw + '## channels\n'
for entry in channels:
name = entry
time = channels[entry][0]
position = channels[entry][1]
pages = channels[entry][2]
raw = raw + f'\n### {name}'
raw = raw + f'\n[view](/channels/{name}) [edit](/config/{name})'
raw = raw + f'\n#### Time: {time} seconds'
raw = raw + f'\n#### Position: {position}/{len(pages)-1}'
for page in pages:
raw = raw + f'\n- {page}\n'
raw = raw + '\n[Add channel](/new)'
return build_markdown('kiosky', raw)
def channel_page(channel, channels):
'''
Returns html for channel
'''
channel = channels[channel]
time = channel[0]
position = channel[1]
pages = channel[2]
page = pages[position]
return make_html(page, time)
def config_page(channel, channels):
'''
Returns html for channel config page
'''
name = channel
channel = channels[channel]
time = channel[0]
position = channel[1]
pages = channel[2]
pages_string = ','.join(pages)
return make_config(name, time, position, pages_string)
# def add_page():
# '''
# Returns html for channel config page
# '''
# return make_config(name, time, position, pages)