Attachment 'Cite.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """MoinMoin - Cite Macro
   3 
   4 A macro to insert citations from a bibliography into the text.
   5 
   6 Syntax:
   7     [[Cite(key[, "before"[, "after"]])]]
   8 
   9 Parameters:
  10     key    -- key of the entry in the bibtex database
  11     before -- text appearing before the reference link, needs to be
  12               wrapped in single- or double-quotes
  13     after  -- text appearing after the reference link, needs to be
  14               wrapped in single- or double-quotes
  15 
  16 Notes:
  17 This macro is intended to be used with the Bibliography macro.
  18 
  19 $Id: Cite.py,v 1.1.1.1 2007-06-07 20:36:15 gber Exp $
  20 Copyright 2007 by Guido Berhoerster <guido+moinmoin@berhoerster.name>
  21 Licensed under the GNU GPL, see COPYING for details.
  22 
  23 $Log: Cite.py,v $
  24 Revision 1.1.1.1  2007-06-07 20:36:15  gber
  25 initial import into CVS
  26 
  27 
  28 """
  29 
  30 import re
  31 
  32 Dependencies = []
  33 
  34 class Cite:
  35     """
  36     A citation in the text with a link to a (possible) corresponding
  37     bibliography entry.
  38 
  39     """
  40     def __init__(self, macro, args):
  41         args_re = re.compile(r'''
  42 ^(?P<key>[^,]+) # citation key (mandatory)
  43 (
  44 (,\s*((?P<bquote>[\'"])(?P<before>.*?)(?P=bquote))?\s*)? # text before citation
  45 (,\s*((?P<aquote>[\'"])(?P<after>.*?)(?P=aquote))?\s*)?  # text after citation
  46 )?''', re.UNICODE|re.VERBOSE)
  47         self.macro = macro
  48         self.args = args
  49         self.request = self.macro.request
  50         self._ = self.request.getText
  51         self.args_match = args_re.match(self.args)
  52 
  53     def run(self):
  54         _ = self._
  55         output = []
  56         if not self.args_match:
  57             output.append(self.macro.formatter.span(1, css_class="error"))
  58             output.append(self.macro.formatter.escapedText(
  59                          _('Error: Invalid Cite arguments: "%(args)s"') %
  60                          {"args": self.args}))
  61             output.append(self.macro.formatter.span(0))
  62             return "".join(output)
  63 
  64         if self.args_match.group("before"):
  65             output.append(self.macro.formatter.escapedText(\
  66                     self.args_match.group("before")))
  67         output.append(self.macro.formatter.anchorlink(1, name="ref:%s" %
  68                                                 self.args_match.group("key")))
  69         output.append(self.macro.formatter.escapedText(self.args_match.group(
  70                                                                        "key")))
  71         output.append(self.macro.formatter.anchorlink(0))
  72         if self.args_match.group("after"):
  73             output.append(self.macro.formatter.escapedText(
  74                                                self.args_match.group("after")))
  75 
  76         return "".join(output)
  77 
  78 
  79 def execute(macro, args):
  80     cite = Cite(macro, args)
  81     return cite.run()

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-06-07 21:38:39, 2.6 KB) [[attachment:Cite.py]]
 All files | Selected Files: delete move to page copy to page

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