view make/indexsections.py @ 99:010777961045

Add -b baseurl support.
author Rob Landley <rob@landley.net>
date Wed, 06 Aug 2008 15:04:36 -0500
parents 490dd648a9bf
children 88f40fbf8e8b
line wrap: on
line source

#!/usr/bin/python

import os,sys

debug = 0

# Print appropriate number of <ul> or </ul> tags, with indentation.
def adjust_depth(newdepth, depth):
  while newdepth > depth:
    sys.stdout.write("%s<ul>\n" % ("  "*depth))
    depth += 1
  while newdepth < depth:
    sys.stdout.write("%s</ul>\n" % ("  "*depth))
    depth -= 1
  return depth

# Read through HTML code to find and process some extra tags, describing
# content sections to be numbered and indexed.

def process(data, idx):
  line = data[0].count("\n")
  depth = 0
  spans = []
  secnum = [0]

  if not idx: sys.stdout.write(data[0])
  for i in data[1:]:
    i = i.split(">",1)
    tag = i[0].split(None,1)

    # The magic tag to output the condensed list of all numbered sections.
    # This recurses to call this function again, printing out different data.

    if tag[0] == "put_index_here":
      if not idx: process(data, 1)
      continue

    # Parse one of our magic "span" tags describing a section.

    if tag[0] == "span":
      if len(tag) == 0 or not tag[1].startswith("id="):
        sys.stderr.write("Bad span at line %s: %s" % (line,i[0]))
        sys.exit(1)
      tag = tag[1][3:]
      if tag[0]=='"' and tag[-1]=='"': tag=tag[1:-1]
      if debug: sys.stderr.write("tag %s\n" % tag)
      spans.append("_".join(tag.split()))
      secnum[-1] += 1
      secstr = ".".join(map(lambda a: str(a), secnum))

      # If we're writing out the index...
      if idx:
        depth = adjust_depth(len(spans)-1, depth)
        if depth: style="li"
        else: style="h2"
        if idx != 1: linkout=idx
        else: linkout=""
        sys.stdout.write('%s<%s><a href="%s#%s" name="%s">%s %s</a></%s>\n' % ("  "*len(spans),style,linkout,spans[-1],secstr,secstr,tag,style))

      # If we're writing out the contents...
      else:
        sys.stdout.write('<h2><a href="#%s" name="%s">%s %s</a></h2>\n' % (secstr,spans[-1],secstr,tag))
        sys.stdout.write('<%s>%s' % (i[0], i[1]))
      secnum.append(0)

    # End of a section, pop it off the stack.

    elif tag[0] == "/span":
      if debug: sys.stderr.write("pop %s\n" % spans.pop())
      else: spans.pop()
      secnum.pop()

    # This tag wasn't one of ours, so output it verbatim.  (It's HTML.)

    else:
      if not idx: sys.stdout.write('<%s>%s' % (i[0], i[1]));
    if debug: sys.stderr.write("i=%s\n" % i)
    if len(i)==1:
      sys.stderr.write("Unterminated tag, line %s: %s\n" % (line,i))
      sys.exit(1)
    if len(i): line += i[1].count("\n")
  if idx: adjust_depth(0, depth)

baseurl=None
if len(sys.argv) == 4 and sys.argv[1] == '-b':
  del sys.argv[1]
  baseurl=sys.argv[1]
  del sys.argv[1]

if len(sys.argv) != 2:
  sys.stderr.write("Usage: indexsections.py [-b http://baseurl] filename\n");
  sys.exit(1)

process(open(sys.argv[1]).read().split("<"), baseurl)