Skip to content

Commit 5ffb107

Browse files
davidfischeragjohnson
authored andcommitted
More JS escapes (readthedocs#76)
* More JS escapes * ToJson requires values to be present -- throws otherwise * No need to duplicate the version check
1 parent 7b6c62b commit 5ffb107

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
var READTHEDOCS_DATA = {
2-
project: "{{ slug }}",
3-
version: "{{ version_slug }}",
4-
language: "{{ rtd_language }}",
5-
programming_language: "{{ programming_language }}",
2+
project: {{ slug|default('')|tojson }},
3+
version: {{ version_slug|default('')|tojson }},
4+
language: {{ rtd_language|default('')|tojson }},
5+
programming_language: {{ programming_language|default('')|tojson }},
66
subprojects: {},
7-
canonical_url: "{{ canonical_url }}",
8-
theme: "{{ html_theme }}",
7+
canonical_url: {{ canonical_url|default('')|tojson }},
8+
theme: {{ html_theme|default('')|tojson }},
99
builder: "sphinx",
10-
docroot: "{{ conf_py_path }}",
11-
source_suffix: "{{ source_suffix }}",
12-
api_host: "{{ api_host }}",
13-
commit: "{{ commit }}",
10+
docroot: {{ conf_py_path|default('')|tojson }},
11+
source_suffix: {{ source_suffix|default('')|tojson }},
12+
api_host: {{ api_host|default('')|tojson }},
13+
commit: {{ commit|default('')|tojson }},
1414
ad_free: {% if ad_free %}true{% else %}false{% endif %},
1515

16-
global_analytics_code: {% if global_analytics_code %}'{{ global_analytics_code }}'{% else %}null{% endif %},
17-
user_analytics_code: {% if user_analytics_code %}'{{ user_analytics_code }}'{% else %}null{% endif %}
16+
global_analytics_code: {% if global_analytics_code %}{{ global_analytics_code|default('')|tojson }}{% else %}null{% endif %},
17+
user_analytics_code: {% if user_analytics_code %}{{ user_analytics_code|default('')|tojson }}{% else %}null{% endif %}
1818
};
1919

2020
{% for slug, url in subprojects %}
21-
READTHEDOCS_DATA.subprojects["{{ slug }}"] = "{{ url }}";
21+
READTHEDOCS_DATA.subprojects[{{ slug|default('')|tojson }}] = {{ url|default('')|tojson }};
2222
{% endfor %}

readthedocs_ext/mixins.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ def copy_static_readthedocs_files(self):
5050
context=ctx,
5151
)
5252
else:
53+
from sphinx.util.template import SphinxRenderer
54+
renderer = SphinxRenderer()
55+
5356
copy_asset(
5457
path_src,
5558
path_dest,
5659
context=ctx,
57-
renderer=self.templates
60+
renderer=renderer,
5861
)
5962
log.info('done')
6063

tests/pyexample/conf.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@
2222
html_theme = 'alabaster'
2323
html_static_path = ['_static']
2424
htmlhelp_basename = 'pyexampledoc'
25+
26+
html_context = {
27+
'slug': 'test-slug',
28+
'rtd_language': 'en',
29+
'user_analytics_code': '',
30+
'global_analytics_code': "malic''ious",
31+
'commit': 'deadd00d',
32+
}

tests/test_integration.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,8 @@ def test_escape_js_vars(self):
9393
builder='readthedocs') as data:
9494
self.assertNotIn('escape \' this js', data)
9595
self.assertIn('escape\\u0027 this js', data)
96+
97+
with build_output('pyexample', '_build/readthedocs/_static/readthedocs-data.js',
98+
builder='readthedocs') as data:
99+
self.assertNotIn("malic''ious", data)
100+
self.assertIn('malic\\u0027\\u0027ious', data)

0 commit comments

Comments
 (0)