Back to index

moin  1.9.0~rc2
FootNote.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - FootNote Macro
00004 
00005     Collect and emit footnotes. Note that currently footnote
00006     text cannot contain wiki markup.
00007 
00008     @copyright: 2002 Juergen Hermann <jh@web.de>,
00009                 2007 MoinMoin:ReimarBauer,
00010                 2007 Johannes Berg
00011     @license: GNU GPL, see COPYING for details.
00012 """
00013 
00014 from MoinMoin import config, wikiutil
00015 from MoinMoin.parser.text_moin_wiki import Parser as WikiParser
00016 from MoinMoin.support.python_compatibility import hash_new
00017 
00018 Dependencies = ["time"] # footnote macro cannot be cached
00019 
00020 def execute(macro, args):
00021     request = macro.request
00022     formatter = macro.formatter
00023 
00024     # create storage for footnotes
00025     if not hasattr(request, 'footnotes'):
00026         request.footnotes = {}
00027         request.footnote_ctr = 0
00028         request.footnote_show_ctr = 0
00029 
00030     if not args:
00031         return emit_footnotes(request, formatter)
00032     else:
00033         # grab new footnote backref number
00034         idx = request.footnote_ctr
00035         request.footnote_ctr += 1
00036 
00037         shahex = hash_new('sha1', args.encode(config.charset)).hexdigest()
00038         backlink_id = "fndef-%s-%d" % (shahex, idx)
00039         fwdlink_id = "fnref-%s" % shahex
00040 
00041         if not args in request.footnotes:
00042             showidx = request.footnote_show_ctr
00043             request.footnote_show_ctr += 1
00044             request.footnotes[args] = ([], fwdlink_id, showidx)
00045         flist, dummy, showidx = request.footnotes[args]
00046         request.footnotes[args] = (flist + [(backlink_id, idx)], fwdlink_id, showidx)
00047 
00048         # do index -> text mapping in the same dict, that's fine because
00049         # args is always a string and idx alwas a number.
00050         request.footnotes[idx] = args
00051 
00052         return "%s%s%s%s%s" % (
00053             formatter.sup(1),
00054             formatter.anchorlink(1, fwdlink_id, id=backlink_id),
00055             formatter.text(str(showidx+1)),
00056             formatter.anchorlink(0),
00057             formatter.sup(0), )
00058 
00059     # nothing to do or emit
00060     return ''
00061 
00062 
00063 def emit_footnotes(request, formatter):
00064     # emit collected footnotes
00065     if request.footnotes:
00066         result = []
00067 
00068         result.append(formatter.div(1, css_class='footnotes'))
00069 
00070         # Add footnotes list
00071         result.append(formatter.number_list(1))
00072         subidx = 0
00073         for ctr in range(request.footnote_ctr):
00074             fn_txt = request.footnotes[ctr]
00075             if not fn_txt in request.footnotes:
00076                 continue
00077             this_txt_footnotes, fwdlink_id, showidx = request.footnotes[fn_txt]
00078             # this text was handled
00079             del request.footnotes[fn_txt]
00080 
00081             result.append(formatter.listitem(1))
00082             result.append(formatter.paragraph(1))
00083             result.append(formatter.anchorlink(1, None, id=fwdlink_id))
00084             result.append(formatter.anchorlink(0))
00085             result.append(wikiutil.renderText(request, WikiParser, fn_txt))
00086 
00087             items = []
00088             # ToDo check why that loop is needed?
00089             for backlink_id, idx in this_txt_footnotes:
00090                 # Add item
00091                 item = formatter.anchorlink(1, backlink_id)
00092                 item += formatter.text(str(subidx+1))
00093                 item += formatter.anchorlink(0)
00094                 items.append(item)
00095                 subidx += 1
00096 
00097             result.append(formatter.text(" ("))
00098             result.append(formatter.text(" ").join(items))
00099             result.append(formatter.text(")"))
00100 
00101             result.append(formatter.paragraph(0))
00102             result.append(formatter.listitem(0))
00103 
00104         result.append(formatter.number_list(0))
00105 
00106         # Finish div
00107         result.append(formatter.div(0))
00108 
00109         del request.footnotes
00110         del request.footnote_ctr
00111         del request.footnote_show_ctr
00112         return ''.join(result)
00113 
00114     return ''
00115