Ejemplo n.º 1
0
    def expand_macro(self, formatter, name, content):
        args, kw = util.parse_args(content)
        prefix = args and args[0] or None
        format = kw.get('format', '')
        minsize = max(int(kw.get('min', 2)), 2)
        depth = int(kw.get('depth', -1))
        start = prefix and prefix.count('/') or 0

        wiki = formatter.wiki
        pages = sorted([page for page in wiki.get_pages(prefix) \
                        if 'WIKI_VIEW' in formatter.perm('wiki', page)])
        pagelangs = {}
        for page in pages:
            name, lang = util.split_lang(page, '')
            langs = pagelangs.get(name, [])
            if lang not in langs:
                langs.append(lang)
            pagelangs[name] = langs
        pages = sorted(pagelangs.keys()) # collection of default pages

        upages, spages = self.split_pages(pages)

        def format_page_name(page, split=False):
            try:
                # for trac 0.11                
                return wiki.format_page_name(page, split=split)
            except:
                # for trac 0.10
                if split:
                    return self.PAGE_SPLIT_RE.sub(r"\1 \2", page)
                return page
        def split(page):
            if format != 'group':
                return [format_page_name(page)]
            else:
                return self.SPLIT_RE.split(format_page_name(page, split=True))
        
        # Group by Wiki word and/or Wiki hierarchy
        upages, spages = [[(split(page), page) for page in pages
                           if depth < 0 or depth >= page.count('/') - start]
                          for pages in (upages, spages)]
                          
        def split_in_groups(group):
            """Return list of pagename or (key, sublist) elements"""
            groups = []
            for key, subgrp in groupby(group, lambda (k,p): k and k[0] or ''):
                subgrp = [(k[1:],p) for k,p in subgrp]
                if key and len(subgrp) >= minsize:
                    sublist = split_in_groups(sorted(subgrp))
                    if len(sublist) == 1:
                        elt = (key+sublist[0][0], sublist[0][1])
                    else:
                        elt = (key, sublist)
                    groups.append(elt)
                else:
                    for elt in subgrp:
                        groups.append(elt[1])
            return groups
Ejemplo n.º 2
0
 def get_page_text(self, *args):
     """Return a tuple of `(text, exists)` for the given page (resource).
     The page is altered if lang suffix is not exist in page name
     like wiki page negotiation. 
     """
     # Since TracTOC macro r4366, 'formatter' argument is added
     # as 2nd argument.
     if len(args) == 1:
         # old code
         formatter = self.formatter
         page_resource = args[0]
     else:
         formatter, page_resource = args
     if page_resource.id == formatter.context.resource.id:
         return (formatter.source, True)
     else:
         req = formatter.context(page_resource).req
         nego = wikinegotiator.negotiator.WikiNegotiator(self.env)
         # know lang of parent page where this macro is on.
         parent = req.args.get('page', 'WikiStart')
         bname, blang = util.split_lang(parent, 'default')
         # get body name and lang from given page name.
         name, lang = util.split_lang(page_resource.id)
         wiki = WikiSystem(self.env)
         if lang is None:
             # When no lang suffix is in given page name,
             # find localized page for lang of parent page,
             # then preferred language.
             langs = [blang] + util.get_preferred_langs(req)
             for lang in langs:
                 dname = util.make_page_name(name, lang)
                 if wiki.has_page(dname):
                     name = dname
                     break # from for
         else:
             # with suffix, exact page is used
             name = page_resource.id
         page = WikiPage(self.env, name)
         # update resource id to highlight current page.
         page_resource.id = name
         return (page.text, page.exists)
Ejemplo n.º 3
0
 def split_pages(self, pages):
     system_pages, exc = _exclude(self._wiki_default_pages,
                                  self._explicit_user_pages)
     # Pick all wiki pages and make list of master pages and also
     # make list of language variations for each master page.
     # The name of master page should not have language suffix.
     # And make list of user pages (non system page).
     user_pages = []                       # pages marked as user's
     lang_map = {}                       # language list for each page
     for page in pages:
         # note: language variant is not stored in system_pages.
         p, l = util.split_lang(page)
         if p not in system_pages and p not in user_pages:
             user_pages.append(p)
     # process explicit system pages
     user_pages, exc = _exclude(user_pages, self._explicit_system_pages)
     system_pages += exc
     system_pages = sorted(system_pages)
     return user_pages, system_pages
Ejemplo n.º 4
0
        def expand_macro(self, formatter, name, args):
            """Expand wildcard page spec to non-suffixed page names.
            """
            args, kw = util.parse_args(args)

            wiki = WikiSystem(self.env)
            newargs = []
            for arg in args:
                newargs.append(arg)
                arg = arg.strip()
                if arg.endswith('*'):
                    newargs.pop()
                    # expand wildcard as indivisual pages with removing
                    # suffixed pages.
                    prefix = arg[:-1]
                    pages = []
                    for page in wiki.get_pages(prefix):
                        name, lang = util.split_lang(page)
                        if name not in pages:
                            pages.append(name)
                    newargs += pages
            # reconstruct 'args' argument and call ancestor
            args = ','.join(newargs + ['%s=%s' % pair for pair in kw.items()])
            return BaseTOCMacro.expand_macro(self, formatter, name, args)