Changeset 4413

Show
Ignore:
Timestamp:
10/07/08 04:04:38 (3 months ago)
Author:
gotoh
Message:

Implement alternative macro of TOC macro for localized page support.
New macro is named as NTOC which is delived from TOCMacro.
So it accepts all the syntax supported by TOC macro.
If you specify the pages without suffix, NTOC macro find and use
localized pages.

As limitation, wildcard page specifying may not work as you expected
because page listing is not hooked by this macro.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tracwikinegotiatorplugin/0.10/wikinegotiator/macros.py

    r4368 r4413  
    11# -*- coding: utf-8 -*- 
    22import os, re 
     3from trac import __version__ as version 
    34from trac.core import * 
    45from trac.config import Option, ListOption, BoolOption 
     
    67from trac.wiki import WikiSystem, html 
    78from trac.wiki.macros import WikiMacroBase 
    8  
    9  
    10  
    11 ## macro 
     9from trac.wiki.model import WikiPage 
     10 
     11 
     12## alternative TitleIndex macro 
    1213 
    1314def _list_wiki_default_pages(): 
     
    158159                    ul.append(html.LI(item)) 
    159160        return html.TABLE(tr) 
     161 
     162 
     163 
     164# alternative TOC macro 
     165 
     166try: 
     167    # for delived new NTOC macro, require trac 0.11 and tractoc enabled. 
     168    assert [int(x) for x in version.split('.')] >= [0, 11] # need 0.11 
     169    import tractoc.macro                        # need enabled 
     170    import wikinegotiator.negotiator 
     171 
     172    class NTOCMacro(tractoc.macro.TOCMacro): 
     173        """Language-aware version of TOC Macro. 
     174 
     175        This macro is an alternative of TOC macro (written by 
     176        coderanger) extending to use content of localized page if 
     177        exist. You can write TOC macro entry by normal page name 
     178        wihtout lang suffix. 
     179 
     180        For example, if you specify the page 'SubPage' in argument on 
     181        the Japanese localized page 'BasePage.ja', find 'SubPage.ja' 
     182        first and return it's content if exist.  Or find localized 
     183        page regarding preferred languages from browser's 
     184        Accept-Language: header.  Or, finally, exact given page name 
     185        is used. 
     186 
     187        If you specify the page with lang suffix, that page is used. 
     188 
     189        === LIMITATION === 
     190         
     191        TOC macro accepts wildcard like `Trac*` to list multiple pages 
     192        but NTOC macro cannot hook it. In such case, all the variants 
     193        will match and be used. It'd be better not to use wildcard 
     194        with NTOC macro. 
     195        """ 
     196         
     197        _override_toc_macro = BoolOption('wiki-negotiator', 
     198                                         'override_toc_macro', 
     199                                         'enabled', 
     200                                         doc="""Expose  NTOC macro as TOC macro.""") 
     201 
     202        def get_page_text(self, formatter, page_resource): 
     203            """Return a tuple of `(text, exists)` for the given page (resource). 
     204            The page is altered if lang suffix is not exist in page name 
     205            like wiki page negotiation.  
     206            """ 
     207            if page_resource.id == formatter.context.resource.id: 
     208                return (formatter.source, True) 
     209            else: 
     210                req = formatter.context(page_resource).req 
     211                nego = wikinegotiator.negotiator.WikiNegotiator(self.env) 
     212                # know lang of parent page where this macro is on. 
     213                parent = req.args.get('page', 'WikiStart') 
     214                bname, blang = nego._split_lang(parent) 
     215                # get body name and lang from given page name. 
     216                name, lang = nego._split_lang(page_resource.id) 
     217                wiki = WikiSystem(self.env) 
     218                if lang is None: 
     219                    # When no lang suffix is in given page name, 
     220                    # find localized page for lang of parent page, 
     221                    # then preferred language. 
     222                    langs = [blang or nego._default_lang] 
     223                    langs += nego._get_preferred_langs(req) 
     224                    for lang in langs: 
     225                        dname = '%s.%s' % (name, lang) 
     226                        if wiki.has_page(dname): 
     227                            name = dname 
     228                else: 
     229                    # with suffix, exact page is used 
     230                    name = page_resource.id 
     231                page = WikiPage(self.env, name) 
     232                return (page.text, page.exists) 
     233 
     234    if _override_toc_macro: 
     235        # alter 'TOC' macro by NTOCMacro 
     236        class TOCMacro(NTOCMacro): 
     237            pass 
     238 
     239except: 
     240   # TOCMacro load fail 
     241   pass 
     242