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 os import path
|
||||
|
||||
import sphinx
|
||||
from sphinx.util import jsdump, rpartition
|
||||
from sphinx.util.pycompat import htmlescape
|
||||
|
||||
@@ -181,14 +182,14 @@ class WordCollector(NodeVisitor):
|
||||
self.lang = lang
|
||||
|
||||
def is_meta_keywords(self, node, nodetype):
|
||||
is_meta = str(nodetype) == '<class \'sphinx.addnodes.meta\'>'
|
||||
if is_meta and node.get('name', None) == 'keywords':
|
||||
node_lang = node.get('lang', None)
|
||||
is_correct_language = node_lang == None \
|
||||
or node_lang == self.lang.lang
|
||||
return is_meta and is_correct_language
|
||||
else:
|
||||
return False
|
||||
if isinstance(node, sphinx.addnodes.meta) and node.get('name') == 'keywords':
|
||||
meta_lang = node.get('lang')
|
||||
if meta_lang is None: # lang not specified
|
||||
return True
|
||||
elif meta_lang == self.lang.lang: # matched to html_search_language
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def dispatch_visit(self, node):
|
||||
nodetype = type(node)
|
||||
@@ -208,9 +209,9 @@ class WordCollector(NodeVisitor):
|
||||
elif issubclass(nodetype, title):
|
||||
self.found_title_words.extend(self.lang.split(node.astext()))
|
||||
elif self.is_meta_keywords(node, nodetype):
|
||||
keywords = node['content']
|
||||
keywords = [keyword.strip() for keyword in keywords.split(',')]
|
||||
self.found_words.extend(keywords)
|
||||
keywords = node['content']
|
||||
keywords = [keyword.strip() for keyword in keywords.split(',')]
|
||||
self.found_words.extend(keywords)
|
||||
|
||||
|
||||
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::
|
||||
:keywords lang=en: findthiskey, thistoo
|
||||
:keywords lang=en: findthiskey, thistoo, notgerman
|
||||
:keywords: thisonetoo
|
||||
:keywords lang=de: findnotthiskey
|
||||
:description: thisnoteither
|
||||
:keywords lang=de: onlygerman, onlytoogerman
|
||||
:description: thisnoteither
|
||||
@@ -8,6 +8,7 @@
|
||||
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
|
||||
:license: BSD, see LICENSE for details.
|
||||
"""
|
||||
import os
|
||||
|
||||
from docutils import frontend, utils
|
||||
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
|
||||
|
||||
def assert_lang_agnostic_key_words(searchindex):
|
||||
assert 'findnotthiskey' not in searchindex
|
||||
assert 'thisnoteith' not in searchindex
|
||||
assert 'thistoo' in searchindex
|
||||
assert 'thisonetoo' in searchindex
|
||||
|
||||
@with_app(testroot='search')
|
||||
def test_meta_keys_are_handled_for_language_en(app, status, warning):
|
||||
os.remove(app.outdir / 'searchindex.js')
|
||||
app.builder.build_all()
|
||||
searchindex = (app.outdir / 'searchindex.js').text()
|
||||
assert_lang_agnostic_key_words(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):
|
||||
app.builder.build_all()
|
||||
searchindex = (app.outdir / 'searchindex.js').text()
|
||||
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