1#!/usr/bin/env python3
 2
 3"""Table of contents widget."""
 4
 5
 6
 7from chart.reports.widget import Widget
 8
 9
10class TableOfContents(Widget):
11    """Insert a table of contents listing headings and subheadings."""
12
13    name = 'table-of-contents'
14
15    thumbnail = 'widgets/toc.png'
16
17    def __init__(self):
18        super(TableOfContents, self).__init__()
19        self.output = None
20
21    def html(self, document):
22        self.output = document.html
23
24    def post_html(self, document):
25        # html = document.html
26        html = self.output
27        html.write("""<h2>Table of contents</h2>
28  <ol>
29""")
30        for heading_cc, heading in enumerate(document.headings):
31            html.write('    <li>'
32                       '<a href="#heading-{heading_cc}">'
33                       '{heading}'
34                       '</a>\n'.format(heading_cc=heading_cc + 1,
35                                       heading=heading))
36
37            if len(document.subheadings[heading_cc]) > 0:
38                html.write('      <ul class="nobullet">\n')
39                for subheading_cc, subheading in enumerate(document.subheadings[heading_cc]):
40                    html.write(
41                        '        <li>{heading_cc}.{subheading_cc}. '
42                        '<a href="#subheading-{heading_cc}-{subheading_cc}">'
43                        '{subheading}</a></li>\n'.format(subheading_cc=subheading_cc + 1,
44                                                         heading_cc=heading_cc + 1,
45                                                         subheading=subheading))
46
47                html.write("      </ul>\n")
48
49            html.write("    </li>\n")
50
51        html.write("  </ol>\n")
52        html.write("<p>&#160;</p>\n")