juan reyero

rehead.py — a Python script for managing HTML headers

The files from which my web site is built are scattered around several directories. They are org-mode and static HTML files, and each belogs to a different web section. This little script allows me to maintain a common header, and ensure that whenever I build the site all the page's headers will be correct.

#!/usr/bin/env python

"""rehead.py - Parses a file, replacing whatever it finds between a
line containing start-head and a line containing end-head with a
string that depends on the directory on which the file is found.

Usage: rehead.py [args|options] file[s]

Arguments:

-H file
--header=file     the file to read the header from (default header.html)
-i class
--inactive=class  class of the elements whose tag doesn't match the
                  directory the file is in (default inactiveClass)
-a class
--active=class    class of the elements whose tag matches the
                  directory the file is in (default activeClass)
-t this
--this=name       name against which the [[tags]] are mached, by default
                  the directory the file is in.

Options:
-h
--help            print this help and exit

The header is read from a file.  Any appeareance of [[tag]] in it is
considered a regular expression and matched against the directory the
file is in: if it matches, it is replaced with the active class
parameter; if it does not match, it is replaced with the inactive
class parameter.

Copyright Juan Reyero, http://juanreyero.com.  You are free to do
whatever you want with this code.
"""
__version__ = "0.1"
__date__ = "2009-01-27"
__author__ = "Juan Reyero"

from os.path import dirname
import re

class Rehead():
    def __init__(self, header, active, inactive, this):
        hin = open(header, 'r')
        self._header = hin.read()
        hin.close()
        self._active = active
        self._inactive = inactive
        self._this = this

    def build_header(self, file):
        header = self._header
        tags = re.findall(r'\[\[(.+?)\]\]', header)
        this = self._this
        if not this:
            this = dirname(file)
        for t in tags:
            replace_with = self._inactive
            if re.search(t, this):
                replace_with = self._active
            # The regular expressions tend to have |, which are ok to
            # match several directories, but mess up the replacement. 
            to_replace = re.sub(r'\|', r'\|', t)
            header = re.sub(r'\[\[' + to_replace + r'\]\]', 
                            replace_with, header)
        return header

    def slurp(self, file):
        fin = open(file, 'r')
        lines = fin.readlines()
        fin.close()
        return lines
    
    def rehead(self, file):
        lines = self.slurp(file)
        header = self.build_header(file)
        in_header = False
        fout = open(file, 'w')
        for l in lines:
            if (in_header == False) and re.search(r'start-head', l):
                fout.write(l)
                fout.write(header)
                in_header = True
            if (in_header == True) and re.search(r'end-head', l):
                in_header = False
            if in_header == False:
                fout.write(l)
        fout.close()

if __name__ == '__main__':
    import sys
    from getopt import getopt
    opts, files = getopt(sys.argv[1:], 'ha:i:H:t:',
                         ['help', 'active=', 'inactive=', 'header=', 'this='])
    active = 'activeClass'
    inactive = 'inactiveClass'
    header = 'head.html'
    this = ''
    for (opt, val) in opts:
        if  opt == '-h' or opt == '--help':
            print __doc__
            sys.exit(1)
        elif opt == '-a' or opt == '--active':
            active = val
        elif opt == '-i' or opt == '--inactive':
            inactive = val
        elif opt == '-H' or opt == '--header':
            header = val
        elif opt == '-t' or opt == '--this':
            this = val
    rh = Rehead(header, active, inactive, this)
    for f in files:
        rh.rehead(f)
Juan Reyero Barcelona, 2009-11-27
 

blog comments powered by Disqus