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

[Update] Support Page Sections & Sphinx Domains #19

Merged
merged 43 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
e24214d
Update the extension to support page sections and sphinx domains
dojutsu-user Jun 29, 2019
8b56cee
fix tests
dojutsu-user Jun 29, 2019
3cdedc7
set count to zero
dojutsu-user Jun 29, 2019
a477e9c
set margin
dojutsu-user Jun 29, 2019
eecb619
update docs
dojutsu-user Jun 30, 2019
03f1808
change debounce time
dojutsu-user Jun 30, 2019
2173fc4
update debounce time
dojutsu-user Jun 30, 2019
66d8861
add min js file
dojutsu-user Jul 1, 2019
31f142f
use templating langugage
dojutsu-user Jul 2, 2019
abf809c
add missing bracket
dojutsu-user Jul 2, 2019
c115c52
show sorted results
dojutsu-user Jul 2, 2019
24c84f5
add comments
dojutsu-user Jul 2, 2019
156a8b9
Fix test
dojutsu-user Jul 2, 2019
d533cfc
show more data for domain objects
dojutsu-user Jul 3, 2019
73f3b4b
remove external templating language
dojutsu-user Jul 3, 2019
2d9763b
fix horizontal scroll bar
dojutsu-user Jul 3, 2019
1e39d47
show subprojecs with font reduced
dojutsu-user Jul 4, 2019
e4796a7
fix clashing css class
dojutsu-user Jul 4, 2019
6e869e2
use more .template
dojutsu-user Jul 4, 2019
a789e81
ui improvements and make extension compatible with sphinx <= 1.8
dojutsu-user Jul 4, 2019
604204b
edit tox.ini
dojutsu-user Jul 4, 2019
4a48c8a
Merge branch 'master' into update-extension-with-sections
dojutsu-user Jul 5, 2019
7ed4096
remove sphinx17 from tox
dojutsu-user Jul 5, 2019
84fc171
ignore RemovedInSphinx40Warning in pytest.ini
dojutsu-user Jul 5, 2019
5119812
add test
dojutsu-user Jul 8, 2019
85d082d
use sphinx 18 syntax
dojutsu-user Jul 8, 2019
8dce55e
don't use minified file
dojutsu-user Jul 8, 2019
c735b75
remove html structure comments and from the docs
dojutsu-user Jul 9, 2019
047a438
show max 3 results from page sections
dojutsu-user Jul 9, 2019
d8c9d46
improvements
dojutsu-user Jul 9, 2019
40fef13
use helper functions
dojutsu-user Jul 9, 2019
ab1dbbb
use 100 as constant
dojutsu-user Jul 10, 2019
2957f9a
use minified files
dojutsu-user Jul 10, 2019
7c1dfcf
update minified files
dojutsu-user Jul 10, 2019
dbddbd4
add checkbox for searching in current section
dojutsu-user Jul 10, 2019
bf7dfec
remove checkbox for now
dojutsu-user Jul 10, 2019
ed8764e
clean some code
dojutsu-user Jul 10, 2019
75a0cd9
fix tests
dojutsu-user Jul 10, 2019
46a383b
Merge branch 'master' into update-extension-with-sections
dojutsu-user Jul 11, 2019
2746b35
update minified files
dojutsu-user Jul 11, 2019
b164cb2
provide setting for choosing between minified and unminified files
dojutsu-user Jul 12, 2019
ffa8bc3
follow sphinx convention
dojutsu-user Jul 12, 2019
a79e44d
update docs
dojutsu-user Jul 14, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
prune common
include LICENSE
include sphinx_search/_static/js/rtd_sphinx_search.js
include sphinx_search/_static/js/rtd_sphinx_search.min.js
include sphinx_search/_static/css/rtd_sphinx_search.css
include sphinx_search/_static/css/rtd_sphinx_search.min.css
43 changes: 30 additions & 13 deletions sphinx_search/extension.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
import os

from sphinx.util.fileutil import copy_asset


CUSTOM_ASSETS_FILES = [
os.path.join('js', 'rtd_sphinx_search.min.js'),
os.path.join('css', 'rtd_sphinx_search.min.css'),
]
CUSTOM_ASSETS_FILES = {
'MINIFIED': [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should also be lowercase.

os.path.join('js', 'rtd_sphinx_search.min.js'),
os.path.join('css', 'rtd_sphinx_search.min.css'),
],
'UN_MINIFIED': [
os.path.join('js', 'rtd_sphinx_search.js'),
os.path.join('css', 'rtd_sphinx_search.css'),
]
}


def copy_asset_files(app, exception):
if exception is None: # build succeeded
for f in CUSTOM_ASSETS_FILES:
path = os.path.join(os.path.dirname(__file__), '_static', f)
copy_asset(path, os.path.join(app.outdir, '_static', f.split('.')[-1]))
files = CUSTOM_ASSETS_FILES['MINIFIED'] + CUSTOM_ASSETS_FILES['UN_MINIFIED']
for file in files:
path = os.path.join(os.path.dirname(__file__), '_static', file)
copy_asset(path, os.path.join(app.outdir, '_static', file.split('.')[-1]))


def setup(app):
def inject_static_files(app):
"""Inject correct CSS and JS files based on the value of ``RTD_SPHINX_SEARCH_FILE_TYPE``."""
file_type = app.config.RTD_SPHINX_SEARCH_FILE_TYPE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should check the incoming value for being one of the two we expect, and give users a good error if not.

files = CUSTOM_ASSETS_FILES[file_type]

app.connect('build-finished', copy_asset_files)

for file in CUSTOM_ASSETS_FILES:
if file.endswith('.min.js'):
for file in files:
if file.endswith('.js'):
app.add_js_file(file)
if file.endswith('.min.css'):
elif file.endswith('.css'):
app.add_css_file(file)


def setup(app):

app.add_config_value('RTD_SPHINX_SEARCH_FILE_TYPE', 'UN_MINIFIED', 'html')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe Sphinx uses lower-case config values by convention.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also should be documented.

Copy link
Member Author

@dojutsu-user dojutsu-user Jul 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the documentation changes be done in another PR (the one with the API reference)?


app.connect('builder-inited', inject_static_files)
app.connect('build-finished', copy_asset_files)
64 changes: 48 additions & 16 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest

from tests import TEST_DOCS_SRC
from sphinx_search.extension import CUSTOM_ASSETS_FILES


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

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

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

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

@pytest.mark.sphinx(
srcdir=TEST_DOCS_SRC,
confoverrides={
'RTD_SPHINX_SEARCH_FILE_TYPE': 'MINIFIED'
}
)
def test_minified_static_files_injected_in_html(selenium, app, status, warning):
"""Test if the static files are correctly injected in the html."""
app.build()
path = app.outdir / 'index.html'

@pytest.mark.sphinx(srcdir=TEST_DOCS_SRC)
def test_static_files_injected_in_html(selenium, app, status, warning):
selenium.get(f'file://{path}')
page_source = selenium.page_source

assert app.config.RTD_SPHINX_SEARCH_FILE_TYPE == 'MINIFIED'

file_type = app.config.RTD_SPHINX_SEARCH_FILE_TYPE
files = CUSTOM_ASSETS_FILES[file_type]

for file in files:
file_name = file.split('/')[-1]
assert (
page_source.count(file_name) == 1
), f'{file} should be present in the page source'


@pytest.mark.sphinx(
srcdir=TEST_DOCS_SRC,
confoverrides={
'RTD_SPHINX_SEARCH_FILE_TYPE': 'UN_MINIFIED'
}
)
def test_un_minified_static_files_injected_in_html(selenium, app, status, warning):
"""Test if the static files are correctly injected in the html."""
app.build()
path = app.outdir / 'index.html'

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

assert (
page_source.count('rtd_sphinx_search.min.js') == 1
), 'js file should be injected only once'
assert app.config.RTD_SPHINX_SEARCH_FILE_TYPE == 'UN_MINIFIED'

file_type = app.config.RTD_SPHINX_SEARCH_FILE_TYPE
files = CUSTOM_ASSETS_FILES[file_type]

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