#!/usr/bin/env python

import sys, getopt, string, re

USAGE="""
PyDeCSS 0.02: a utility to strip Cascading Style Sheets (CSS) tags 
            from HTML documents

USAGE: PyDeCSS [-h] [-i input file] [-o output file]

options:
	-h		print this help message
        -i input file	input file to strip (default: standard input)
        -o output file	place to put the output (default: standard
output)

Hm.  I just copied Mr. Bad's usage message and changed three
characters.
         Damn, I'm lazy.
"""

optlist, args = getopt.getopt(sys.argv[1:], 'hi:o:')

for opt in optlist:
    if opt[0] == '-h':
        print USAGE
        sys.exit(0)
    elif opt[0] == '-i':
        try:
            IN = open(opt[1], 'r').readlines()
        except:
            print "Unable to open %s" % opt[1]
            sys.exit(1)
    elif opt[0] == '-o':
        try:
            OUT = open(opt[1], 'w')
        except:
            print "Unable to open %s" % opt[1]
            sys.exit(1)


try:
    dummy = IN    # now trying a different way to work around the lack of "if undef".
except:           #   If anyone has a reasonable way to do this, let me know:  aaron@seul.org
    IN = sys.stdin.readlines()

try:
    dummy = OUT
except:
    OUT = sys.stdout

css =
re.compile("""(<link.*?rel=(\"|\')stylesheet(\"|\').*?>)|(<style>.*?</style>)|(style=(\"|\').*?(\"|\'))|(class=(\"|\').*?(\"|\'))|(id=(\"|\').*?(\"|\'))""", re.I)
output = re.sub(css, "", string.join(IN))

OUT.write(output)
