#! /usr/bin/env python # # The purpose of this script is to convert files which use legitimate # server side include (SSI) techniques (which requires the server to # support the 'include' statement) to those which merely simulate # SSI by turning that which would be included into that which is written # by Javascript calls when viewed. Javascript, Free webservers are not likely # to support services like ASP which allow SSI, but pretty much any browser # supports Javascript. # # Why is SSI so useful? Because it allows many pages to use the same # HTML code, which is all stored in a single file. This is especially # useful for headers and footers for webpages. # # In this script, a 'parent page' is a page which might contain an # include reference to an include file, and an include file is, well.. # just that. # # On my own computer, I have a webserver supporting ASP. This allows me to # do development on my webpages which are done the 'correct' way, and then # use this script to creat the 'hacked' Javascript version automatically. # Once they are hacked, they are not pretty to look at, and so that's why # I only convert right before I publish them. # # Author: John Lehmann # Date: 2-16-05 # URL: www.jplehmann.com # # Date: 2-20-05: disabled functionality that copies .asp files to .html import os, re # All files with the PARENT_EXT will be searched for the include directive, # which will be changed to the javascript include. Also, these # files will be changed to have the extension PARENT_NEW_EXT, and any reference # to their previous names will be updated in the INCLUDE_PAGES and PARENT_PAGES # Note: this reference matching is fairly weak, just looking for the filename # in quotes. It prints out all changes it makes. PARENT_EXT = ".asp" PARENT_NEW_EXT = ".html" PARENT_PAGES = {} for file in os.listdir(os.getcwd()): if os.path.splitext(file)[1] == PARENT_EXT: PARENT_PAGES[file] = os.path.splitext(file)[0]+PARENT_NEW_EXT # This is how the include files will be found. Include filenames are # changed in a like manner to the PARENT_PAGES. INCLUDE_EXT = ".inc" INCLUDE_NEW_EXT = ".inc.js" INCLUDE_PAGES = {} for file in os.listdir(os.getcwd()): if os.path.splitext(file)[1] == INCLUDE_EXT: INCLUDE_PAGES[file] = os.path.splitext(file)[0]+INCLUDE_NEW_EXT # Directory to output the produced files DEST_DIR = "." ABS_DEST_DIR = os.path.abspath(DEST_DIR) # create the directory if necessary if not os.path.exists(ABS_DEST_DIR): os.mkdir(ABS_DEST_DIR) # function update any references to changing parent filenames def updateLinks(line): for parent in PARENT_PAGES.keys(): # replace any instances of the old parent reference with the new one line2 = line.replace("\"%s\"" % (parent), "\""+PARENT_PAGES[parent]+"\"") if line != line2: print "Changed reference:", line2 line = line2 return line # Step 1: convert the include pages to Javascript for ifilename in INCLUDE_PAGES: ifile = open(ifilename, 'r') ofilename = os.path.join(ABS_DEST_DIR, INCLUDE_PAGES[ifilename]) ofile = open(ofilename, 'w') for line in ifile: line = line.strip() #line = updateLinks(line) if len(line) > 0: line = "document.writeln(" + repr(line) + ")" ofile.write(line + "\n") # Step 2: convert the references to the include pages in the parent pages #for ifilename in PARENT_PAGES.keys(): # ifile = open(ifilename, 'r') # # rename the extention to .html # ofilename = os.path.join(ABS_DEST_DIR, PARENT_PAGES[ifilename]) # ofile = open(ofilename, 'w') # for line in ifile: # line = line.strip() # if len(line) > 0: # # detect any reference to the inclusion of a .inc file # m = re.match("" % (INCLUDE_EXT), line) # if m: # includeFile = m.group(1) # includeFile = INCLUDE_PAGES[includeFile] # includeLine = "\n" % includeFile # ofile.write(includeLine) # else: # line = updateLinks(line) # ofile.write(line + "\n")