Back to index

moin  1.9.0~rc2
Functions
MoinMoin.action.SpellCheck Namespace Reference

Functions

def _getWordsFiles
def _loadWords
def _loadWordsFile
def _loadWordsPage
def _loadDict
def _addLocalWords
def checkSpelling
def execute

Function Documentation

def MoinMoin.action.SpellCheck._addLocalWords (   request) [private]

Definition at line 101 of file SpellCheck.py.

00101 
00102 def _addLocalWords(request):
00103     from MoinMoin.PageEditor import PageEditor
00104     # get the new words as a string (if any are marked at all)
00105     try:
00106         newwords = request.form.getlist('newwords')
00107     except KeyError:
00108         # no new words checked
00109         return
00110     newwords = u' '.join(newwords)
00111 
00112     # get the page contents
00113     lsw_page = PageEditor(request, request.cfg.page_local_spelling_words)
00114     words = lsw_page.get_raw_body()
00115 
00116     # add the words to the page and save it
00117     if words and words[-1] != '\n':
00118         words = words + '\n'
00119     lsw_page.saveText(words + '\n' + newwords, 0)
00120 

Here is the caller graph for this function:

def MoinMoin.action.SpellCheck._getWordsFiles (   request) [private]
Check a list of possible word files

Definition at line 28 of file SpellCheck.py.

00028 
00029 def _getWordsFiles(request):
00030     """Check a list of possible word files"""
00031     candidates = []
00032 
00033     # load a list of possible word files
00034     for basedir in (request.cfg.moinmoin_dir, request.cfg.data_dir):
00035         localdict = os.path.join(basedir, 'dict')
00036         if os.path.isdir(localdict):
00037             candidates.extend(
00038                 [os.path.join(localdict, fn) for fn in os.listdir(localdict)])
00039 
00040     # validate candidate list (leave out directories!)
00041     wordsfiles = []
00042     for f in candidates:
00043         if os.path.isfile(f) and os.access(f, os.F_OK | os.R_OK):
00044             wordsfiles.append(f)
00045 
00046     # return validated file list
00047     return wordsfiles

Here is the caller graph for this function:

def MoinMoin.action.SpellCheck._loadDict (   request) [private]
Load words from words files or cached dict 

Definition at line 73 of file SpellCheck.py.

00073 
00074 def _loadDict(request):
00075     """ Load words from words files or cached dict """
00076     # check for "dbhash" module
00077     try:
00078         import dbhash
00079     except ImportError:
00080         dbhash = None
00081 
00082     # load the words
00083     cachename = os.path.join(request.cfg.data_dir, 'cache', 'spellchecker.dict')
00084     if dbhash and os.path.exists(cachename):
00085         wordsdict = dbhash.open(cachename, "r")
00086     else:
00087         wordsfiles = _getWordsFiles(request)
00088         if dbhash:
00089             wordsdict = dbhash.open(cachename, 'n')
00090         else:
00091             wordsdict = {}
00092 
00093         for wordsfile in wordsfiles:
00094             _loadWordsFile(request, wordsdict, wordsfile)
00095 
00096         if dbhash:
00097             wordsdict.sync()
00098 
00099     return wordsdict
00100 

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.action.SpellCheck._loadWords (   lines,
  dict 
) [private]

Definition at line 48 of file SpellCheck.py.

00048 
00049 def _loadWords(lines, dict):
00050     for line in lines:
00051         words = line.split()
00052         for word in words:
00053             dict[word.encode(config.charset)] = ''

Here is the caller graph for this function:

def MoinMoin.action.SpellCheck._loadWordsFile (   request,
  dict,
  filename 
) [private]

Definition at line 54 of file SpellCheck.py.

00054 
00055 def _loadWordsFile(request, dict, filename):
00056     request.clock.start('spellread')
00057     try:
00058         try:
00059             f = codecs.open(filename, 'rt', config.charset)
00060             lines = f.readlines()
00061         except UnicodeError:
00062             f = codecs.open(filename, 'rt', 'iso-8859-1')
00063             lines = f.readlines()
00064     finally:
00065         f.close()
00066     _loadWords(lines, dict)
00067     request.clock.stop('spellread')

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.action.SpellCheck._loadWordsPage (   request,
  dict,
  page 
) [private]

Definition at line 68 of file SpellCheck.py.

00068 
00069 def _loadWordsPage(request, dict, page):
00070     lines = page.getlines()
00071     _loadWords(lines, dict)
00072 

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.action.SpellCheck.checkSpelling (   page,
  request,
  own_form = 1 
)
Do spell checking, return a tuple with the result.

Definition at line 121 of file SpellCheck.py.

00121 
00122 def checkSpelling(page, request, own_form=1):
00123     """ Do spell checking, return a tuple with the result.
00124     """
00125     _ = request.getText
00126 
00127     # first check to see if we we're called with a "newwords" parameter
00128     if 'button_newwords' in request.form:
00129         _addLocalWords(request)
00130 
00131     # load words
00132     wordsdict = _loadDict(request)
00133 
00134     localwords = {}
00135     lsw_page = Page(request, request.cfg.page_local_spelling_words)
00136     if lsw_page.exists():
00137         _loadWordsPage(request, localwords, lsw_page)
00138 
00139     # init status vars & load page
00140     request.clock.start('spellcheck')
00141     badwords = {}
00142     text = page.get_raw_body()
00143 
00144     # checker regex and matching substitute function
00145     word_re = re.compile(r'([%s]?[%s]+)' % (
00146         config.chars_upper, config.chars_lower), re.UNICODE)
00147 
00148     def checkword(match, wordsdict=wordsdict, badwords=badwords,
00149             localwords=localwords, num_re=re.compile(r'^\d+$', re.UNICODE)):
00150         word = match.group(1)
00151         if len(word) == 1:
00152             return ""
00153         w_enc = word.encode(config.charset)
00154         wl_enc = word.lower().encode(config.charset)
00155         if not (w_enc in wordsdict or wl_enc in wordsdict or
00156                 w_enc in localwords or wl_enc in localwords):
00157             if not num_re.match(word):
00158                 badwords[word] = 1
00159         return ""
00160 
00161     # do the checking
00162     for line in text.split('\n'):
00163         if line == '' or line[0] == '#':
00164             continue
00165         word_re.sub(checkword, line)
00166 
00167     if badwords:
00168         badwords = badwords.keys()
00169         badwords.sort(lambda x, y: cmp(x.lower(), y.lower()))
00170 
00171         # build regex recognizing the bad words
00172         badwords_re = r'(^|(?<!\w))(%s)(?!\w)'
00173         badwords_re = badwords_re % ("|".join([re.escape(bw) for bw in badwords]), )
00174         badwords_re = re.compile(badwords_re, re.UNICODE)
00175 
00176         lsw_msg = ''
00177         if localwords:
00178             lsw_msg = ' ' + _('(including %(localwords)d %(pagelink)s)') % {
00179                 'localwords': len(localwords), 'pagelink': lsw_page.link_to(request)}
00180         msg = _('The following %(badwords)d words could not be found in the dictionary of '
00181                 '%(totalwords)d words%(localwords)s and are highlighted below:') % {
00182             'badwords': len(badwords),
00183             'totalwords': len(wordsdict)+len(localwords),
00184             'localwords': lsw_msg} + "<br>"
00185 
00186         # figure out what this action is called
00187         action_name = os.path.splitext(os.path.basename(__file__))[0]
00188 
00189         # add a form containing the bad words
00190         if own_form:
00191             msg = msg + ('<form method="post" action="%s">\n'
00192                          '<input type="hidden" name="action" value="%s">\n') % (request.href(page.page_name), action_name)
00193 
00194         checkbox = '<input type="checkbox" name="newwords" value="%(word)s">%(word)s&nbsp;&nbsp;'
00195         msg = msg + (
00196             " ".join([checkbox % {'word': wikiutil.escape(w, True), } for w in badwords]) +
00197             '<p><input type="submit" name="button_newwords" value="%s"></p>' %
00198                 _('Add checked words to dictionary')
00199         )
00200         if own_form:
00201             msg = msg + '</form>'
00202     else:
00203         badwords_re = None
00204         msg = _("No spelling errors found!")
00205 
00206     request.clock.stop('spellcheck')
00207 
00208     return badwords, badwords_re, msg
00209 

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.action.SpellCheck.execute (   pagename,
  request 
)

Definition at line 210 of file SpellCheck.py.

00210 
00211 def execute(pagename, request):
00212     _ = request.getText
00213 
00214     page = Page(request, pagename)
00215     if not request.user.may.write(request.cfg.page_local_spelling_words):
00216         request.theme.add_msg(_("You can't save spelling words."), "error")
00217         page.send_page()
00218         return
00219 
00220     if request.user.may.read(pagename):
00221         badwords, badwords_re, msg = checkSpelling(page, request)
00222     else:
00223         badwords = []
00224         request.theme.add_msg(_("You can't check spelling on a page you can't read."), "error")
00225 
00226     request.theme.add_msg(msg, "dialog")
00227     if badwords:
00228         page.send_page(hilite_re=badwords_re)
00229     else:
00230         page.send_page()
00231 

Here is the call graph for this function: