Skip to content

Commit

Permalink
Embed stemmer code into language_data.js by reading it from minified-…
Browse files Browse the repository at this point in the history
…js files
  • Loading branch information
mitya57 committed Feb 12, 2021
1 parent 5abbb40 commit 34553dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
10 changes: 7 additions & 3 deletions sphinx/builders/html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,9 +754,13 @@ def copy_translation_js(self) -> None:
def copy_stemmer_js(self) -> None:
"""Copy a JavaScript file for stemmer."""
if self.indexer is not None:
jsfile = self.indexer.get_js_stemmer_rawcode()
if jsfile:
copyfile(jsfile, path.join(self.outdir, '_static', '_stemmer.js'))
if hasattr(self.indexer, 'get_js_stemmer_rawcodes'):
for jsfile in self.indexer.get_js_stemmer_rawcodes():
copyfile(jsfile, path.join(self.outdir, '_static', path.basename(jsfile)))
else:
jsfile = self.indexer.get_js_stemmer_rawcode()
if jsfile:
copyfile(jsfile, path.join(self.outdir, '_static', '_stemmer.js'))

def copy_theme_static_files(self, context: Dict) -> None:
def onerror(filename: str, error: Exception) -> None:
Expand Down
27 changes: 23 additions & 4 deletions sphinx/search/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,15 +439,34 @@ def context_for_searchtool(self) -> Dict[str, Any]:
js_splitter_code = self.js_splitter_code

return {
'search_language_stemming_code': self.lang.js_stemmer_code,
'search_language_stemming_code': self.get_js_stemmer_code(),
'search_language_stop_words': jsdump.dumps(sorted(self.lang.stopwords)),
'search_scorer_tool': self.js_scorer_code,
'search_word_splitter_code': js_splitter_code,
}

def get_js_stemmer_rawcodes(self) -> List[str]:
"""Returns a list of non-minified stemmer JS files to copy."""
if self.lang.js_stemmer_rawcode:
return [
path.join(package_dir, 'search', 'non-minified-js', fname)
for fname in ('base-stemmer.js', self.lang.js_stemmer_rawcode)
]
else:
return []

def get_js_stemmer_rawcode(self) -> str:
return None

def get_js_stemmer_code(self) -> str:
"""Returns JS code that will be inserted into language_data.js."""
if self.lang.js_stemmer_rawcode:
return path.join(package_dir, 'search', 'non-minified-js',
self.lang.js_stemmer_rawcode)
js_dir = path.join(package_dir, 'search', 'minified-js')
with open(path.join(js_dir, 'base-stemmer.js')) as js_file:
base_js = js_file.read()
with open(path.join(js_dir, self.lang.js_stemmer_rawcode)) as js_file:
language_js = js_file.read()
return ('%s\n%s\nStemmer = %sStemmer;' %
(base_js, language_js, self.lang.language_name))
else:
return None
return self.lang.js_stemmer_code

0 comments on commit 34553dc

Please sign in to comment.