Skip to content

Commit ee36ae3

Browse files
authored
Add an aspect-ratio-helper mdbook preprocessor (#187)
The idea is that this will help us make better slides: we can see at a glance when the slide becomes too full.
1 parent c066ab5 commit ee36ae3

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

Diff for: aspect-ratio-helper.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright 2023 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""mdbook preprocessor which will help show the presentation aspect ratio.
18+
19+
The preprocessor adds a large red rectangle on every page. The
20+
rectangle has an aspect ratio of 16 to 8.5, meaning that it is
21+
slightly smaller than a standard 16/9 monitor.
22+
23+
The idea is that this approximates what the course participants can
24+
bee at once during a class. This in turn will help you estimate when
25+
the slides are too large to be seen without scrolling.
26+
27+
Enable it in book.toml.
28+
"""
29+
30+
import json
31+
import sys
32+
import textwrap
33+
34+
def update_book_items(items):
35+
aspect_ratio_helper = textwrap.dedent("""
36+
<style>
37+
div#aspect-ratio-helper {
38+
position: fixed;
39+
top: 8px;
40+
left: 8px;
41+
right: 8px;
42+
z-index: 1000;
43+
pointer-events: none;
44+
45+
}
46+
47+
div#aspect-ratio-helper div {
48+
outline: 3px dashed red;
49+
margin: 0 auto;
50+
/* At this width, the theme fonts are readable in a 16
51+
person conference room. If the browser is wider, the
52+
text becomes too small to be legible. */
53+
max-width: 980px;
54+
/* On a standard 16/9 monitor, we expect to lose a bit
55+
of vertical space to borders. */
56+
aspect-ratio: 16/8.5;
57+
}
58+
</style>
59+
60+
<div id="aspect-ratio-helper">
61+
<div></div>
62+
</div>
63+
64+
""")
65+
66+
for item in items:
67+
if type(item) != dict:
68+
continue
69+
70+
chapter = item.get('Chapter')
71+
if not chapter:
72+
continue
73+
74+
chapter['content'] = aspect_ratio_helper + chapter['content']
75+
update_book_items(chapter['sub_items'])
76+
77+
if __name__ == '__main__':
78+
if len(sys.argv) > 1:
79+
if sys.argv[1] == "supports":
80+
sys.exit(0)
81+
82+
context, book = json.load(sys.stdin)
83+
update_book_items(book['sections'])
84+
print(json.dumps(book))
85+

Diff for: book.toml

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ renderers = ["html"]
2121
renderers = ["html"]
2222
class = "bob"
2323

24+
# Enable this preprocessor to overlay a large red rectangle on the
25+
# pages. This will show you an estimate of what the course
26+
# participants can see during the presentation.
27+
#
28+
# [preprocessor.aspect-ratio-helper]
29+
# command = "./aspect-ratio-helper.py"
30+
2431
[output.html]
2532
curly-quotes = true
2633
additional-js = ["ga4.js", "speaker-notes.js"]

0 commit comments

Comments
 (0)