1 # -*- coding: utf-8 -*-
   2 """
   3 Based on acronyms.py, tries a similar thing but without the
   4 carets surrounding the words to be expanded and supports
   5 optional URLs.  Definitions look like:
   6         entry :: this is a tool tip[| this is a url]
   7 
   8 Note that it does not use the formatter correctly. The rule is: do never nest calls to the formatter.
   9 
  10 Example:
  11 #FORMAT glossary
  12 #pragma glossary-definitions GlossaryDefs
  13 """
  14 
  15 from MoinMoin.parser import wiki
  16 from MoinMoin.wikidicts import Dict
  17 import re
  18 
  19 class Parser(wiki.Parser):
  20     def __init__(self, raw, request, **kw):
  21         self.glossary_dict = Dict(request, request.getPragma('glossary-definitions'))
  22         # It turns out that all but the first dict key had a leading space so I need
  23         # this kludge and some work with strip below to make it work.
  24         self.d = dict(zip([k.strip() for k in self.glossary_dict.keys()], self.glossary_dict.values()))
  25         words = ur'(?P<abbr>\s?\b'+ ur'|'.join(self.d.keys()) + ur'\b)'
  26         self.formatting_rules = words + '\n' + self.formatting_rules
  27         wiki.Parser.__init__(self, raw, request, **kw)
  28 
  29     def _abbr_repl(self, word):
  30         txt = self.request.formatter.text
  31         sword = word.strip()
  32         if not self.d.has_key(sword):
  33             return txt(word)
  34         else:
  35             value = (self.d[sword]).replace('"','&quot;')
  36             var = value.split('|')
  37             if len(var) == 1:
  38                 frag = '<abbr title="%s">%s</abbr>' % (txt(var[0]), word)
  39             else:
  40                 frag = '<a href="%s"><abbr title="%s">%s</abbr></a>' % (txt(var[1]), txt(var[0]), word)
  41             return self.request.formatter.rawHTML(frag)

MoinMoin: parser/glossary.py (last edited 2007-10-29 19:09:19 by localhost)