Merge pull request #2516 from TimKam/meta-keywords-to-search-index

Meta keywords to search index
This commit is contained in:
Takeshi KOMIYA
2016-05-24 11:10:07 +09:00
5 changed files with 51 additions and 2 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ Other contributors, listed alphabetically, are:
* Horst Gutmann -- internationalization support
* Martin Hans -- autodoc improvements
* Doug Hellmann -- graphviz improvements
* Timotheus Kampik - JS enhancements, stop words language fix
* Timotheus Kampik - JS theme & search enhancements
* Takeshi Komiya -- numref feature
* Dave Kuhlman -- original LaTeX writer
* Blaise Laflamme -- pyramid theme
+15 -1
View File
@@ -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
@@ -180,6 +181,16 @@ class WordCollector(NodeVisitor):
self.found_title_words = []
self.lang = lang
def is_meta_keywords(self, node, nodetype):
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)
if issubclass(nodetype, comment):
@@ -197,6 +208,10 @@ class WordCollector(NodeVisitor):
self.found_words.extend(self.lang.split(node.astext()))
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)
class IndexBuilder(object):
@@ -353,7 +368,6 @@ class IndexBuilder(object):
def feed(self, filename, title, doctree):
"""Feed a doctree to the index."""
self._titles[filename] = title
visitor = WordCollector(doctree, self.lang)
doctree.walk(visitor)
+3
View File
@@ -0,0 +1,3 @@
master_doc = 'index'
exclude_patterns = ['_build']
html_search_language = 'en'
+8
View File
@@ -0,0 +1,8 @@
meta keywords
=============
.. meta::
:keywords lang=en: findthiskey, thistoo, notgerman
:keywords: thisonetoo
:keywords lang=de: onlygerman, onlytoogerman
:description: thisnoteither
+24
View File
@@ -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
@@ -52,3 +53,26 @@ def test_objects_are_escaped(app, status, warning):
index = jsdump.loads(searchindex[16:-2])
assert 'n::Array&lt;T, d&gt;' in index.get('objects').get('') # n::Array<T,d> is escaped
def assert_lang_agnostic_key_words(searchindex):
assert 'thisnoteith' not 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', 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 'onlygerman' in searchindex
assert 'notgerman' not in searchindex
assert 'onlytoogerman' in searchindex