Skip to content

EditMenu

dossist edited this page Sep 16, 2014 · 29 revisions

Editing Menu

Basic knowledge of Python coding is required!

You can define your own menu in wikiconfig.py and override the default one.

Menu is built up from two variables: a list and a dictionary. The list consists of names, while the dictionary carries detailed information (label, where to link to etc) on each name in the list.

Config

You can customize the menu via following variable and method in wikiconfig.py.

  • memodump_menuoverride
    A list of strings which defines menu itself.
    It will override, not be appended to, the default menu list.

  • memodump_menu_def(request)
    An optional method to return a dictionary of menu item definitions.
    Define this when you need detailed definitions of menu entries, otherwise they are automatically generated. (described later)
    Returned dictionary will be appended to the default dictionary and will overwrite any overlapping keys.

The default values in the theme source will serve as examples.

memodump_menuoverride

Menu items are assembled in the order they defined in this list. Details will be pulled (if any) from the definition dictionary, otherwise they are automatically generated according to how item names are defined.

Example:

    memodump_menuoverride = [
        '----',                # horizontal rule
        '===== Display =====', # menu header
        'raw',                 # action name
        'print',               # action name
        '----',
        '===== Navigation =====',
        'RecentChanges',       # not action name, but defined in menu_def
        '----',                # auto-removed if there is no entry below
        '===== Edit =====',    # same as above
        'RenamePage',          # action only available for users with permission,
        'DeletePage',          # and auto-removed if unavailable
    ]

Names in this list are interpreted in several ways.

  • An action name, e.g. 'raw'
    Will turn into a link to the given action.
  • Header title, e.g. '== Header =='
    Decorate the name in the way we do for headers in wiki syntax.
  • Separator, e.g. '----'
    Again, just write what we do for rules in wiki syntax.
    '__separator__' will also be accepted as a rule.
  • A name defined in the dictionary
    Will pull information from the dictionary rather than automatically generate some.
    Label string, link destination, query string and contextual removal can be controlled.

Headers and separators are automatically culled if they have no entries between them.
Details on the action name and the defined name are below.

Action names

MoinMoin has additional functions called action which does various works besides normal page display such as editing pages, searches and setting user preferences. These can be accessed via query string in URL (e.g. http://site/page?action=something).

You can simply add a name of an action to the list, and it will automatically turn into a link to the current page with given action if:

  • No entry was found in the dictionary
  • Or there was an entry for the name and there still was no information about link destination ('href' and 'args') in the entry (giving 'title' only will work)

If given action is not allowed, that item will be ignored.

Names defined in the dictionary

Define details in the dictionary to turn menu items into manually tailored links.

A list item will turn into a link to a page if:

  • You had an entry for the item in the dictionary
  • And the entry contained information on 'href' and/or 'args'

If 'href' is omitted and only 'args' is set, 'href' will automatically be set to the address of the current page. Details on dictionary will be described below.

memodump_menu_def(request)

Optionally, define a method which returns a definitions dictionary which will be merged into the default one held in theme source. Names in the list memodump_menuoverride will be looked up on the dictionary to see if there is some data. If there is some, it will be used. If not, data will be automatically generated.

Example:

   def memodump_menu_def(self, request):
        _ = request.getText   # for localized text

        dictionary = {
            'raw': {
                'title': _('Raw Text'), # _('text') to get localized 'text'
                'href': '', # False values or nonexistent for current page
                'args': {},
                'special': False, # False for normal display
            },
        }
        return dictionary

'raw' is a name in the list memodump_menuoverride that you want to define details about.

title

Label for the item. Use request.getText() to get a translated string.

href

Link destination. Omit this or set a False value (False, None, '', etc) to automatically use the URL of the current page.

args

A dictionary which defines query string. e.g.

{'action': 'edit', 'editor': 'text'}

will generate

'?action=edit&editor=text'

and append it to the end of href.

Omit both href and args (or set False values on them) to have the key interpreted as an action name and get both href and args automatically generated.

special

Controls how item will be displayed. It could be:

  • False
    Normal display
  • 'removed'
    Completely removed
  • 'disabled'
    Show disabled link with grayed-out text
  • 'header'
    Show header string. You can get a header without this, see sections above.
  • 'separator' Show a horizontal rule.

Examples to use this feature is found below.

Advanced example

   def memodump_menu_def(self, request):
        _ = request.getText   # for localized text
        d = request.themedict # holds useful data such as current page
        page = d['page']      # current page object
        rev = request.rev     # revision of current page

        from MoinMoin.Page import Page
        page_japanese_dict = Page(request, u'JapaneseDict')

        dictionary = {
            # you can add actions manually even if key doesn't match action name
            'another_raw': {
                'title': _('Raw Text'),
                # url of the current page
                # not needed as this is auto generated for the current page
#               'href': page.url(request),
                # specify raw action manually
                # we need 'rev' in case page is not the newest revision
                'args': {'action': 'raw', 'rev': rev},
                # if action is not available, display a disabled link rather than remove it
                # if action is available, this field becomes False
                'special': not request.memodumpIsAvailableAction(page, 'raw') and 'disabled',
            },
            # edit link to a page named JapaneseDict.
            'editJapaneseDict': {
                'title': _('Edit JapaneseDict'),
                'href': page_japanese_dict.url(request),
                'args': {'action': 'edit'},
                'special': not request.memodumpIsEditablePage(page_japanese_dict) and 'removed',
            },
        }
        return dictionary

Utility methods in request object

Following utility methods will be added to request object by memodump theme.

  • request.memodumpIsAvailableAction(page, action)
    Returns True if action for page is available for current user, otherwise returns False.
  • request.memodumpIsEditablePage(page)
    Returns True if page is editable by current user, otherwise returns False.