#!/usr/bin/python # # Convert HTML generated from DocBook (i.e. xmlto) to PHP files for # fedora.redhat.com website # Copyright (C) 2006 Paul W. Frields # This script is licensed under the GNU General Public License (GPL) v2.0. # # Designed after Tammy Fox's original script (C) 2003. import sys, os import re from glob import glob _debug = 0 html_files = sys.argv[1:] mungelist = [] def mungeFileRefs(fname, htmlfilename): try: f = open(fname, 'r') except: print "Cannot open file", fname sys.exit(10) else: data = f.read() f.close() try: f = open(fname, 'w') except: print "Cannot write to file", fname sys.exit(11) else: f.write(data.replace(htmlfilename, htmlfilename.replace('.html', '.php'))) f.close() if html_files == []: print "Usage: docbookhtml2php.py " sys.exit(2) if _debug: print html_files for file in html_files: try: infile = open(file, "r") except: print "Could not open file", file sys.exit(3) else: content = infile.read() infile.close() header_include = ''.join(['initCommon();\n', '$template->displayHeader();\n\n', '?>\n\n']) # The PHP "Date" string is broken up to prevent CVS from seeing it footer_include = ''.join(['\n', 'displayFooter(', "'$", 'Date', "$'", ');\n\n', '?>\n']) content = re.sub('<.*alink.*?>', header_include, content) content = re.sub('<\/body.*>', footer_include, content) phpfilename = file.split('.html')[0] + '.php' try: outfile = open(phpfilename, 'w') except: print "Could not open output file", phpfilename sys.exit(4) else: outfile.write(content) outfile.close() mungelist.append(file) if _debug: print mungelist phpfiles = glob("*.php") for phpfile in phpfiles: for m in mungelist: mungeFileRefs(phpfile, m)