mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2026-09-03 20:52:55 -05:00
refactor code/tests: keyword support for search index #2516
This commit is contained in:
+12
-11
@@ -15,6 +15,7 @@ from six.moves import cPickle as pickle
|
|||||||
from docutils.nodes import raw, comment, title, Text, NodeVisitor, SkipNode
|
from docutils.nodes import raw, comment, title, Text, NodeVisitor, SkipNode
|
||||||
from os import path
|
from os import path
|
||||||
|
|
||||||
|
import sphinx
|
||||||
from sphinx.util import jsdump, rpartition
|
from sphinx.util import jsdump, rpartition
|
||||||
from sphinx.util.pycompat import htmlescape
|
from sphinx.util.pycompat import htmlescape
|
||||||
|
|
||||||
@@ -181,14 +182,14 @@ class WordCollector(NodeVisitor):
|
|||||||
self.lang = lang
|
self.lang = lang
|
||||||
|
|
||||||
def is_meta_keywords(self, node, nodetype):
|
def is_meta_keywords(self, node, nodetype):
|
||||||
is_meta = str(nodetype) == '<class \'sphinx.addnodes.meta\'>'
|
if isinstance(node, sphinx.addnodes.meta) and node.get('name') == 'keywords':
|
||||||
if is_meta and node.get('name', None) == 'keywords':
|
meta_lang = node.get('lang')
|
||||||
node_lang = node.get('lang', None)
|
if meta_lang is None: # lang not specified
|
||||||
is_correct_language = node_lang == None \
|
return True
|
||||||
or node_lang == self.lang.lang
|
elif meta_lang == self.lang.lang: # matched to html_search_language
|
||||||
return is_meta and is_correct_language
|
return True
|
||||||
else:
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def dispatch_visit(self, node):
|
def dispatch_visit(self, node):
|
||||||
nodetype = type(node)
|
nodetype = type(node)
|
||||||
@@ -208,9 +209,9 @@ class WordCollector(NodeVisitor):
|
|||||||
elif issubclass(nodetype, title):
|
elif issubclass(nodetype, title):
|
||||||
self.found_title_words.extend(self.lang.split(node.astext()))
|
self.found_title_words.extend(self.lang.split(node.astext()))
|
||||||
elif self.is_meta_keywords(node, nodetype):
|
elif self.is_meta_keywords(node, nodetype):
|
||||||
keywords = node['content']
|
keywords = node['content']
|
||||||
keywords = [keyword.strip() for keyword in keywords.split(',')]
|
keywords = [keyword.strip() for keyword in keywords.split(',')]
|
||||||
self.found_words.extend(keywords)
|
self.found_words.extend(keywords)
|
||||||
|
|
||||||
|
|
||||||
class IndexBuilder(object):
|
class IndexBuilder(object):
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
master_doc = 'index'
|
|
||||||
exclude_patterns = ['_build']
|
|
||||||
html_search_language = 'de'
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
meta keywords
|
|
||||||
=============
|
|
||||||
|
|
||||||
.. meta::
|
|
||||||
:keywords lang=de: findthiskey, thistoo
|
|
||||||
:keywords: thisonetoo
|
|
||||||
:keywords lang=en: findnotthiskey
|
|
||||||
:description: thisnoteither
|
|
||||||
@@ -2,7 +2,7 @@ meta keywords
|
|||||||
=============
|
=============
|
||||||
|
|
||||||
.. meta::
|
.. meta::
|
||||||
:keywords lang=en: findthiskey, thistoo
|
:keywords lang=en: findthiskey, thistoo, notgerman
|
||||||
:keywords: thisonetoo
|
:keywords: thisonetoo
|
||||||
:keywords lang=de: findnotthiskey
|
:keywords lang=de: onlygerman, onlytoogerman
|
||||||
:description: thisnoteither
|
:description: thisnoteither
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
|
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
|
||||||
:license: BSD, see LICENSE for details.
|
:license: BSD, see LICENSE for details.
|
||||||
"""
|
"""
|
||||||
|
import os
|
||||||
|
|
||||||
from docutils import frontend, utils
|
from docutils import frontend, utils
|
||||||
from docutils.parsers import rst
|
from docutils.parsers import rst
|
||||||
@@ -54,21 +55,24 @@ def test_objects_are_escaped(app, status, warning):
|
|||||||
assert 'n::Array<T, d>' in index.get('objects').get('') # n::Array<T,d> is escaped
|
assert 'n::Array<T, d>' in index.get('objects').get('') # n::Array<T,d> is escaped
|
||||||
|
|
||||||
def assert_lang_agnostic_key_words(searchindex):
|
def assert_lang_agnostic_key_words(searchindex):
|
||||||
assert 'findnotthiskey' not in searchindex
|
|
||||||
assert 'thisnoteith' not in searchindex
|
assert 'thisnoteith' not in searchindex
|
||||||
assert 'thistoo' in searchindex
|
|
||||||
assert 'thisonetoo' in searchindex
|
assert 'thisonetoo' in searchindex
|
||||||
|
|
||||||
@with_app(testroot='search')
|
@with_app(testroot='search')
|
||||||
def test_meta_keys_are_handled_for_language_en(app, status, warning):
|
def test_meta_keys_are_handled_for_language_en(app, status, warning):
|
||||||
|
os.remove(app.outdir / 'searchindex.js')
|
||||||
app.builder.build_all()
|
app.builder.build_all()
|
||||||
searchindex = (app.outdir / 'searchindex.js').text()
|
searchindex = (app.outdir / 'searchindex.js').text()
|
||||||
assert_lang_agnostic_key_words(searchindex)
|
assert_lang_agnostic_key_words(searchindex)
|
||||||
assert 'findthiskei' in searchindex
|
assert 'findthiskei' in searchindex
|
||||||
|
assert 'onlygerman' not in searchindex
|
||||||
|
assert 'thistoo' in searchindex
|
||||||
|
|
||||||
@with_app(testroot='search-de')
|
@with_app(testroot='search', confoverrides={'html_search_language': 'de'})
|
||||||
def test_meta_keys_are_handled_for_language_de(app, status, warning):
|
def test_meta_keys_are_handled_for_language_de(app, status, warning):
|
||||||
app.builder.build_all()
|
app.builder.build_all()
|
||||||
searchindex = (app.outdir / 'searchindex.js').text()
|
searchindex = (app.outdir / 'searchindex.js').text()
|
||||||
assert_lang_agnostic_key_words(searchindex)
|
assert_lang_agnostic_key_words(searchindex)
|
||||||
assert 'findthiskey' in searchindex
|
assert 'onlygerman' in searchindex
|
||||||
|
assert 'notgerman' not in searchindex
|
||||||
|
assert 'onlytoogerman' in searchindex
|
||||||
Reference in New Issue
Block a user