#! /usr/bin/python -u # html-index Copyright (c) 1997-2000 Alex Schroeder """HTML-INDEX USAGE html-toc [-dv] files... OPTIONS -d debuggin -v verbose EXAMPLES create site-index.html and link-index.html for HTML files in the current directory: html-index * FILES Running this script will overwrite existing site-index.html and link-index.html files. The site-index.html file will contain a list of index entries. These index entries must be indicated in the HTML files as follows: The link-index.html file will contain a list of links from your HTML files to the rest of the web. These index entries must be indicated in the HTML files as follows: Some Text The title attribute is used to label the entries; the name attribute is used to create a link pointing from the index files back to the original occurence of the entry. If an anchor tag contains href, name and title attribute, it will be added to link-index.html. If an anchor tag contains name and title attribute but no href attribute, it will be added to site-index.html. Anchor tags lacking name or title attributes are not added to either index. BUGS It's slow: the old Perl implementation was faster. Generating n indexes requires parsing all files n times. """ import getopt import sys import string # import re # import os import htmllib import formatter def main(debug=0): try: opts, args = getopt.getopt(sys.argv[1:], 'vd') except getopt.error, msg: sys.stdout = sys.stderr print msg print __doc__%globals() sys.exit(2) for o, a in opts: if o == '-v': debug = debug + 1 if o == '-d': debug = debug + 2 if len(args) < 1: sys.exit("No files given on command line") Index(input=args[0:], output='site-index.html', page_header=site_index_page_start, page_footer=site_page_footer, required=('title', 'name'), prohibited=('href',), format_func=link_file_and_name, debug=debug).create() Index(input=args[0:], output='link-index.html', page_header=link_index_page_start, page_footer=site_page_footer, required=('title', 'name', 'href'), format_func=link_href, debug=debug).create() def link_file_and_name(link): return "%s" % (link['file'], link['name'], link['title']) def link_href(link): return "%s (Go!)" % (link['file'], link['name'], link['title'], link['href']) site_index_page_start = """ Site Index

Navigation:

Index

""" link_index_page_start = """ Link Index

Navigation:

Links

""" site_page_footer = """


Navigation: Top,



""" class Index(htmllib.HTMLParser): def __init__(self, tag='a', input=(), output=None, page_header='', page_footer='', format_func=None, required=('title',), prohibited=(), debug=0): """Create an index for all tags TAG from the input files in the list INPUT. Write the index to the output file OUTPUT. Use only the TAG tags which have all the attributes in REQUIRED set and none of the attributes in PROHIBITED. Note that if PROHIBITED contains only one value, it must still be a list; do not forget the comma in the list: ('bad stuff',). Note also that the title attribute should always be part of the required set of attributes since the index entries are sorted by title attribute.""" # Init parser. htmllib.HTMLParser.__init__(self, formatter.NullFormatter()) self.required = required self.prohibited = prohibited self.page_header = page_header self.page_footer = page_footer self.format_func = format_func self.output = output self.debug = debug self.links = [] if debug > 2: print tag, input, output, required, prohibited # Open all the files, feed the data to the parser and close the files. # Then close the parser. for file in input: if file == output: continue if debug: print "reading", file f = open(file) self.input = file self.feed(f.read()) f.close() self.close() def start_a(self, attributes): link = { 'file': self.input } for attr in attributes: (name, value) = attr link[name] = value; for key in self.required: if not link.has_key(key): if self.debug > 1: print link, "rejected: no", key return for key in self.prohibited: if link.has_key(key): if self.debug > 1: print link, "rejected: found", key return if self.debug > 1: print link, "accepted" self.links.append(link) def end_a(self): pass def create(self): self.links.sort(sort_by_title) index = '' menu = '' letter = '' for link in self.links: this_letter = string.upper(link['title'][0]) if this_letter != letter: letter = this_letter menu = menu + '%s\n' % (letter, letter) index = index + '

%s

\n' % (letter, letter) index = index + '%s
\n' % self.format_func(link) f = open(self.output, 'w') f.write(self.page_header) f.write(menu) f.write(index) f.write(self.page_footer) f.close() def sort_by_title(a, b): return cmp(string.lower(a['title']), string.lower(b['title'])) if __name__ == '__main__': main()