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

Use new stable API #55

Merged
merged 1 commit into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 0 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
# This file does only contain a selection of the most common options. For a
Expand Down
1 change: 0 additions & 1 deletion docs/js-api-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Following are the functions that are defined in `rtd_sphinx_search.js`_,
.. js:autofunction:: _is_string
.. js:autofunction:: _is_array
.. js:autofunction:: get_section_html
.. js:autofunction:: getHighlightListData
.. js:autofunction:: get_domain_html
.. js:autofunction:: generateSingleResult
.. js:autofunction:: generateSuggestionsList
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.1",
"description": "Enable search-as-you-type feature.",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "gulp"
},
"repository": {
"type": "git",
Expand Down
101 changes: 32 additions & 69 deletions sphinx_search/static/js/rtd_sphinx_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,18 @@ const get_section_html = (sectionData, page_link) => {
</a> \
<br class="br-for-hits">';

let section_subheading = sectionData._source.title;
let highlight = sectionData.highlight;
if (getHighlightListData(highlight, "sections.title")) {
section_subheading = getHighlightListData(
highlight,
"sections.title"
)[0];
let section_subheading = sectionData.title;
let highlights = sectionData.highlights;
if (highlights.title.length) {
section_subheading = highlights.title[0];
}

let section_content = [
sectionData._source.content.substring(0, MAX_SUBSTRING_LIMIT) + " ..."
sectionData.content.substring(0, MAX_SUBSTRING_LIMIT) + " ..."
];

if (getHighlightListData(highlight, "sections.content")) {
let highlight_content = getHighlightListData(
highlight,
"sections.content"
);
if (highlights.content.length) {
let highlight_content = highlights.content;
section_content = [];
for (
let j = 0;
Expand All @@ -206,7 +200,7 @@ const get_section_html = (sectionData, page_link) => {
}
}

let section_link = `${page_link}#${sectionData._source.id}`;
let section_link = `${page_link}#${sectionData.id}`;

let section_id = "hit__" + COUNT;

Expand All @@ -220,22 +214,6 @@ const get_section_html = (sectionData, page_link) => {
return section_html;
};

/**
* Returns value of the corresponding key (if present),
* else returns false.
*
* @param {Object} data object containing the data used for highlighting
* @param {String} key key whose values is to be returned
* @return {Array|Boolean} if key is present, it will return its value. Otherwise, return false
*/
const getHighlightListData = (data, key) => {
if (_is_array(data[key])) {
return data[key];
} else {
return false;
}
};

/**
* Generate and return html structure
* for a sphinx domain result.
Expand All @@ -258,38 +236,28 @@ const get_domain_html = (domainData, page_link) => {
</a> \
<br class="br-for-hits">';

let domain_link = `${page_link}#${domainData._source.anchor}`;
let domain_role_name = domainData._source.role_name;
let domain_name = domainData._source.name;
let domain_docstrings =
domainData._source.docstrings.substr(0, MAX_SUBSTRING_LIMIT) + " ...";
let domain_link = `${page_link}#${domainData.id}`;
let domain_role_name = domainData.role;
let domain_name = domainData.name;
let domain_content =
domainData.content.substr(0, MAX_SUBSTRING_LIMIT) + " ...";

// take values from highlighted fields (if present)
if (domainData.highlight !== undefined && domainData.highlight !== null) {
let highlight = domainData.highlight;

let name = getHighlightListData(highlight, "domains.name");
let docstrings = getHighlightListData(highlight, "domains.docstrings");

if (name) {
domain_name = name[0];
}

if (docstrings) {
domain_docstrings = docstrings[0];
}
let highlights = domainData.highlights;
if (highlights.name.length) {
domain_name = highlights.name[0];
}
if (highlights.content.length) {
domain_content = highlights.content[0];
}

let domain_subheading = domain_name;
let domain_content = domain_docstrings;
let domain_id = "hit__" + COUNT;
domain_role_name = "[" + domain_role_name + "]";

let domain_html = $u.template(domain_template, {
domain_link: domain_link,
domain_id: domain_id,
domain_content: domain_content,
domain_subheading: domain_subheading,
domain_subheading: domain_name,
domain_role_name: domain_role_name
});

Expand All @@ -312,17 +280,12 @@ const generateSingleResult = (resultData, projectName) => {
</h2> \
</a>';

let page_link = resultData.link;
let page_link = resultData.path;
let page_title = resultData.title;
let highlights = resultData.highlights;

// if title is present in highlighted field, use that.
if (resultData.highlight !== undefined && resultData.highlight !== null) {
if (
resultData.highlight.title !== undefined &&
resultData.highlight.title !== null
) {
page_title = resultData.highlight.title;
}
if (highlights.title.length) {
page_title = highlights.title[0];
}

// if result is not from the same project,
Expand All @@ -347,19 +310,19 @@ const generateSingleResult = (resultData, projectName) => {
page_title: page_title
});

for (let i = 0; i < resultData.inner_hits.length; ++i) {
const type = resultData.inner_hits[i].type;
for (let i = 0; i < resultData.blocks.length; ++i) {
let block = resultData.blocks[i];
COUNT += 1;
let html_structure = "";

if (type === "sections") {
if (block.type === "section") {
html_structure = get_section_html(
resultData.inner_hits[i],
block,
page_link
);
} else if (type === "domains") {
} else if (block.type === "domain") {
html_structure = get_domain_html(
resultData.inner_hits[i],
block,
page_link
);
}
Expand Down Expand Up @@ -673,12 +636,12 @@ window.addEventListener("DOMContentLoaded", evt => {
q: SEARCH_QUERY,
project: project,
version: version,
language: language
language: language,
};

const search_url =
api_host +
"/api/v2/docsearch/?" +
"/api/v2/search/?" +
convertObjToUrlParams(search_params);

if (typeof SEARCH_QUERY === "string" && SEARCH_QUERY.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion sphinx_search/static/js/rtd_sphinx_search.min.js

Large diffs are not rendered by default.

52 changes: 24 additions & 28 deletions tests/dummy_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,43 @@
"project": "sphinx",
"version": "latest",
"title": "Developing a “Hello world” extension",
"path": "development/tutorials/helloworld",
"link": "http://localhost:8000/docs/template/projects/sphinx/en/latest/development/tutorials/helloworld",
"highlight": {
"highlights": {
"title": [
"Developing a “<em>Hello</em> <em>world</em>” extension"
]
},
"inner_hits": [
"blocks": [
{
"type": "sections",
"_source": {
"id": "overview",
"title": "Overview",
"content": "We want the extension to add the following to Sphinx:\nA helloworld directive, that will simply output the text “hello world”."
},
"highlight": {
"sections.content": ["<em>world</em>”."]
"type": "section",
"id": "overview",
"title": "Overview",
"content": "We want the extension to add the following to Sphinx:\nA helloworld directive, that will simply output the text “hello world”.",
"highlights": {
"title": [],
"content": ["<em>world</em>”."]
}
},
{
"type": "sections",
"_source": {
"id": "developing-a-hello-world-extension",
"title": "Developing a “Hello world” extension",
"content": "Only basic information is provided in this tutorial. For more information, refer to the other tutorials that go into more details.\n\nWarning\nFor this extension, you will need some basic understanding of docutils and Python.\n"
},
"highlight": {
"sections.title": [
"type": "section",
"id": "developing-a-hello-world-extension",
"title": "Developing a “Hello world” extension",
"content": "Only basic information is provided in this tutorial. For more information, refer to the other tutorials that go into more details.\n\nWarning\nFor this extension, you will need some basic understanding of docutils and Python.\n",
"highlights": {
"title": [
"Developing a “<em>Hello</em> <em>world</em>” extension"
]
],
"content": []
}
},
{
"type": "sections",
"_source": {
"id": "using-the-extension",
"title": "Using the extension",
"content": "The extension has to be declared in your conf.py file to make Sphinx aware of it. There are two steps necessary here:\nAdd the _ext directory to the Python path using sys.path.append. This should be placed at the top of the file.\nUpdate or create the extensions list and add the extension file name to the list\nFor example:\nimport os import sys sys.path.append(os.path.abspath(\"./_ext\")) extensions = ['helloworld']\nTip\nWe’re not distributing this extension as a Python package, we need to modify the Python path so Sphinx can find our extension. This is why we need the call to sys.path.append.\nYou can now use the extension in a file. For example:\nSome intro text here... .. helloworld:: Some more text here...\nThe sample above would generate:\nSome intro text here... Hello World! Some more text here..."
},
"highlight": {
"sections.content": [
"type": "section",
"id": "using-the-extension",
"title": "Using the extension",
"content": "The extension has to be declared in your conf.py file to make Sphinx aware of it. There are two steps necessary here:\nAdd the _ext directory to the Python path using sys.path.append. This should be placed at the top of the file.\nUpdate or create the extensions list and add the extension file name to the list\nFor example:\nimport os import sys sys.path.append(os.path.abspath(\"./_ext\")) extensions = ['helloworld']\nTip\nWe’re not distributing this extension as a Python package, we need to modify the Python path so Sphinx can find our extension. This is why we need the call to sys.path.append.\nYou can now use the extension in a file. For example:\nSome intro text here... .. helloworld:: Some more text here...\nThe sample above would generate:\nSome intro text here... Hello World! Some more text here...",
"highlights": {
"title": [],
"content": [
"<em>Hello</em> <em>World</em>! Some more text here..."
]
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def test_closing_the_modal_by_escape_button(selenium, app, status, warning):
with InjectJsManager(path, SCRIPT_TAG) as _:
selenium.get(f'file://{path}')
open_search_modal(selenium)

search_outer_wrapper = selenium.find_element_by_class_name(
'search__outer__wrapper'
)
Expand Down Expand Up @@ -611,7 +611,7 @@ def test_writing_query_adds_rtd_search_as_url_param(selenium, app, status, warni
search_outer_input.send_keys(Keys.BACK_SPACE)

if i != query_len -1:

current_query = query[:query_len - i - 1]
current_url = parse.unquote(selenium.current_url)
query_in_url = current_url[current_url.find('rtd_search'):]
Expand Down
4 changes: 2 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __enter__(self):
)
f.seek(0)
f.write(new_content)

return self._file

def __exit__(self, exc_type, exc_val, exc_tb):
Expand Down Expand Up @@ -129,7 +129,7 @@ def get_ajax_overwrite_func(type_, **kwargs):
elif type_ == 'dummy_results':
with open(DUMMY_RESULTS, 'r') as f:
dummy_res = f.read()

ajax_func = f'''
<script>
$.ajax = function(params) {{
Expand Down