#! /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 = """
""" link_index_page_start = """
""" site_page_footer = """
Navigation: Top,
\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()