Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit f7bbffb

Browse files
authored
Merge branch 'master' into accessibility_fixes
2 parents 428cc0b + a353996 commit f7bbffb

18 files changed

Lines changed: 177 additions & 35 deletions

File tree

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Declare files that must always have LF line endings on checkout so docker compose works on Windows
2+
* eol=lf

conf.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@
319319
# This is the file name suffix for HTML files (e.g. ".xhtml").
320320
# html_file_suffix = None
321321

322+
# It True, sets js files from Sphinx & Runestone to be loaded with defer attr
323+
# substantially speeding up page rendering. May cause issues with books that
324+
# have custom directives or raw html that assume jquery or another library
325+
# is loaded before body is parsed.
326+
html_defer_js = True
327+
322328
# Output file base name for HTML help builder.
323329
htmlhelp_basename = "PythonCoursewareProjectdoc"
324330

runestone/__init__.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import os, socket, pkg_resources
3131
import CodeChat.CodeToRest
3232
from sphinx.errors import ExtensionError
33+
from sphinx.builders.html import JavaScript
3334

3435

3536
# TODO: clean up - many of the folders are not needed as the files are imported by webpack
@@ -75,6 +76,26 @@ def runestone_extensions():
7576
modules.insert(0, modules.pop(modules.index("runestone.common")))
7677
return modules
7778

79+
# setup_js_defer(app, pagename, templatexname, context, doctree)
80+
# -----------------------
81+
# Used to inspect js right before it is rendered to page so that
82+
# we can forcibly defer js files or prevent same
83+
def setup_js_defer(app, pagename, templatexname, context, doctree):
84+
def js_defer(script_files):
85+
for js in sorted(script_files):
86+
if app.config.html_defer_js:
87+
# Files added from Runestone should already have defer set - so just add it to sphinx based ones
88+
to_defer = ["_static/jquery.js", "_static/underscore.js","_static/doctools.js"]
89+
if isinstance(js, JavaScript) and js in to_defer:
90+
js.attributes["defer"] = ""
91+
else:
92+
#config flag not set, prevent all deferrals
93+
if isinstance(js, JavaScript):
94+
js.attributes.pop("defer", None)
95+
return ''
96+
97+
context['js_defer'] = js_defer
98+
7899

79100
# setup(app)
80101
# ----------
@@ -94,16 +115,18 @@ def setup(app):
94115

95116
for jsfile in script_files:
96117
try:
97-
app.add_autoversioned_javascript(jsfile)
118+
app.add_autoversioned_javascript(jsfile, defer="")
98119
except ExtensionError:
99-
app.add_js_file(jsfile)
120+
app.add_js_file(jsfile, defer="")
100121
for cssfile in _css_files:
101122
try:
102123
app.add_autoversioned_stylesheet(cssfile)
103124
except ExtensionError:
104125
app.add_css_file(cssfile)
105126

106127
app.config.html_static_path.append("dist/")
128+
app.add_config_value("html_defer_js", False, 'env')
129+
app.connect('html-page-context', setup_js_defer)
107130

108131

109132
def get_master_url():

runestone/animation/animation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
def setup(app):
2727
app.add_directive("animation", Animation)
28-
app.add_autoversioned_javascript("animationbase.js")
28+
app.add_autoversioned_javascript("animationbase.js", defer="")
2929

3030

3131
SRC = """

runestone/animation/chart.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// $("#visualization").gchart({type: 'graphviz', series: [$.gchart.series([20, 50, 30])]});
1919
// label: '<f0> left | <f1> middle | <f2> right'
2020

21-
$('#visualization').gchart($.gchart.graphviz(true,
21+
document.addEventListener('load', (event) => {
22+
$('#visualization').gchart($.gchart.graphviz(true,
2223
{
2324
struct1: {label: '<f0> left |<f1> middle |<f2> right'},
2425
struct2: {label: '<f0> one|<f1> two'},
@@ -32,6 +33,7 @@
3233
node: {shape: 'record'}
3334
}
3435
));
36+
});
3537

3638

3739
// $('#visualization').gchart($.gchart.graphviz(true,

runestone/animation/jqchart/gChartBasic.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
</style>
99
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
1010
<script type="text/javascript" src="jquery.gchart.js"></script>
11-
<script type="text/javascript">
11+
<script>
12+
document.addEventListener('load', (event) => {
1213
$(function () {
1314
$('#basicGChart').gchart({type: 'line', maxValue: 40,
1415
title: 'Weather for|Brisbane, Australia', titleColor: 'green',
@@ -27,6 +28,7 @@
2728
$.gchart.axis('right', ['mm'], [50], 'blue', 'left')],
2829
legend: 'right'});
2930
});
31+
});
3032
</script>
3133
</head>
3234
<body>

runestone/common/project_template/_templates/plugin_layouts/sphinx_bootstrap/layout.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
<!DOCTYPE html>
1717
{%- endblock %}
1818

19+
{%- block scripts %}
20+
{# Override the scripts block from sphinx so we can force loading of jquery/underscore/doctools js files #}
21+
{{ js_defer(script_files) }}
22+
{{ super() }}
23+
{%- endblock %}
24+
1925
{# Sidebar: Rework into our Boostrap nav section. #}
2026
{% macro navBar() %}
2127

@@ -386,6 +392,10 @@
386392
{% endif %}
387393
{% endif %}
388394

389-
<script> runestoneComponents.getSwitch(); </script>
395+
<script>
396+
document.addEventListener('load', (event) => {
397+
runestoneComponents.getSwitch();
398+
});
399+
</script>
390400

391401
{% endblock %}

runestone/common/project_template/_templates/plugin_layouts/sphinx_bootstrap/relations.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
'delay': { show: 100, hide: 50}
2626
};
2727

28-
$('#relations-prev').tooltip(opts);
29-
$('#relations-next').tooltip(opts);
28+
document.addEventListener('load', (event) => {
29+
$('#relations-prev').tooltip(opts);
30+
$('#relations-next').tooltip(opts);
31+
});
3032
</script>

runestone/common/project_template/_templates/plugin_layouts/sphinx_bootstrap/subchapter.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
{%- endif %}
2626

2727
<script type="text/javascript">
28-
28+
document.addEventListener('load', (event) => {
2929
$('#relations-prev').tooltip({'placement':'right', 'selector': '', 'delay': { show: 100, hide: 50}});
3030
$('#relations-next').tooltip({'placement':'left', 'selector': '', 'delay': { show: 100, hide: 50}});
31-
31+
});
3232
</script>
3333

3434
<script>

runestone/common/project_template/conf.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ html_show_sourcelink = False
275275
# This is the file name suffix for HTML files (e.g. ".xhtml").
276276
#html_file_suffix = None
277277

278+
# It True, sets js files from Sphinx & Runestone to be loaded with defer attr
279+
# substantially speeding up page rendering. May cause issues with books that
280+
# have custom directives or raw html that assume jquery or another library
281+
# is loaded before body is parsed.
282+
html_defer_js = True
283+
278284
# Output file base name for HTML help builder.
279285
htmlhelp_basename = 'PythonCoursewareProjectdoc'
280286

0 commit comments

Comments
 (0)