diff --git a/AUTHORS b/AUTHORS index e1e4e8a597..dc8e824822 100644 --- a/AUTHORS +++ b/AUTHORS @@ -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 diff --git a/sphinx/search/__init__.py b/sphinx/search/__init__.py index 13fb4d528e..2ba0c38029 100644 --- a/sphinx/search/__init__.py +++ b/sphinx/search/__init__.py @@ -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) diff --git a/tests/roots/test-search/conf.py b/tests/roots/test-search/conf.py new file mode 100644 index 0000000000..38b8b28c5a --- /dev/null +++ b/tests/roots/test-search/conf.py @@ -0,0 +1,3 @@ +master_doc = 'index' +exclude_patterns = ['_build'] +html_search_language = 'en' diff --git a/tests/roots/test-search/index.rst b/tests/roots/test-search/index.rst new file mode 100644 index 0000000000..f2536ce9a8 --- /dev/null +++ b/tests/roots/test-search/index.rst @@ -0,0 +1,8 @@ +meta keywords +============= + +.. meta:: + :keywords lang=en: findthiskey, thistoo, notgerman + :keywords: thisonetoo + :keywords lang=de: onlygerman, onlytoogerman + :description: thisnoteither \ No newline at end of file diff --git a/tests/test_search.py b/tests/test_search.py index cd2ff76f29..160dd18278 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -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<T, d>' in index.get('objects').get('') # n::Array 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 \ No newline at end of file