htmlpage.py

'''\
HTML Page Base Class.

example:
    fn = path.join(HtmlPath,FileName)
    fp = open(fn, 'w')
    try:
        # The PageWriterClass implements obj.writepage(fstream)
        pw = PageWriterClass(pagedata)

        # New HTML Page object...
        pg = htmlpage(Title,H1,H2,CSS,JS)
        pg.writepage(fp, pw)

        # ...
        print 'wrote: %s' % fn
    except Exception as e:
        raise
    finally:
        fp.close()

Developer@Sonnack.com
April 2012
'''
####################################################################################################
from sys import stdoutstderrargv
from datetime import datetime
####################################################################################################
HtmlH1 = 'Sonnack.com'
HtmlH2 = 'HTML Page'

HtmlCSS = ['/basic.css']
HtmlJS  = []

##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
class htmlpage (object):
    '''\
HTML Page Class.

properties:
    .now                    - Current datetime
    .header1                - <H1> string
    .header2                - <H2> string
    .title                  - <H3> string
    .csspages[]             - List of CSS filenames
    .jspages[]              - List of JS filenames

methods:
    .writefile(fn, caller)   - write to filename 'fn'
    .writepage(fp, caller)   - write to open file 'fp'
    str(obj)                - Headers + Title string
    repr(obj)               - JSON-like string

'''
    def __init__ (selfttlh1=HtmlH1h2=HtmlH2meta=Nonecsspages=HtmlCSSjspages=HtmlJS):
        self.now = datetime.today()
        self.title = ttl
        self.header1 = h1
        self.header2 = h2
        self.meta = meta
        self.csspages = csspages
        self.jspages = jspages
        self.onload = None
        self.cb_htmlhead = None
        self.cb_pagehead = None
        self.cb_pagefoot = None

    def writefile (selffilenamecaller):
        ''' Write HTML Page to filename. Caller must have a writepage(fp) method. '''
        f = open(filename"w")
        try:
            self.writepage(fcaller)
        except Exception as e:
            raise
        finally:
            f.close()

    def writepage (selffpcaller):
        ''' Write HTML Page to fp. Caller must have a writepage(fp) method. '''
        lm_dt = self.now.strftime('%a %b %d %H:%M:%S %Y')
        cr_dt = self.now.strftime('%B %d, %Y %H:%M:%S')

        # Page Head...
        print >> fp'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
        print >> fp'<html><head>'
        print >> fp'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
        print >> fp'<meta http-equiv="Last-Modified" content="%s">' % lm_dt
        print >> fp'<title>%s</title>' % (self.meta if self.meta else self.title)

        # CSS Links...
        for css in self.csspages:
            print >> fp'<link rel="stylesheet" type="text/css" href="%s">' % css
        # JavaScript Links...
        for js in self.jspages:
            print >> fp'<script type="text/javascript" src="%s"></script>' % js

        # Client HTML-HEAD Callback...
        if self.cb_htmlhead:
            self.cb_htmlhead(selffp)
        print >> fp'</head>'

        # Body-OnLoad Switch...
        if self.onload:
            print >> fp'<body onload="%s">' % self.onload
        else:
            print >> fp'<body>'

        # Page Head...
        print >> fp'<div id="PageHead">'
        if self.cb_pagehead:
            self.cb_pagehead(selffp)
        else:
            print >> fp'<h1>%s</h1>' % self.header1
            print >> fp'<h2>%s</h2>' % self.header2
        print >> fp'</div>'

        # Page Content...
        print >> fp'<div id="PageBody">'
        print >> fp'<h3>%s</h3>' % self.title
        fp.flush()
        try:
            caller.writepage(fp)
        except Exception as e:
            raise
        fp.flush()
        print >> fp'</div>'

        # Page Foot...
        print >> fp'<div id="PageFoot">'
        if self.cb_pagefoot:
            self.cb_pagefoot(selffp)
        else:
            print >> fp'<span class="Lt">created: %s</span>' % cr_dt
            print >> fp'<span class="Rt" style="font-style:italic;">(private page)</span>'
            print >> fp'<br clear=ALL>'
        print >> fp'</div>'
        print >> fp'</body></html>'
        fp.flush()

    def __str__ (self):
        s = '::%s:%s::%s'
        t = (self.header1self.header2self.title)
        return s % t

    def __repr__ (self):
        s = '{htmlpage:{h1:"%s", h2:"%s", ttl:"%s"}}'
        t = (self.header1self.header2self.title)
        return s % t


##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
class writer (object):
    tag = 'Demo Writer'

    def writepage (selffp):
        print >> fp'<p>Hello, World!</p>'
        print >> fp'<p>%s</p>' % self.tag


####################################################################################################
if __name__ == '__main__':
    print 'autorun:',argv[0]
    pg = htmlpage('Test!''CJSoftware''Python')
    print str(pg)
    print repr(pg)
    pg.writepage(stdoutwriter())

####################################################################################################
###eof###