NavigationLinks

Description

A macro to create previous and next page links for the calling page, based on the the caller's position in the FrontPage table of contents.

I wrote it for myself and my project, discarding entirely all rules for multiple languages and clean coding style.

Download & Release Notes

Download

Release Version

Moin Version

Release Notes

No Download (see the Source)

1.9.0

<<NavigationLinks()>>

Usage

<<NavigationLinks()>>

There are no parameters, as the macro examines the caller to determine current page (macro.formatter.page.page_name).

Source

   1 from MoinMoin.Page import Page
   2 from MoinMoin import wikiutil
   3 import re
   4 
   5 # do not cache
   6 Dependencies = ["time"]
   7 
   8 def macro_NavigationLinks(macro):
   9     '''
  10     Get's the front page list of contents, and builds an HTML table for previous and
  11     next pages based on the position of the caller in the list
  12     '''
  13     request = macro.request
  14     _ = request.getText
  15     response = ""
  16     
  17     # lists to hold urls and aliases [[URL|ALIAS]]
  18     myList = [] # captures the URLS
  19     myListOfNames = [] # captures the Aliases of the URLS
  20     
  21     # frontpage where, my contents list is
  22     frontPage = request.cfg.page_front_page
  23 
  24     # make page request through moin processor
  25     inc_page = Page(request, frontPage)
  26     #gets lines of body
  27     lines = inc_page.getlines()
  28 
  29     # current calling page
  30     thisPage = macro.formatter.page.page_name
  31 
  32     # find the page brief table of contents, capture the entire link
  33     # the frontpage list of contents would have to reflect what is really going on here
  34     #the pattern I have used is [SPACE][INTEGER][.][SPACE][LINK]
  35     regex = re.compile('^\s\d+\.\s\[\[(?P<url>[^\|]+)((\|)(?P<alias>[^\]]+))?\]\](\s+)?$', re.IGNORECASE)
  36     for line in lines:
  37         m = regex.match(line)
  38         if m:
  39             myList.append(m.group("url"))
  40 
  41             if m.group("alias") != None:
  42                 myListOfNames.append(m.group("alias"))
  43             else:
  44                 myListOfNames.append(m.group("url"))
  45 
  46     try:
  47         listIndex = myList.index(thisPage)
  48     except:
  49         listIndex = None
  50 
  51 
  52     if listIndex != None:
  53         response = '<table id="navigationLinksTable"><tr><td>'
  54 
  55         # Previous Page
  56         if listIndex > 0:
  57             response += '<a href ="%s">%s</a>' % (str(myList[listIndex - 1]),str(myListOfNames[listIndex - 1]))
  58         response += '</td><td>%s</td><td>' % (str(myListOfNames[listIndex]))
  59 
  60         # Next Page
  61         if listIndex < (len(myList) - 1):
  62             response += '<a href ="%s">%s</a>' % (str(myList[listIndex + 1]), str(myListOfNames[listIndex + 1]))
  63         response  += '</td></tr></table>'
  64 
  65     return macro.formatter.rawHTML(response)

MoinMoin: TobinCataldo/NavigationLinks (last edited 2009-12-18 15:08:31 by TobinCataldo)