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

Commit 79bc347

Browse files
committed
Merge branch 'master' into link-to-full-page
2 parents 29cdcbb + dff4877 commit 79bc347

File tree

8 files changed

+114
-36
lines changed

8 files changed

+114
-36
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

docs/conf.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,9 @@
176176

177177
# A list of files that should not be packed into the epub file.
178178
epub_exclude_files = ['search.html']
179+
180+
# -- Setup for 'confval' used in docs/configuration.rst ----------------------
181+
182+
def setup(app):
183+
app.add_object_type('confval', 'confval',
184+
'pair: %s; configuration value')

docs/configuration.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Configuration
2+
=============
3+
4+
The following settings are available.
5+
You can customize these configuration options in your ``conf.py`` file:
6+
7+
.. confval:: rtd_sphinx_search_file_type
8+
9+
Description: Type of files to be included in the html.
10+
11+
Possible values:
12+
13+
- ``minified``: Include the minified and uglified CSS and JS files.
14+
- ``un-minified``: Include the original CSS and JS files.
15+
16+
Default: ``'minified'``
17+
18+
Type: ``string``

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ The CSS is also autoprefixed to extend the support to most of the browsers.
3939
:caption: Table of Contents
4040

4141
installation
42+
configuration
4243
custom-design
4344
development
4445
testing

sphinx_search/_static/js/rtd_sphinx_search.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -643,13 +643,9 @@ window.addEventListener("DOMContentLoaded", evt => {
643643
let initialHtml = generateAndReturnInitialHtml();
644644
document.body.innerHTML += initialHtml;
645645

646-
let search_outer_wrapper = document.querySelector(
647-
".search__outer__wrapper"
648-
);
649-
let search_outer_input = document.querySelector(
650-
".search__outer__input"
651-
);
652-
let cross_icon = document.querySelector(".search__cross");
646+
let search_outer_wrapper = document.querySelector('.search__outer__wrapper');
647+
let search_outer_input = document.querySelector('.search__outer__input');
648+
let cross_icon = document.querySelector('.search__cross');
653649

654650
// this denotes the search suggestion which is currently selected
655651
// via tha ArrowUp/ArrowDown keys.

sphinx_search/_static/js/rtd_sphinx_search.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sphinx_search/extension.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,49 @@
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+
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 = ASSETS_FILES['minified'] + 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+
expected_file_type = ASSETS_FILES.keys()
1930

20-
app.connect('build-finished', copy_asset_files)
31+
assert (
32+
file_type in expected_file_type
33+
), f'"{file_type}" file type is not supported.'
2134

22-
for file in CUSTOM_ASSETS_FILES:
23-
if file.endswith('.min.js'):
35+
files = ASSETS_FILES[file_type]
36+
37+
for file in files:
38+
if file.endswith('.js'):
2439
app.add_js_file(file)
25-
if file.endswith('.min.css'):
40+
elif file.endswith('.css'):
2641
app.add_css_file(file)
42+
43+
44+
def setup(app):
45+
46+
app.add_config_value('rtd_sphinx_search_file_type', 'un-minified', 'html')
47+
48+
app.connect('builder-inited', inject_static_files)
49+
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 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 = ASSETS_FILES['minified'] + 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 = 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 = 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)