Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit b164cb2

Browse files
committed
provide setting for choosing between minified and unminified files
1 parent 2746b35 commit b164cb2

File tree

3 files changed

+80
-29
lines changed

3 files changed

+80
-29
lines changed

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
prune common
22
include LICENSE
3+
include sphinx_search/_static/js/rtd_sphinx_search.js
34
include sphinx_search/_static/js/rtd_sphinx_search.min.js
5+
include sphinx_search/_static/css/rtd_sphinx_search.css
46
include sphinx_search/_static/css/rtd_sphinx_search.min.css

sphinx_search/extension.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
11
import os
2+
23
from sphinx.util.fileutil import copy_asset
34

45

5-
CUSTOM_ASSETS_FILES = [
6-
os.path.join('js', 'rtd_sphinx_search.min.js'),
7-
os.path.join('css', 'rtd_sphinx_search.min.css'),
8-
]
6+
CUSTOM_ASSETS_FILES = {
7+
'MINIFIED': [
8+
os.path.join('js', 'rtd_sphinx_search.min.js'),
9+
os.path.join('css', 'rtd_sphinx_search.min.css'),
10+
],
11+
'UN_MINIFIED': [
12+
os.path.join('js', 'rtd_sphinx_search.js'),
13+
os.path.join('css', 'rtd_sphinx_search.css'),
14+
]
15+
}
916

1017

1118
def copy_asset_files(app, exception):
1219
if exception is None: # build succeeded
13-
for f in CUSTOM_ASSETS_FILES:
14-
path = os.path.join(os.path.dirname(__file__), '_static', f)
15-
copy_asset(path, os.path.join(app.outdir, '_static', f.split('.')[-1]))
20+
files = CUSTOM_ASSETS_FILES['MINIFIED'] + CUSTOM_ASSETS_FILES['UN_MINIFIED']
21+
for file in files:
22+
path = os.path.join(os.path.dirname(__file__), '_static', file)
23+
copy_asset(path, os.path.join(app.outdir, '_static', file.split('.')[-1]))
1624

1725

18-
def setup(app):
26+
def inject_static_files(app):
27+
"""Inject correct CSS and JS files based on the value of ``RTD_SPHINX_SEARCH_FILE_TYPE``."""
28+
file_type = app.config.RTD_SPHINX_SEARCH_FILE_TYPE
29+
files = CUSTOM_ASSETS_FILES[file_type]
1930

20-
app.connect('build-finished', copy_asset_files)
21-
22-
for file in CUSTOM_ASSETS_FILES:
23-
if file.endswith('.min.js'):
31+
for file in files:
32+
if file.endswith('.js'):
2433
app.add_js_file(file)
25-
if file.endswith('.min.css'):
34+
elif file.endswith('.css'):
2635
app.add_css_file(file)
36+
37+
38+
def setup(app):
39+
40+
app.add_config_value('RTD_SPHINX_SEARCH_FILE_TYPE', 'UN_MINIFIED', 'html')
41+
42+
app.connect('builder-inited', inject_static_files)
43+
app.connect('build-finished', copy_asset_files)

tests/test_extension.py

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77

88
from tests import TEST_DOCS_SRC
9+
from sphinx_search.extension import CUSTOM_ASSETS_FILES
910

1011

1112
@pytest.mark.sphinx(srcdir=TEST_DOCS_SRC)
@@ -14,31 +15,62 @@ def test_static_files_exists(app, status, warning):
1415
app.build()
1516
path = app.outdir
1617

17-
js_file = os.path.join(path, '_static', 'js', 'rtd_sphinx_search.min.js')
18-
css_file = os.path.join(path, '_static', 'css', 'rtd_sphinx_search.min.css')
18+
static_files = CUSTOM_ASSETS_FILES['MINIFIED'] + CUSTOM_ASSETS_FILES['UN_MINIFIED']
1919

20-
assert (
21-
os.path.exists(js_file) is True
22-
), 'js file should be copied to build folder'
20+
for file in static_files:
21+
file_path = os.path.join(path, '_static', file)
22+
assert (
23+
os.path.exists(file_path)
24+
), f'{file_path} should be present in the _build folder'
2325

24-
assert (
25-
os.path.exists(css_file) is True
26-
), 'css file should be copied to build folder'
2726

27+
@pytest.mark.sphinx(
28+
srcdir=TEST_DOCS_SRC,
29+
confoverrides={
30+
'RTD_SPHINX_SEARCH_FILE_TYPE': 'MINIFIED'
31+
}
32+
)
33+
def test_minified_static_files_injected_in_html(selenium, app, status, warning):
34+
"""Test if the static files are correctly injected in the html."""
35+
app.build()
36+
path = app.outdir / 'index.html'
2837

29-
@pytest.mark.sphinx(srcdir=TEST_DOCS_SRC)
30-
def test_static_files_injected_in_html(selenium, app, status, warning):
38+
selenium.get(f'file://{path}')
39+
page_source = selenium.page_source
40+
41+
assert app.config.RTD_SPHINX_SEARCH_FILE_TYPE == 'MINIFIED'
42+
43+
file_type = app.config.RTD_SPHINX_SEARCH_FILE_TYPE
44+
files = CUSTOM_ASSETS_FILES[file_type]
45+
46+
for file in files:
47+
file_name = file.split('/')[-1]
48+
assert (
49+
page_source.count(file_name) == 1
50+
), f'{file} should be present in the page source'
51+
52+
53+
@pytest.mark.sphinx(
54+
srcdir=TEST_DOCS_SRC,
55+
confoverrides={
56+
'RTD_SPHINX_SEARCH_FILE_TYPE': 'UN_MINIFIED'
57+
}
58+
)
59+
def test_un_minified_static_files_injected_in_html(selenium, app, status, warning):
3160
"""Test if the static files are correctly injected in the html."""
3261
app.build()
3362
path = app.outdir / 'index.html'
3463

3564
selenium.get(f'file://{path}')
3665
page_source = selenium.page_source
3766

38-
assert (
39-
page_source.count('rtd_sphinx_search.min.js') == 1
40-
), 'js file should be injected only once'
67+
assert app.config.RTD_SPHINX_SEARCH_FILE_TYPE == 'UN_MINIFIED'
68+
69+
file_type = app.config.RTD_SPHINX_SEARCH_FILE_TYPE
70+
files = CUSTOM_ASSETS_FILES[file_type]
4171

42-
assert (
43-
page_source.count('rtd_sphinx_search.min.css') == 1
44-
), 'css file should be injected only once'
72+
for file in files:
73+
file_name = file.split('/')[-1]
74+
assert (
75+
page_source.count(file_name) == 1
76+
), f'{file_name} should be present in the page source'

0 commit comments

Comments
 (0)