/
slideshow.py
202 lines (173 loc) · 6.82 KB
/
slideshow.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from browser import document, html, window
import markdown
import highlight
def escape(html):
html = html.replace('<', '<')
html = html.replace('>', '>')
return html
def _keydown(ev, path, zone, page):
if ev.keyCode in [39, 40]: # key right or down : next page
show(path, zone, page + 1)
ev.preventDefault()
elif ev.keyCode in [37, 38]: #key left or up: previous page
show(path, zone, page - 1)
ev.preventDefault()
def keydown(ev, slideshow, zone):
if ev.keyCode in [39, 40]: # key right or down : next page
slideshow.page_num += 1
if slideshow.page_num >= len(slideshow.pages):
slideshow.page_num = 0
show_page(slideshow, zone, slideshow.page_num)
ev.preventDefault()
elif ev.keyCode in [37, 38]: #key left or up: previous page
slideshow.page_num -= 1
if slideshow.page_num < 0:
slideshow.page_num = len(slideshow.pages) - 1
show_page(slideshow, zone, slideshow.page_num)
ev.preventDefault()
def move_to(ev, slideshow, zone):
pc = (ev.x - ev.target.abs_left) / ev.target.width
nb_pages = len(slideshow.pages) - 1
page = round(nb_pages * pc)
slideshow.page_num = page
new_pos = '%spx' %(ev.x -ev.target.abs_left -
(document['tl_pos'].width / 2))
# show page at specified position
show_page(slideshow, zone, page)
# set new cursor position
document['tl_pos'].style.left = new_pos
def click_on_tl_pos(ev):
# don't move if user clicks on current timeline position
ev.stopPropagation()
class Slideshow:
def __init__(self, path):
qs = window.Date.new().getTime()
self.src = src = open(path+'?foo=%s' %qs).read()
self.title = ''
self.show_page_num = False
# table of contents : matches matter with page number
self.contents = []
# directives for the document
while src.startswith('@'):
line_end = src.find('\n')
key,value = src[:line_end].split(' ', 1)
if key=='@title':
self.title = value
elif key=='@pagenum':
self.show_page_num = True
elif key=="@index":
self.contents.append([value, 0])
src = src[line_end + 1:]
self.pages = []
lines = []
for line in src.split('\n'):
if line.startswith('../..'):
self.pages.append('\n'.join(lines))
lines = []
elif line.startswith('@pause'):
self.pages.append('\n'.join(lines))
elif line.startswith('@index'):
self.contents.append([line.split(' ', 1)[1], len(self.pages)])
else:
lines.append(line)
if lines:
self.pages.append('\n'.join(lines))
def show(path, zone, page_num=None):
slideshow = Slideshow(path)
if page_num is None:
page_num = 0
# check if page num was stored in a cookie
cookies = document.cookie
if cookies:
elts = dict([x.strip() for x in cookie.split('=')]
for cookie in cookies.split(";"))
if "page" in elts:
page_num = int(elts["page"])
if page_num < 0:
page_num = 0
elif page_num >= len(slideshow.pages):
page_num = len(slideshow.pages) - 1
slideshow.page_num = page_num
document.unbind('keydown')
document.bind('keydown',lambda ev:keydown(ev, slideshow, zone))
show_page(slideshow, zone, page_num)
def run_code(ev):
print(ev.target.text)
def show_page(slideshow, zone, page_num):
# if table of contents is not empty, add it
if slideshow.contents:
toc = html.SELECT(name="toc")
toc.bind('change', lambda ev: show_page(slideshow, zone,
int(ev.target.options[ev.target.selectedIndex].value)))
for content in slideshow.contents:
toc <= html.OPTION(content[0], value=content[1],
selected=page_num>=content[1])
slideshow.page_num = int(page_num)
# store page num in a cookie
document.cookie = "page={}".format(page_num)
zone.clear()
body = html.DIV()
body.html = markdown.mark(slideshow.pages[page_num])[0]
wh = window.innerHeight
fontSize = int(18 * window.innerHeight / 800)
body.style.fontSize = "{}px".format(fontSize)
if slideshow.contents:
body = html.DIV(toc + body)
footer = html.DIV(Id="footer")
footer.style.fontSize = "{}px".format(fontSize)
if slideshow.title:
footer <= html.DIV(slideshow.title,style=dict(display='inline'))
if slideshow.show_page_num:
footer <= html.SPAN(' (%s/%s)' %(page_num+1, len(slideshow.pages)),
style=dict(display='inline'))
timeline = html.DIV(Id='timeline')
timeline.style.height = "{}px".format(int(fontSize/2))
tl_pos = html.DIV(Id='tl_pos')
timeline <= tl_pos
timeline.bind('click', lambda ev:move_to(ev, slideshow, zone))
tl_pos.bind('click', click_on_tl_pos)
zone <= body + footer + timeline
wh = window.innerHeight
tl_pos.style.left = '%spx' %(timeline.width*page_num/len(slideshow.pages))
document["cours"].style.minHeight = "{}px".format(int(wh * 0.9))
for elt in zone.get(selector='.python'):
src = elt.text.strip()
width = max(len(line) for line in src.split('\n'))
width = max(width, 30)
# replace element content by highlighted code
elt.html = highlight.highlight(src).html
elt.style.width = '%sem' %int(0.7*width)
elt.bind('click', run_code)
for elt in zone.get(selector='.python-console'):
src = elt.text.strip()
lines = src.split('\n')
result = ''
py = ''
py_starts = []
for line in lines:
if line.startswith('>>>') or line.startswith('...'):
py += line[4:]+'\n'
py_starts.append('<span class="python-prompt">{}</span>'.format(line[:3]))
else:
if py:
colored = highlight.highlight(py).html
colored_lines = colored.split('\n')
if result:
result += '\n'
result += '\n'.join(start+' '+line
for (start, line) in zip(py_starts, colored_lines))
py = ''
py_starts = []
else:
line = escape(line)
result += '\n' + line
if py:
colored = highlight.highlight(py).html
colored_lines = colored.split('\n')
if result:
result += '\n'
result += '\n'.join(start+' '+line
for (start, line) in zip(py_starts, colored_lines))
py = ''
py_starts = []
elt.html = result