view make/ @ 87:490dd648a9bf

Better debugging info.
author Rob Landley <>
date Wed, 07 Nov 2007 23:41:23 -0600
parents 75251bfa6b33
children 010777961045
line wrap: on
line source


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)

    # 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]))
      tag = tag[1][3:]
      if tag[0]=='"' and tag[-1]=='"': tag=tag[1:-1]
      if debug: sys.stderr.write("tag %s\n" % tag)
      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"
        sys.stdout.write('%s<%s><a href="#%s" name="%s">%s %s</a></%s>\n' % ("  "*len(spans),style,spans[-1],secstr,secstr,tag,style))

      # If we're writing out the contents...
        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]))

    # 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()

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

      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))
    if len(i): line += i[1].count("\n")
  if idx: adjust_depth(0, depth)

if len(sys.argv) != 2:
  sys.stderr.write("Usage: filename\n");

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