| 1 |
# |
| 2 |
# Copyright (c) 2010 Doug Hellmann. All rights reserved. |
| 3 |
# |
| 4 |
"""Spelling checker extension for Sphinx. |
| 5 |
""" |
| 6 |
|
| 7 |
import collections |
| 8 |
|
| 9 |
from docutils.parsers import rst |
| 10 |
from sphinx.util import logging |
| 11 |
|
| 12 |
logger = logging.getLogger(__name__) |
| 13 |
|
| 14 |
|
| 15 |
def add_good_words_to_document(env, docname, good_words): |
| 16 |
# Initialize the per-document good words list |
| 17 |
if not hasattr(env, 'spelling_document_words'): |
| 18 |
env.spelling_document_words = collections.defaultdict(list) |
| 19 |
logger.debug('Extending local dictionary for %s with %s', |
| 20 |
env.docname, good_words) |
| 21 |
env.spelling_document_words[env.docname].extend(good_words) |
| 22 |
|
| 23 |
|
| 24 |
class SpellingDirective(rst.Directive): |
| 25 |
"""Custom directive for passing instructions to the spelling checker. |
| 26 |
|
| 27 |
.. spelling:: |
| 28 |
|
| 29 |
word1 |
| 30 |
word2 |
| 31 |
|
| 32 |
""" |
| 33 |
|
| 34 |
has_content = True |
| 35 |
|
| 36 |
def run(self): |
| 37 |
env = self.state.document.settings.env |
| 38 |
|
| 39 |
good_words = [] |
| 40 |
for entry in self.content: |
| 41 |
if not entry: |
| 42 |
continue |
| 43 |
good_words.extend(entry.split()) |
| 44 |
if good_words: |
| 45 |
add_good_words_to_document(env, env.docname, good_words) |
| 46 |
|
| 47 |
return [] |
| 48 |
|
| 49 |
|
| 50 |
class LegacySpellingDirective(SpellingDirective): |
| 51 |
|
| 52 |
def run(self): |
| 53 |
logger.info('direct use of the spelling directive is deprecated, ' |
| 54 |
'replace ".. spelling::" with ".. spelling:word-list::"') |
| 55 |
return super().run() |