Skip to content

Commit 27cf105

Browse files
committed
chore(convert): improve HTML Jinja2 template
As suggested by @yunusey in #442
1 parent c0a240d commit 27cf105

File tree

2 files changed

+52
-108
lines changed

2 files changed

+52
-108
lines changed

manim_slides/convert.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ def prefix(i: int) -> str:
430430
dest.parent.mkdir(parents=True, exist_ok=True)
431431

432432
with open(dest, "w") as f:
433-
revealjs_template = Template(self.load_template())
433+
revealjs_template = Template(self.load_template(), trim_blocks=True, lstrip_blocks=True)
434434

435435
options = self.dict()
436436
options["assets_dir"] = assets_dir

manim_slides/templates/revealjs.html

+51-107
Original file line numberDiff line numberDiff line change
@@ -19,82 +19,75 @@
1919
<body>
2020
<div class="reveal">
2121
<div class="slides">
22-
{%- for presentation_config in presentation_configs -%}
23-
{% set outer_loop = loop %}
24-
{%- for slide_config in presentation_config.slides -%}
25-
{%- if data_uri -%}
22+
{% for presentation_config in presentation_configs %}
23+
{%- set outer_loop = loop -%}
24+
{% for slide_config in presentation_config.slides %}
25+
{% if data_uri %}
2626
{% set file = file_to_data_uri(slide_config.file) %}
27-
{%- else -%}
27+
{% else %}
2828
{% set file = assets_dir / slide_config.file.name %}
29-
{%- endif -%}
29+
{% endif %}
3030
<section
3131
data-background-size={{ background_size }}
3232
data-background-color="{{ presentation_config.background_color }}"
3333
data-background-video="{{ file }}"
34-
{% if loop.index == 1 and outer_loop.index == 1 -%}
34+
{% if loop.index == 1 and outer_loop.index == 1 %}
3535
data-background-video-muted
36-
{%- endif -%}
37-
{% if slide_config.loop -%}
36+
{% endif %}
37+
{% if slide_config.loop %}
3838
data-background-video-loop
39-
{%- endif -%}
40-
{% if slide_config.auto_next -%}
39+
{% endif %}
40+
{% if slide_config.auto_next %}
4141
data-autoslide="{{ get_duration_ms(slide_config.file) }}"
42-
{%- endif -%}>
43-
{% if slide_config.notes != "" -%}
42+
{% endif %}
43+
>
44+
{% if slide_config.notes != "" %}
4445
<aside class="notes" data-markdown>{{ slide_config.notes }}</aside>
45-
{%- endif %}
46+
{% endif %}
4647
</section>
47-
{%- endfor -%}
48-
{%- endfor -%}
48+
{% endfor %}
49+
{% endfor %}
4950
</div>
5051
</div>
5152

5253
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/{{ reveal_version }}/reveal.min.js"></script>
5354

5455
<!-- To include plugins, see: https://revealjs.com/plugins/ -->
55-
56-
{% if has_notes -%}
56+
{% if has_notes %}
5757
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/{{ reveal_version }}/plugin/markdown/markdown.min.js"></script>
5858
<script src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/{{ reveal_version }}/plugin/notes/notes.min.js"></script>
59-
{%- endif -%}
59+
{% endif %}
6060

6161
<!-- <script src="index.js"></script> -->
6262
<script>
6363
Reveal.initialize({
64-
{% if has_notes -%}
64+
{% if has_notes %}
65+
/// The list of RevealJS plugins.
6566
plugins: [ RevealMarkdown, RevealNotes ],
66-
{%- endif %}
67+
{% endif %}
6768
// The "normal" size of the presentation, aspect ratio will
6869
// be preserved when the presentation is scaled to fit different
6970
// resolutions. Can be specified using percentage units.
7071
width: {{ width }},
7172
height: {{ height }},
72-
7373
// Factor of the display size that should remain empty around
7474
// the content
7575
margin: {{ margin }},
76-
7776
// Bounds for smallest/largest possible scale to apply to content
7877
minScale: {{ min_scale }},
7978
maxScale: {{ max_scale }},
80-
8179
// Display presentation control arrows
8280
controls: {{ controls }},
83-
8481
// Help the user learn the controls by providing hints, for example by
8582
// bouncing the down arrow when they first encounter a vertical slide
8683
controlsTutorial: {{ controls_tutorial }},
87-
8884
// Determines where controls appear, "edges" or "bottom-right"
8985
controlsLayout: {{ controls_layout }},
90-
9186
// Visibility rule for backwards navigation arrows; "faded", "hidden"
9287
// or "visible"
9388
controlsBackArrows: {{ controls_back_arrows }},
94-
9589
// Display a presentation progress bar
9690
progress: {{ progress }},
97-
9891
// Display the page number of the current slide
9992
// - true: Show slide number
10093
// - false: Hide slide number
@@ -110,55 +103,41 @@
110103
// object and return an array with one string [slideNumber] or
111104
// three strings [n1,delimiter,n2]. See #formatSlideNumber().
112105
slideNumber: {{ slide_number }},
113-
114106
// Can be used to limit the contexts in which the slide number appears
115107
// - "all": Always show the slide number
116108
// - "print": Only when printing to PDF
117109
// - "speaker": Only in the speaker view
118110
showSlideNumber: {{ show_slide_number }},
119-
120111
// Use 1 based indexing for # links to match slide number (default is zero
121112
// based)
122113
hashOneBasedIndex: {{ hash_one_based_index }},
123-
124114
// Add the current slide number to the URL hash so that reloading the
125115
// page/copying the URL will return you to the same slide
126116
hash: {{ hash }},
127-
128117
// Flags if we should monitor the hash and change slides accordingly
129118
respondToHashChanges: {{ respond_to_hash_changes }},
130-
131-
// Push each slide change to the browser history. Implies `hash: true`
119+
// Push each slide change to the browser history. Implies `hash: true`
132120
history: {{ history }},
133-
134121
// Enable keyboard shortcuts for navigation
135122
keyboard: {{ keyboard }},
136-
137123
// Optional function that blocks keyboard events when retuning false
138124
//
139125
// If you set this to 'focused', we will only capture keyboard events
140126
// for embedded decks when they are in focus
141127
keyboardCondition: {{ keyboard_condition }},
142-
143128
// Disables the default reveal.js slide layout (scaling and centering)
144129
// so that you can use custom CSS layout
145130
disableLayout: {{ disable_layout }},
146-
147131
// Enable the slide overview mode
148132
overview: {{ overview }},
149-
150133
// Vertical centering of slides
151134
center: {{ center }},
152-
153135
// Enables touch navigation on devices with touch input
154136
touch: {{ touch }},
155-
156137
// Loop the presentation
157138
loop: {{ loop }},
158-
159139
// Change the presentation direction to be RTL
160140
rtl: {{ rtl }},
161-
162141
// Changes the behavior of our navigation directions.
163142
//
164143
// "default"
@@ -184,168 +163,133 @@
184163
// from 1.3 -> 2.1. If "grid" is used, the same navigation takes you
185164
// from 1.3 -> 2.3.
186165
navigationMode: {{ navigation_mode }},
187-
188166
// Randomizes the order of slides each time the presentation loads
189167
shuffle: {{ shuffle }},
190-
191168
// Turns fragments on and off globally
192169
fragments: {{ fragments }},
193-
194170
// Flags whether to include the current fragment in the URL,
195171
// so that reloading brings you to the same fragment position
196172
fragmentInURL: {{ fragment_in_url }},
197-
198173
// Flags if the presentation is running in an embedded mode,
199174
// i.e. contained within a limited portion of the screen
200175
embedded: {{ embedded }},
201-
202176
// Flags if we should show a help overlay when the question-mark
203177
// key is pressed
204178
help: {{ help }},
205-
206179
// Flags if it should be possible to pause the presentation (blackout)
207180
pause: {{ pause }},
208-
209181
// Flags if speaker notes should be visible to all viewers
210182
showNotes: {{ show_notes }},
211-
212183
// Global override for autolaying embedded media (video/audio/iframe)
213184
// - null: Media will only autoplay if data-autoplay is present
214185
// - true: All media will autoplay, regardless of individual setting
215186
// - false: No media will autoplay, regardless of individual setting
216187
autoPlayMedia: {{ auto_play_media }},
217-
218188
// Global override for preloading lazy-loaded iframes
219189
// - null: Iframes with data-src AND data-preload will be loaded when within
220190
// the viewDistance, iframes with only data-src will be loaded when visible
221191
// - true: All iframes with data-src will be loaded when within the viewDistance
222192
// - false: All iframes with data-src will be loaded only when visible
223193
preloadIframes: {{ preload_iframes }},
224-
225194
// Can be used to globally disable auto-animation
226195
autoAnimate: {{ auto_animate }},
227-
228196
// Optionally provide a custom element matcher that will be
229197
// used to dictate which elements we can animate between.
230198
autoAnimateMatcher: {{ auto_animate_matcher }},
231-
232199
// Default settings for our auto-animate transitions, can be
233200
// overridden per-slide or per-element via data arguments
234201
autoAnimateEasing: {{ auto_animate_easing }},
235202
autoAnimateDuration: {{ auto_animate_duration }},
236203
autoAnimateUnmatched: {{ auto_animate_unmatched }},
237-
238204
// CSS properties that can be auto-animated. Position & scale
239205
// is matched separately so there's no need to include styles
240206
// like top/right/bottom/left, width/height or margin.
241207
autoAnimateStyles: {{ auto_animate_styles }},
242-
243208
// Controls automatic progression to the next slide
244209
// - 0: Auto-sliding only happens if the data-autoslide HTML attribute
245210
// is present on the current slide or fragment
246211
// - 1+: All slides will progress automatically at the given interval
247212
// - false: No auto-sliding, even if data-autoslide is present
248213
autoSlide: {{ auto_slide }},
249-
250214
// Stop auto-sliding after user input
251215
autoSlideStoppable: {{ auto_slide_stoppable }},
252-
253216
// Use this method for navigation when auto-sliding (defaults to navigateNext)
254217
autoSlideMethod: {{ auto_slide_method }},
255-
256218
// Specify the average time in seconds that you think you will spend
257219
// presenting each slide. This is used to show a pacing timer in the
258220
// speaker view
259221
defaultTiming: {{ default_timing }},
260-
261222
// Enable slide navigation via mouse wheel
262223
mouseWheel: {{ mouse_wheel }},
263-
264224
// Opens links in an iframe preview overlay
265225
// Add `data-preview-link` and `data-preview-link="false"` to customize each link
266226
// individually
267227
previewLinks: {{ preview_links }},
268-
269228
// Exposes the reveal.js API through window.postMessage
270229
postMessage: {{ post_message }},
271-
272230
// Dispatches all reveal.js events to the parent window through postMessage
273231
postMessageEvents: {{ post_message_events }},
274-
275232
// Focuses body when page changes visibility to ensure keyboard shortcuts work
276233
focusBodyOnPageVisibilityChange: {{ focus_body_on_page_visibility_change }},
277-
278234
// Transition style
279235
transition: {{ transition }}, // none/fade/slide/convex/concave/zoom
280-
281236
// Transition speed
282237
transitionSpeed: {{ transition_speed }}, // default/fast/slow
283-
284238
// Transition style for full page slide backgrounds
285239
backgroundTransition: {{ background_transition }}, // none/fade/slide/convex/concave/zoom
286-
287240
// The maximum number of pages a single slide can expand onto when printing
288241
// to PDF, unlimited by default
289242
pdfMaxPagesPerSlide: {{ pdf_max_pages_per_slide }},
290-
291243
// Prints each fragment on a separate slide
292244
pdfSeparateFragments: {{ pdf_separate_fragments }},
293-
294245
// Offset used to reduce the height of content within exported PDF pages.
295246
// This exists to account for environment differences based on how you
296247
// print to PDF. CLI printing options, like phantomjs and wkpdf, can end
297248
// on precisely the total height of the document whereas in-browser
298249
// printing has to end one pixel before.
299250
pdfPageHeightOffset: {{ pdf_page_height_offset }},
300-
301251
// Number of slides away from the current that are visible
302252
viewDistance: {{ view_distance }},
303-
304253
// Number of slides away from the current that are visible on mobile
305254
// devices. It is advisable to set this to a lower number than
306255
// viewDistance in order to save resources.
307256
mobileViewDistance: {{ mobile_view_distance }},
308-
309257
// The display mode that will be used to show slides
310258
display: {{ display }},
311-
312259
// Hide cursor if inactive
313260
hideInactiveCursor: {{ hide_inactive_cursor }},
314-
315261
// Time before the cursor is hidden (in ms)
316262
hideCursorTime: {{ hide_cursor_time }}
317263
});
318-
319-
{% if data_uri -%}
320-
// Fix found by @t-fritsch on GitHub
321-
// see: https://github.com/hakimel/reveal.js/discussions/3362#discussioncomment-6651475.
322-
function fixBase64VideoBackground(event) {
323-
// event.previousSlide, event.currentSlide, event.indexh, event.indexv
324-
if (event.currentSlide.getAttribute('data-background-video')) {
325-
const background = Reveal.getSlideBackground(event.indexh, event.indexv),
326-
video = background.querySelector('video'),
327-
sources = video.querySelectorAll('source');
328-
329-
sources.forEach((source, i) => {
330-
const src = source.getAttribute('src');
331-
if(src.match(/^data:video.*;base64$/)) {
332-
const nextSrc = sources[i+1]?.getAttribute('src');
333-
video.setAttribute('src', `${src},${nextSrc}`);
334-
}
335-
});
264+
{% if data_uri %}
265+
// Fix found by @t-fritsch on GitHub
266+
// see: https://github.com/hakimel/reveal.js/discussions/3362#discussioncomment-6651475.
267+
function fixBase64VideoBackground(event) {
268+
// event.previousSlide, event.currentSlide, event.indexh, event.indexv
269+
if (event.currentSlide.getAttribute('data-background-video')) {
270+
const background = Reveal.getSlideBackground(event.indexh, event.indexv),
271+
video = background.querySelector('video'),
272+
sources = video.querySelectorAll('source');
273+
274+
sources.forEach((source, i) => {
275+
const src = source.getAttribute('src');
276+
if(src.match(/^data:video.*;base64$/)) {
277+
const nextSrc = sources[i+1]?.getAttribute('src');
278+
video.setAttribute('src', `${src},${nextSrc}`);
279+
}
280+
});
281+
}
336282
}
337-
}
338-
Reveal.on( 'ready', fixBase64VideoBackground );
339-
Reveal.on( 'slidechanged', fixBase64VideoBackground );
340-
{%- endif %}
283+
Reveal.on( 'ready', fixBase64VideoBackground );
284+
Reveal.on( 'slidechanged', fixBase64VideoBackground );
285+
{% endif %}
341286
</script>
342-
343-
{% if env['READTHEDOCS'] -%}
344-
<style>
345-
readthedocs-flyout, readthedocs-notification {
346-
display: none;
347-
}
348-
</style>
349-
{%- endif %}
287+
{% if env['READTHEDOCS'] %}
288+
<style>
289+
readthedocs-flyout, readthedocs-notification {
290+
display: none;
291+
}
292+
</style>
293+
{% endif %}
350294
</body>
351295
</html>

0 commit comments

Comments
 (0)