#! /usr/bin/python

# This code is licensed under the GPL.
# Get yourself a version here : http://www.gnu.org/copyleft/gpl.html
#
# This should transform slides into html
# The slides are directly written in PostScript, using Matt Welsh
# PostScript macros set, psslides.
# http://www.cs.berkeley.edu/~mdw/proj/psslides/
#
# I use a modified version of psslides, allowing iso8859-1 fonts
#
# Warning: this code should be rewritten based uppon a syntax analysis

import re
import sys
import string

HEADER_COLOR = "#93A9F2"
FRAME_COLOR  = "blue"

def begin_html(title):
    """ begins the HTML stuff """
    str = '<html><head><title> %s </title></head>\n' % title
    str = str + '<body text="black" bgcolor="white">\n'
    str = str + '<table align="center" border="0" cols="1">\n'

    str = str + '<tr><th> <font size=+2>%s</font> </th></tr>\n' % title
    
    return str

def end_html():
    """ ends the html """
    str = '<tr><td align=right> <i>Produced by slides2html</i> </td></tr>'
    str = str + '</table>\n'
    str = str + '</body></html>'
    return str

def open_slide(title, fcolor, tcolor):
    """ Begin a html table, the html slide representation """
    str = '<tr><td align="center" width="100%">\n'
    
    str = str + '<table bgcolor="%s" border="0"  width="80%%"' % fcolor + \
          ' cellspacing="0" cellpadding="1">\n'
    
    str = str + '<tr><td>\n' + \
          '<table bgcolor="white" border="0" width="100%"' + \
          ' cellspacing="0" cellpadding="5">'
    str = str + '\n<tr bgcolor="%s"><th> %s </th></tr>\n' % (tcolor, title)

    str = str + '</tr></td>'
    
    return str

def close_slide(number):
    """ Close a html table, the html slide representation """
    str = '<tr valign="bottom"><td align="right">%s</td></tr>\n' % number
    str = str + '</table>\n</td></tr></table>\n'
    str = str + '\n<tr><td> &nbsp; </td></tr>\n'
    return str


def put_line(level, text):
    """ add a line in the current html table """
    if len(string.split(text)):
        if level == 1:
            str = '<tr><td> <b><i>%s</i></b> </tr></td>\n' % text
            
        elif level == 2:
            str = '<tr><td><dl><dl> %s </dl></dl></td></tr>' % text
            
        elif level == 3:
            str = '<tr><td> <pre> %s </pre> </td></tr>' % text

        elif level == 0:
            # Foot String
            str = '<tr><td align="center"> <i>%s</i> </td></tr>' % text
    else:
        str = ""
        
    return str

###
# MAIN STUFF
###
if __name__ == "__main__":
    # Here we gonna make the job !

    if len(sys.argv) == 1:
        print "no file to parse"
        sys.exit(0)

    filename = sys.argv[1]

    file = open(filename, "r")
    buffer = file.read()
    file.close()

    first_slide = 1
    slide_number = 0
    lines = string.split(buffer, "\n")

    for line in lines:
        # TitleString
        mobj = re.match("^\/TitleString (.*) def", line)
        if mobj is not None:
            print begin_html(mobj.group(1)[1:-1])

        # FootString
        mobj = re.match("^\/FootString (.*) def", line)
        if mobj is not None:
            print put_line(0, mobj.group(1)[1:-1])

        # Now Title
        mobj = re.match("^\((.*)\) *T", line)
        if mobj is not None:
            if not first_slide:
                print close_slide(slide_number)
            else:
                first_slide = 0
                
            print open_slide(mobj.group(1), FRAME_COLOR, HEADER_COLOR)
            slide_number = slide_number + 1

        # H1
        mobj = re.match("^\((.*)\) *H1", line)
        if mobj is not None:
            print put_line(1, mobj.group(1))
            
        # H2
        mobj = re.match("^\((.*)\) *H2", line)
        if mobj is not None:
            print put_line(2, mobj.group(1))

        # C
        mobj = re.match("^\((.*)\) *C", line)
        if mobj is not None:
            print put_line(3, mobj.group(1))

    print close_slide(slide_number)
    print end_html()
