web/scripts docbookhtml2php.py,1.1.1.1,1.2

Karsten Wade (kwade) fedora-websites-list at redhat.com
Sun Feb 25 17:54:02 UTC 2007


Author: kwade

Update of /cvs/fedora/web/scripts
In directory cvs-int.fedora.redhat.com:/tmp/cvs-serv16421

Modified Files:
	docbookhtml2php.py 
Log Message:
pfrields fixed script, should solve the occasional problem we have with some DocBook output HTML


Index: docbookhtml2php.py
===================================================================
RCS file: /cvs/fedora/web/scripts/docbookhtml2php.py,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -r1.1.1.1 -r1.2
--- docbookhtml2php.py	30 Mar 2005 17:47:28 -0000	1.1.1.1
+++ docbookhtml2php.py	25 Feb 2007 17:54:00 -0000	1.2
@@ -1,144 +1,88 @@
 #!/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 <stickster at gmail.com>
+# This script is licensed under the GNU General Public License (GPL) v2.0.
+#
+# Designed after Tammy Fox's original script (C) 2003.
 
-## script to convert HTML generated from DocBook to PHP files for RHLP website
-## Copyright (C) 2003 Red Hat, Inc.
-## Copyright (C) 2003 Tammy Fox <tfox at redhat.com>
-
-## Author: Tammy Fox
-
-import sys
-import string
-import os
+import sys, os
 import re
-import tempfile
+from glob import glob
 
-debug = 0
+_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 <html_files>"
     sys.exit(2)
 
-if debug == 1:
-    print html_files
-
-path = os.getcwd()
-
-#open temp script file
-try:
-    tempfile.tempdir = path
-    tmpscriptfilename = tempfile.mktemp()
-    tmpscriptfile = open(tmpscriptfilename, 'w')
-    os.chmod(tmpscriptfilename, 0700)
-except IOError:
-    print "Error: can't create temp file"
-    sys.exit(0)    
-tmpscriptfile.write('#!/bin/bash\nfor i in *.php\ndo\ncat $i ')
+if _debug:  print html_files
 
 for file in html_files:
-
-## HEADER REPLACEMENT ##    
-    #open file
-    f = open(file, "r")
-
-    #read in contents of file
-    content =  f.read()
-
-    #reg expression to find header
-    replace_this = re.search('<.*alink.*?>', content, re.DOTALL | re.IGNORECASE)
-
-    if debug == 1:
-        print "replace:"
-        print replace_this.group()
-
-    #what to add to the HTML file
-    include_lines = "<?\n\ninclude(\"site.inc\");\n$template = new Page;\n$template->initCommon(); \n$template->displayHeader();\n\n?>\n\n"
-
-    if debug == 1:
-        print "with:"
-        print include_lines
-    
-    #open temp file
     try:
-        tempfile.tempdir = path
-        tmpfilename = tempfile.mktemp()
-        tmpfile = open(tmpfilename, 'w')
-        os.chmod(tmpfilename, 0600)
-    except IOError:
-        print "Error: can't create temp file"
-        sys.exit(0)
-    
-    #replace
-    new_content = re.sub(replace_this.group(), include_lines, content)
-    tmpfile.write(new_content)
-   
-    #close files
-    f.close()
-    tmpfile.close()
-
+        infile = open(file, "r")
+    except:
+        print "Could not open file", file
+        sys.exit(3)
+    else:
+        content = infile.read()
+        infile.close()
+    
+    header_include = ''.join(['<?\n\n',
+                              'include("site.inc");\n',
+                              '$template = new Page;\n',
+                              '$template->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',
+                              '<?\n\n',
+                              '$template->displayFooter(',
+                              "'$", 'Date', "$'",
+                              ');\n\n',
+                              '?>\n'])
+    content = re.sub('<.*alink.*?>', header_include, content)
 
-## FOOTER REPLACEMENT ##    
-
-    #open file
-    f = open(tmpfilename, "r")
-
-    #read in contents of file
-    content =  f.read()
-
-    #reg expression to find footer
-    replace_this = re.search('<\/body.*>', content, re.DOTALL | re.IGNORECASE)
-    
-    if debug == 1:
-        print "replace:"
-        print replace_this.group()
-
-    #what to add to the HTML file
-    # note the RCS Date tag broken up so that it is not modified in this file!
-    include_lines = "\n\n<?\n\n$template->displayFooter(\'$"+"Date"+"$\');\n\n?>\n"
-
-    if debug == 1:
-        print "with:"
-        print include_lines
-    
-    #open another temp file
+    content = re.sub('<\/body.*>', footer_include, content)
+    phpfilename = file.split('.html')[0] + '.php'
     try:
-        tempfile.tempdir = path
-        tmpfilename2 = tempfile.mktemp()
-        tmpfile2 = open(tmpfilename2, 'w')
-        os.chmod(tmpfilename2, 0600)
-    except IOError:
-        print "Error: can't create temp file"
-        sys.exit(0)
-    
-    #replace
-    new_content = re.sub(replace_this.group(), include_lines, content)
-    tmpfile2.write(new_content)
-   
-    #close files
-    f.close()
-    tmpfile2.close()
-
-    #remove intermediate temp file
-    if debug == 1:
-        print "removing", tmpfilename
-    os.remove(tmpfilename)
-    #mv temp file to real file
-    phpfilename = string.split(file, ".html")[0] + ".php"
-    if debug == 1:
-        print phpfilename
-    os.rename(tmpfilename2, phpfilename)
-    os.chmod(phpfilename, 0664)
-    #print completion msg
-    print file, "converted to", phpfilename
-    
-    
-## GENERATE REPLACE SCRIPT ##
-    tmpscriptfile.write('| sed s/' + str(file) + '/' + str(phpfilename) + '/g')
-
-## EXECUTE REPLACEMENT SCRIPT ##
+        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)
 
-tmpscriptfile.write(' > foo\n  mv foo $i\ndone')
-tmpscriptfile.close()
-os.system(str(tmpscriptfilename))
-os.remove(tmpscriptfilename)




More information about the Fedora-websites-list mailing list