mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2026-09-03 20:52:55 -05:00
Merge pull request #2516 from TimKam/meta-keywords-to-search-index
Meta keywords to search index
This commit is contained in:
@@ -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,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)
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
master_doc = 'index'
|
||||
exclude_patterns = ['_build']
|
||||
html_search_language = 'en'
|
||||
@@ -0,0 +1,8 @@
|
||||
meta keywords
|
||||
=============
|
||||
|
||||
.. meta::
|
||||
:keywords lang=en: findthiskey, thistoo, notgerman
|
||||
:keywords: thisonetoo
|
||||
: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
|
||||
@@ -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<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
|
||||
Reference in New Issue
Block a user