Attachment 'SystemPageLink-1.9.0-1.py'

Download

   1 # -*- coding: utf-8 -*-
   2 """
   3     MoinMoin - Create a link to system page based on used locale.
   4 
   5     This macro adds link to system page (page which included in i18n
   6     "all_pages" list).
   7 
   8     Usage: <<SystemPageLink(pagename, [description, [lang, [anchor, 
   9                         [params]]]])>>
  10            <<SystemPageLink(pagename, [description="description string"],
  11                         [lang="iso 639-1 lang code"], [anchor="anchor name"],
  12                         [params="url query params"])>>
  13 
  14     @param pagename: name of system page linked to. Should be in i18n 
  15            "all_pages" list, otherwise error returned. 
  16     @param description: description string, used as link text.
  17     @param lang: Language which should be used. Value should be iso 639-1
  18            language code, or "page" for page language, "user" for user
  19            language, "default" for default wiki instance language. If no
  20            language provided, macro tries to find existing page with name
  21            translated to language in next order: page language, user default
  22            language, instance default language, english (used even page doesn't
  23            exist).
  24     @param anchor: Anchor on page link should point to. By default no anchor 
  25            pointed.
  26     @param params: URL query params. No params added by default.
  27     
  28     @return Formatted link if pagename in i18n "all_pages" list, formatted
  29             error message text in other case.
  30 
  31     @copyright: 2009 MoinMoin:EugeneSyromyatnikov
  32     @license: GNU GPL, see COPYING for details.
  33 """
  34 
  35 from MoinMoin import wikiutil
  36 from MoinMoin import i18n
  37 from MoinMoin.i18n import strings
  38 from MoinMoin.Page import Page
  39 
  40 Dependencies = ["language"]
  41 
  42 def macro_SystemPageLink(macro, pagename=u'', description=u'', lang=u'',
  43     anchor=u'', params=u''):
  44     _ = macro.request.getText
  45 
  46     if pagename in i18n.strings.all_pages:
  47         page_lang = macro.formatter.page.get_pi()['language']
  48         use_lang = lang
  49 
  50         #using array instead of dict to preserve order of items
  51         langs = [
  52             ('page', page_lang),
  53             ('user', macro.request.lang),
  54             ('default', macro.formatter.page.request.cfg.language_default),
  55         ]
  56 
  57         if lang:
  58             use_lang = lang
  59             for lang_name, lang_val in langs:
  60                 if lang == lang_name:
  61                     use_lang = lang_val
  62                     break
  63         else:
  64             for language in [lang[1] for lang in langs] + ['en']:
  65                 use_lang = language
  66                 if Page(macro.request, i18n.getText(pagename, macro.request,
  67                     use_lang)).exists():
  68                     break
  69 
  70         __ = lambda text: i18n.getText(text, macro.request, use_lang)
  71 
  72         return ''.join([
  73                 macro.formatter.pagelink(1, __(pagename), anchor=anchor,
  74                     querystr=params),
  75                 macro.formatter.text(description and
  76                     __(description) or __(pagename)),
  77                 macro.formatter.pagelink(0),
  78             ])
  79     else:
  80         return  macro.formatter.text(_("(SystemPageLink macro error: can't "
  81             "find page \"%(pagename)s\" in list of all system pages)" % {
  82             'pagename': pagename,
  83         }))

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] (2009-11-30 06:32:30, 3.2 KB) [[attachment:SystemPageLink-1.9.0-1.py]]
 All files | Selected Files: delete move to page copy to page

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