#! /usr/bin/env python
# Copyright (C) 1998-1999 by Dr. Dieter Maurer <dieter@handshake.de>
# D-66386 St. Ingbert, Eichendorffstr. 23, Germany
#
#			All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice and this permission
# notice appear in all copies, modified copies and in
# supporting documentation.
# 
# Dieter Maurer DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL Dieter Maurer
# BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
# PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
"""Add content table to html files.

usage: addContentTable [options] file ...

  adds content tables to the given files.

Recognized options:

  -s sec,...,sec  --  defines the sections for with a content table
                      should be generated
		      Default: H2,H3,H4

  -p place        --  defines, where the content table is being placed,
                      when there is not already a content table.
		      In this case, the table is placed after *place*.
		      Default: H1

  -d destdir      --  without this option, the file is overwritten
                      with this option, the modified file is written
		      to *destdir*

  -n              --  discard an existing content table before processing.
  -v              --  verbose
"""

from dmutil.ContentTable import ContentTableBuilder
from xml.dom.html_builder import HtmlBuilder
from xml.dom.writer import HtmlWriter
from getopt import getopt
from os import rename, getpid, unlink, path
from sys import exc_info, argv
from string import split, upper

def process(file):
  if verbose: print 'Processing: ' + file
  h= HtmlBuilder(); h.feed(open(file).read()); h.close()
  builder.addContentTable(h.document,newplace=newplace)
  tmpfile= destdir and path.join(destdir,path.basename(file)) or (file + pidsuffix)
  try:
    w= HtmlWriter(open(tmpfile,'w'))
    w.write(h.document)
    del w
    if not destdir:
      rename(file,file+'~')
      rename(tmpfile,file)
  except:
    (c,v)= exc_info()[:2]
    print "Exception `%s' during processing of `%s'" % (str(v),file)
    try: unlink(tmpfile)
    except: pass
  
sections='H2,H3,H4'
place='H1'
destdir= None
newplace=0
verbose=0

optlist, args= getopt(argv[1:],'s:d:p:nv')
for (o,v) in optlist:
  if o == '-s': sections= v
  elif o == '-p': place= v
  elif o == '-d': destdir= v
  elif o == '-n': newplace= 1
  elif o == '-v': verbose= 1
  else: raise ValueError, 'option %s not recognized' % o

sections= map(upper,split(sections,','))
place= upper(place)
builder= ContentTableBuilder(sections=sections,place=place)
pidsuffix= '@%d' % getpid()

if len(args) == 0: print __doc__
else:
  for f in args: process(f)


# Local Variables:
# mode: python
# End:
