Attachment 'EmbeddedPage-1.0.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - EmeddedPage macro Version 1.0, 25.06.2014
   4     
   5     Post-processes HTML from a formatted page to convert URL references to
   6     anchor links for pages provided in the arguments. It also changes HTML
   7     heading tag ids to match the anchor links. This macro imports the
   8     FormattedPage marco and is used when embedding one or more wiki pages into 
   9     the current page.
  10     
  11     Syntax:
  12        <<EmbeddedPage(pages)>>
  13        
  14     @copyright: 2014 Ian Riley <ian@riley.asia>
  15     license: GNU GPL, see COPYING for details.
  16 
  17 """
  18 
  19 from MoinMoin import wikiutil
  20 
  21 def execute(macro, args):
  22     request = macro.request
  23     page_name = macro.formatter.page.page_name
  24 
  25     i = -1
  26     pages = []
  27     page_anchors = []
  28     ltag = '<a href="%s">'
  29     htag = '<h%s id="%s'
  30     result = ''
  31     
  32     if args is not None:
  33         i, names = args.split(',', 1)
  34         i = int(i)
  35         pages = names.split(',')
  36 
  37     embed_page = pages[i]
  38     
  39     FormattedPage = wikiutil.importPlugin(macro.cfg, u'macro', u'FormattedPage')
  40     result = FormattedPage(macro, embed_page)
  41     
  42     # Convert URL links to embeded pages to anchor links
  43     for i, page in enumerate(pages):
  44         page_anchor = '#' + wikiutil.anchor_name_from_text(page) + '.top'
  45         page = request.script_root + '/' + page
  46         result = result.replace(ltag % page, ltag % page_anchor)
  47 
  48     # Repair ids and anchors - this is hack, better find away to stop the
  49     # formatter incremeniting the page_name.
  50     # Also, if a page is embeded twice this will only work on the first one.
  51     for i in range(2):
  52         suffix = '-' + str(i+1)
  53         page_anchor = wikiutil.anchor_name_from_text(embed_page)
  54         result = result.replace('#' + page_anchor + suffix,
  55                                 '#' + page_anchor)
  56 
  57         result = result.replace('id="' + page_anchor + suffix,
  58                                 'id="' + page_anchor)
  59                                
  60         for j in range(6):
  61             result = result.replace(htag % (str(j+1), page_anchor + suffix),
  62                                     htag % (str(j+1), page_anchor))
  63                                 
  64     return result

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