| | 161 | |
|---|
| | 162 | |
|---|
| | 163 | |
|---|
| | 164 | # alternative TOC macro |
|---|
| | 165 | |
|---|
| | 166 | try: |
|---|
| | 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 | |
|---|
| | 239 | except: |
|---|
| | 240 | # TOCMacro load fail |
|---|
| | 241 | pass |
|---|
| | 242 | |
|---|