# HG changeset patch # User Paul Boddie # Date 1533328495 -7200 # Node ID d234e6e97a5cf92026b4584354bd7cd6b2a74138 # Parent 8874a2ac2c6dae1a1763d297cf01285b7a0cb898 Added linking to the different headings from the table. diff -r 8874a2ac2c6d -r d234e6e97a5c moinformat/macros/toc.py --- a/moinformat/macros/toc.py Wed Aug 01 15:35:40 2018 +0200 +++ b/moinformat/macros/toc.py Fri Aug 03 22:34:55 2018 +0200 @@ -20,7 +20,8 @@ """ from moinformat.macros.common import Macro -from moinformat.tree.moin import Container, Heading, List, ListItem, Text +from moinformat.serialisers.common import make_id +from moinformat.tree.moin import Container, Heading, Link, List, ListItem, Text class TableOfContents(Macro): @@ -63,7 +64,6 @@ marker = "1." space = " " num = "1" - nl = [Text("\n")] # Start with no lists, no current item. @@ -89,7 +89,7 @@ # level or whether there are intermediate levels being # produced. - nodes = level == new_level and heading.nodes[:] + nl or [] + nodes = level == new_level and self.get_entry(heading) or [] indent = level - 1 # Create a new item for the heading or sublists. @@ -148,7 +148,7 @@ if min_level <= level <= max_level: indent = level - 1 - nodes = heading.nodes[:] + nl + nodes = self.get_entry(heading) item = ListItem(nodes, indent, marker, space, None) items.append(item) @@ -169,6 +169,13 @@ elif isinstance(n, Container): self.find_headings(n, headings) + def get_entry(self, heading): + + "Return nodes for an entry involving 'heading'." + + target = make_id(heading.text_content()) + return [Link(heading.nodes[:], "#%s" % target), Text("\n")] + macro = TableOfContents # vim: tabstop=4 expandtab shiftwidth=4