#!/usr/bin/env python import sys try: import markdown import jinja2 except ImportError: print "You must have the Markdown and Jinja2 python libraries installed." sys.exit(-1) import os.path from HTMLParser import HTMLParser BASE_DIR = os.path.abspath(os.path.dirname(__file__)) SRC_DIR = os.path.join(BASE_DIR, 'src') OUT_DIR = os.path.join(BASE_DIR, 'out', 'doc') class TOCParser(HTMLParser): def __init__(self, tags=['h1', 'h2', 'h3']): HTMLParser.__init__(self) self.tags = tags self.toc = [] self.output = [] self.accum = None self.header_index = 1 self.online = True # Are we outputting the content we find? self.title = None def _convert_attr(self, attr): return "".join([ ' %s="%s"' % (key, value) for key, value in attr ]) def handle_starttag(self, tag, attr): if tag in self.tags: # Start tracking content assert self.accum is None self.accum = [] # Check if we're the first top level (interpret that as # the page title). level = self.tags.index(tag) if level == 0 and self.title is None: self.online = False return # Add a reference to this header id = 'h_%d' % self.header_index attr.append(('id', id)) # Link back to the top, unless we're already there. if self.header_index > 1: self.output.append('