Back to index

moin  1.9.0~rc2
Classes | Functions | Variables
MoinMoin.macro.TableOfContents Namespace Reference

Classes

class  TOCFormatter

Functions

def macro_TableOfContents

Variables

list Dependencies = ['time']

Function Documentation

def MoinMoin.macro.TableOfContents.macro_TableOfContents (   macro,
  maxdepth = int 
)
Prints a table of contents.

 maxdepth:: maximum depth the table of contents is generated for (defaults to unlimited)

Definition at line 134 of file TableOfContents.py.

00134 
00135 def macro_TableOfContents(macro, maxdepth=int):
00136     """
00137 Prints a table of contents.
00138 
00139  maxdepth:: maximum depth the table of contents is generated for (defaults to unlimited)
00140     """
00141     try:
00142         mindepth = int(macro.request.getPragma('section-numbers', 1))
00143     except (ValueError, TypeError):
00144         mindepth = 1
00145 
00146     if maxdepth is None:
00147         maxdepth = 99
00148 
00149     pname = macro.formatter.page.page_name
00150 
00151     macro.request.uid_generator.push()
00152 
00153     macro.request._tocfm_collected_headings = []
00154     macro.request._tocfm_orig_formatter = macro.formatter
00155 
00156     tocfm = TOCFormatter(macro.request)
00157     p = Page(macro.request, pname, formatter=tocfm, rev=macro.request.rev)
00158 
00159     # this is so we get a correctly updated TOC if we just preview in the editor -
00160     # the new content is not stored on disk yet, but available as macro.parser.raw:
00161     p.set_raw_body(macro.parser.raw, modified=1)
00162 
00163     output = macro.request.redirectedOutput(p.send_page,
00164                                             content_only=True,
00165                                             count_hit=False,
00166                                             omit_footnotes=True)
00167 
00168     _ = macro.request.getText
00169 
00170     result = [
00171         macro.formatter.div(1, css_class="table-of-contents"),
00172         macro.formatter.paragraph(1, css_class="table-of-contents-heading"),
00173         macro.formatter.text(_('Contents')),
00174         macro.formatter.paragraph(0),
00175     ]
00176 
00177 
00178     # find smallest used level and use that as the outer-most indentation,
00179     # to fix pages like HelpOnMacros that only use h2 and lower levels.
00180     lastlvl = 100
00181     for lvl, id, txt in macro.request._tocfm_collected_headings:
00182         if txt is None:
00183             incl_id = id
00184             continue
00185         if lvl < mindepth or lvl > maxdepth or id is None:
00186             continue
00187         if lvl < lastlvl:
00188             lastlvl = lvl
00189 
00190     # headings are 1-based, lastlvl needs to be one less so that one is closed
00191     lastlvl -= 1
00192 
00193     for lvl, id, txt in macro.request._tocfm_collected_headings:
00194         if txt is None:
00195             incl_id = id
00196             continue
00197         if lvl < mindepth or lvl > maxdepth or id is None:
00198             continue
00199 
00200         # will be reset by pop_unique_ids below
00201         macro.request.include_id = incl_id
00202 
00203         need_li = lastlvl >= lvl
00204         while lastlvl > lvl:
00205             result.extend([
00206                 macro.formatter.listitem(0),
00207                 macro.formatter.number_list(0),
00208             ])
00209             lastlvl -= 1
00210         while lastlvl < lvl:
00211             result.extend([
00212                 macro.formatter.number_list(1),
00213                 macro.formatter.listitem(1),
00214             ])
00215             lastlvl += 1
00216         if need_li:
00217             result.extend([
00218                 macro.formatter.listitem(0),
00219                 macro.formatter.listitem(1),
00220             ])
00221         result.extend([
00222             '\n',
00223             macro.formatter.anchorlink(1, id),
00224             macro.formatter.text(txt),
00225             macro.formatter.anchorlink(0),
00226         ])
00227 
00228     while lastlvl > 0:
00229         result.append(macro.formatter.listitem(0))
00230         result.append(macro.formatter.number_list(0))
00231         lastlvl -= 1
00232 
00233     macro.request.uid_generator.pop()
00234 
00235     result.append(macro.formatter.div(0))
00236     return ''.join(result)

Variable Documentation

Definition at line 34 of file TableOfContents.py.