refactor, add more tests #2516

This commit is contained in:
Timotheus Kampik
2016-05-02 07:54:44 +02:00
parent a8dabf334d
commit 6f6b28d3e6
7 changed files with 38 additions and 23 deletions

View File

@@ -29,7 +29,7 @@ Other contributors, listed alphabetically, are:
* Horst Gutmann -- internationalization support * Horst Gutmann -- internationalization support
* Martin Hans -- autodoc improvements * Martin Hans -- autodoc improvements
* Doug Hellmann -- graphviz improvements * Doug Hellmann -- graphviz improvements
* Timotheus Kampik - JS enhancements, stop words language fix * Timotheus Kampik - JS theme & search enhancements
* Takeshi Komiya -- numref feature * Takeshi Komiya -- numref feature
* Dave Kuhlman -- original LaTeX writer * Dave Kuhlman -- original LaTeX writer
* Blaise Laflamme -- pyramid theme * Blaise Laflamme -- pyramid theme

View File

@@ -182,11 +182,10 @@ class WordCollector(NodeVisitor):
def is_meta_keywords(self, node, nodetype): def is_meta_keywords(self, node, nodetype):
is_meta = str(nodetype) == '<class \'sphinx.addnodes.meta\'>' is_meta = str(nodetype) == '<class \'sphinx.addnodes.meta\'>'
if is_meta: if is_meta and node.get('name', None) == 'keywords':
language_match = re.search(r'lang\=\"(.*?)\"', str(node)) node_lang = node.get('lang', None)
is_correct_language =\ is_correct_language = node_lang == None \
language_match == None \ or node_lang == self.lang.lang
or self.lang.lang == language_match.group(1)
return is_meta and is_correct_language return is_meta and is_correct_language
else: else:
return False return False
@@ -209,8 +208,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 = re.search(r'content\=\"(.*?)\"', str(node)).group(1) keywords = node['content']
self.found_words.extend(self.lang.split(keywords)) keywords = [keyword.strip() for keyword in keywords.split(',')]
self.found_words.extend(keywords)
class IndexBuilder(object): class IndexBuilder(object):

View File

@@ -0,0 +1,3 @@
master_doc = 'index'
exclude_patterns = ['_build']
html_search_language = 'de'

View File

@@ -0,0 +1,8 @@
meta keywords
=============
.. meta::
:keywords lang=de: findthiskey, thistoo
:keywords: thisonetoo
:keywords lang=en: findnotthiskey
:description: thisnoteither

View File

@@ -1,11 +1,3 @@
# -*- coding: utf-8 -*-
import sys
import os
sys.path.insert(0, os.path.abspath('.'))
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode']
master_doc = 'index' master_doc = 'index'
exclude_patterns = ['_build'] exclude_patterns = ['_build']
html_search_language = 'en' html_search_language = 'en'

View File

@@ -2,7 +2,7 @@ meta keywords
============= =============
.. meta:: .. meta::
:keywords lang=en: findthiskey :keywords lang=en: findthiskey, thistoo
:keywords lang=de: findnotthiskey :keywords: thisonetoo
:keywords: thisonetoo :keywords lang=de: findnotthiskey
:description: thisnoteither

View File

@@ -53,10 +53,22 @@ def test_objects_are_escaped(app, status, warning):
index = jsdump.loads(searchindex[16:-2]) index = jsdump.loads(searchindex[16:-2])
assert 'n::Array&lt;T, d&gt;' in index.get('objects').get('') # n::Array<T,d> is escaped 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 'findnotthiskey' not in searchindex
assert 'thisnoteith' not in searchindex
assert 'thistoo' in searchindex
assert 'thisonetoo' in searchindex
@with_app(testroot='search') @with_app(testroot='search')
def test_meta_keys_are_handled_for_language(app, status, warning): def test_meta_keys_are_handled_for_language_en(app, status, warning):
app.builder.build_all() app.builder.build_all()
searchindex = (app.outdir / 'searchindex.js').text() searchindex = (app.outdir / 'searchindex.js').text()
assert 'findnotthiskey' not in searchindex assert_lang_agnostic_key_words(searchindex)
assert 'findthiskei' in searchindex assert 'findthiskei' in searchindex
assert 'thisonetoo' in searchindex
@with_app(testroot='search-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