Attachment 'Quote.py'

Download

   1 """
   2     MoinMoin - Quote Macro
   3     
   4     This macro allows to insert a html <q cite=".." lang="...">Your quote </q>
   5     element.
   6 
   7     Usage:
   8        [[Quote("Your quote as long as you want")]]
   9        [[Quote("Your quote, more, more...", cite=http://source.net, language=en)]]
  10        [[Quote("Your quote, more, more...", cite=http://source.net, footnote=0)]]
  11 
  12     Please note: If a cite source is provided, by default a footnote is created. You
  13     can turn this behaviour of by seeting "footnote=0".
  14 
  15     For longer quotes please use the quote parser which outputs text inside a html
  16     <blockquote> ... </blockquote> element.
  17 
  18     @copyright: 2007 Oliver Siemoneit
  19     @license: GNU GPL, see COPYING for details.
  20 
  21 """
  22 
  23 from MoinMoin import wikiutil, i18n
  24 from MoinMoin.macro.FootNote import execute as make_footnote
  25 
  26 
  27 def _getArgs(given_arguments, allowed_arguments):
  28     if not given_arguments:
  29         return {}
  30     args = {}
  31     for s in given_arguments.split(','):
  32         if s and s.find('=') > 0:
  33             key, value = s.split('=', 1)
  34             if key and value:
  35                 key = key.strip()
  36                 if key in allowed_arguments:
  37                     args[key] = value.strip()
  38     return args
  39 
  40 def _get_formatterName(formatter):
  41     # XXX remove this function again. It's just a workaround since FormatterBase
  42     # has no 'def quote' yet.
  43     import inspect
  44     for key, value in inspect.getmembers(formatter, inspect.isclass(formatter)):
  45         if key == '__module__':
  46             return value
  47     return ''
  48 
  49 def execute(macro, options):
  50     allowed_args = ['cite', 'language', 'footnote']
  51     # parse given options
  52     try:
  53         text, given_args = options.rsplit('"', 1)
  54     except:
  55         return
  56     text = text.strip('"')
  57     args = _getArgs(given_args, allowed_args)
  58 
  59     # get cite source
  60     source = args.get('cite', None)
  61     footnote = int(args.get('footnote', '1'))
  62     cite = fn = ''
  63     if source:
  64         cite = ' cite="%s"' % wikiutil.escape(source)
  65         if footnote:
  66             fn = make_footnote(macro, wikiutil.escape(source))
  67     # get language
  68     language = args.get('language', None)
  69     lang = ''
  70     if language and (language in i18n.wikiLanguages()):
  71         lang = ' lang="%s"' % language
  72 
  73     # ouput quote
  74     # XXX change that in a "macro.formatter.quote" call
  75     if _get_formatterName(macro.formatter) == 'MoinMoin.formatter.text_html':
  76         output = '<q %s%s>' % (cite, lang) + text + '</q>' + fn
  77     else:
  78         ouput = macro.formatter.text(text) + fn
  79     return output

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2007-02-08 23:38:16, 2.5 KB) [[attachment:Quote.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.