| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
"""A baseclass for the SkeletonPage template |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
__author__ = "Tavis Rudd <tavis@damnsimple.com>" |
|---|
| 13 |
__revision__ = "$Revision: 1.13 $"[11:-2] |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
import time, types, os, sys |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
from Cheetah.Template import Template |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
True = (1==1) |
|---|
| 28 |
False = (0==1) |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
class _SkeletonPage(Template): |
|---|
| 34 |
"""A baseclass for the SkeletonPage template""" |
|---|
| 35 |
|
|---|
| 36 |
docType = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ' + \ |
|---|
| 37 |
'"http://www.w3.org/TR/html4/loose.dtd">' |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
title = '' |
|---|
| 43 |
siteDomainName = 'www.example.com' |
|---|
| 44 |
siteCredits = 'Designed & Implemented by Tavis Rudd' |
|---|
| 45 |
siteCopyrightName = "Tavis Rudd" |
|---|
| 46 |
htmlTag = '<html>' |
|---|
| 47 |
|
|---|
| 48 |
def __init__(self, *args, **KWs): |
|---|
| 49 |
Template.__init__(self, *args, **KWs) |
|---|
| 50 |
self._metaTags = {'HTTP-EQUIV':{'keywords':'Cheetah', |
|---|
| 51 |
'Content-Type':'text/html; charset=iso-8859-1', |
|---|
| 52 |
}, |
|---|
| 53 |
'NAME':{'generator':'Cheetah: The Python-Powered Template Engine'} |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
self._stylesheets = {} |
|---|
| 57 |
|
|---|
| 58 |
self._stylesheetsOrder = [] |
|---|
| 59 |
|
|---|
| 60 |
self._stylesheetLibs = {} |
|---|
| 61 |
|
|---|
| 62 |
self._javascriptLibs = {} |
|---|
| 63 |
self._javascriptTags = {} |
|---|
| 64 |
|
|---|
| 65 |
self._bodyTagAttribs = {} |
|---|
| 66 |
|
|---|
| 67 |
def metaTags(self): |
|---|
| 68 |
"""Return a formatted vesion of the self._metaTags dictionary, using the |
|---|
| 69 |
formatMetaTags function from Cheetah.Macros.HTML""" |
|---|
| 70 |
|
|---|
| 71 |
return self.formatMetaTags(self._metaTags) |
|---|
| 72 |
|
|---|
| 73 |
def stylesheetTags(self): |
|---|
| 74 |
"""Return a formatted version of the self._stylesheetLibs and |
|---|
| 75 |
self._stylesheets dictionaries. The keys in self._stylesheets must |
|---|
| 76 |
be listed in the order that they should appear in the list |
|---|
| 77 |
self._stylesheetsOrder, to ensure that the style rules are defined in |
|---|
| 78 |
the correct order.""" |
|---|
| 79 |
|
|---|
| 80 |
stylesheetTagsTxt = '' |
|---|
| 81 |
for title, src in self._stylesheetLibs.items(): |
|---|
| 82 |
stylesheetTagsTxt += '<link rel="stylesheet" type="text/css" href="' + str(src) + '" />\n' |
|---|
| 83 |
|
|---|
| 84 |
if not self._stylesheetsOrder: |
|---|
| 85 |
return stylesheetTagsTxt |
|---|
| 86 |
|
|---|
| 87 |
stylesheetTagsTxt += '<style type="text/css"><!--\n' |
|---|
| 88 |
for identifier in self._stylesheetsOrder: |
|---|
| 89 |
if not self._stylesheets.has_key(identifier): |
|---|
| 90 |
warning = '# the identifier ' + identifier + \ |
|---|
| 91 |
'was in stylesheetsOrder, but not in stylesheets' |
|---|
| 92 |
print warning |
|---|
| 93 |
stylesheetTagsTxt += warning |
|---|
| 94 |
continue |
|---|
| 95 |
|
|---|
| 96 |
attribsDict = self._stylesheets[identifier] |
|---|
| 97 |
cssCode = '' |
|---|
| 98 |
attribCode = '' |
|---|
| 99 |
for k, v in attribsDict.items(): |
|---|
| 100 |
attribCode += str(k) + ': ' + str(v) + '; ' |
|---|
| 101 |
attribCode = attribCode[:-2] |
|---|
| 102 |
|
|---|
| 103 |
cssCode = '\n' + identifier + ' {' + attribCode + '}' |
|---|
| 104 |
stylesheetTagsTxt += cssCode |
|---|
| 105 |
|
|---|
| 106 |
stylesheetTagsTxt += '\n//--></style>\n' |
|---|
| 107 |
|
|---|
| 108 |
return stylesheetTagsTxt |
|---|
| 109 |
|
|---|
| 110 |
def javascriptTags(self): |
|---|
| 111 |
"""Return a formatted version of the javascriptTags and |
|---|
| 112 |
javascriptLibs dictionaries. Each value in javascriptTags |
|---|
| 113 |
should be a either a code string to include, or a list containing the |
|---|
| 114 |
JavaScript version number and the code string. The keys can be anything. |
|---|
| 115 |
The same applies for javascriptLibs, but the string should be the |
|---|
| 116 |
SRC filename rather than a code string.""" |
|---|
| 117 |
|
|---|
| 118 |
javascriptTagsTxt = [] |
|---|
| 119 |
for key, details in self._javascriptTags.items(): |
|---|
| 120 |
if type(details) not in (types.ListType, types.TupleType): |
|---|
| 121 |
details = ['',details] |
|---|
| 122 |
|
|---|
| 123 |
javascriptTagsTxt += ['<script language="JavaScript', str(details[0]), |
|---|
| 124 |
'" type="text/javascript"><!--\n', |
|---|
| 125 |
str(details[0]), '\n//--></script>\n'] |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
for key, details in self._javascriptLibs.items(): |
|---|
| 129 |
if type(details) not in (types.ListType, types.TupleType): |
|---|
| 130 |
details = ['',details] |
|---|
| 131 |
|
|---|
| 132 |
javascriptTagsTxt += ['<script language="JavaScript', str(details[0]), |
|---|
| 133 |
'" type="text/javascript" src="', |
|---|
| 134 |
str(details[1]), '" />\n'] |
|---|
| 135 |
return ''.join(javascriptTagsTxt) |
|---|
| 136 |
|
|---|
| 137 |
def bodyTag(self): |
|---|
| 138 |
"""Create a body tag from the entries in the dict bodyTagAttribs.""" |
|---|
| 139 |
return self.formHTMLTag('body', self._bodyTagAttribs) |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
def imgTag(self, src, alt='', width=None, height=None, border=0): |
|---|
| 143 |
|
|---|
| 144 |
"""Dynamically generate an image tag. Cheetah will try to convert the |
|---|
| 145 |
src argument to a WebKit serverSidePath relative to the servlet's |
|---|
| 146 |
location. If width and height aren't specified they are calculated using |
|---|
| 147 |
PIL or ImageMagick if available.""" |
|---|
| 148 |
|
|---|
| 149 |
src = self.normalizePath(src) |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
if not width or not height: |
|---|
| 153 |
try: |
|---|
| 154 |
import Image |
|---|
| 155 |
im = Image.open(src) |
|---|
| 156 |
calcWidth, calcHeight = im.size |
|---|
| 157 |
del im |
|---|
| 158 |
if not width: width = calcWidth |
|---|
| 159 |
if not height: height = calcHeight |
|---|
| 160 |
|
|---|
| 161 |
except: |
|---|
| 162 |
try: |
|---|
| 163 |
calcWidth, calcHeight = os.popen( |
|---|
| 164 |
'identify -format "%w,%h" ' + src).read().split(',') |
|---|
| 165 |
if not width: width = calcWidth |
|---|
| 166 |
if not height: height = calcHeight |
|---|
| 167 |
|
|---|
| 168 |
except: |
|---|
| 169 |
pass |
|---|
| 170 |
|
|---|
| 171 |
if width and height: |
|---|
| 172 |
return ''.join(['<img src="', src, '" width="', str(width), '" height="', str(height), |
|---|
| 173 |
'" alt="', alt, '" border="', str(border), '" />']) |
|---|
| 174 |
elif width: |
|---|
| 175 |
return ''.join(['<img src="', src, '" width="', str(width), |
|---|
| 176 |
'" alt="', alt, '" border="', str(border), '" />']) |
|---|
| 177 |
elif height: |
|---|
| 178 |
return ''.join(['<img src="', src, '" height="', str(height), |
|---|
| 179 |
'" alt="', alt, '" border="', str(border), '" />']) |
|---|
| 180 |
else: |
|---|
| 181 |
return ''.join(['<img src="', src, '" alt="', alt, '" border="', str(border),'" />']) |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
def currentYr(self): |
|---|
| 185 |
"""Return a string representing the current yr.""" |
|---|
| 186 |
return time.strftime("%Y",time.localtime(time.time())) |
|---|
| 187 |
|
|---|
| 188 |
def currentDate(self, formatString="%b %d, %Y"): |
|---|
| 189 |
"""Return a string representing the current localtime.""" |
|---|
| 190 |
return time.strftime(formatString,time.localtime(time.time())) |
|---|
| 191 |
|
|---|
| 192 |
def spacer(self, width=1,height=1): |
|---|
| 193 |
return '<img src="spacer.gif" width="%s" height="%s" alt="" />'% (str(width), str(height)) |
|---|
| 194 |
|
|---|
| 195 |
def formHTMLTag(self, tagName, attributes={}): |
|---|
| 196 |
"""returns a string containing an HTML <tag> """ |
|---|
| 197 |
tagTxt = ['<', tagName.lower()] |
|---|
| 198 |
for name, val in attributes.items(): |
|---|
| 199 |
tagTxt += [' ', name.lower(), '="', str(val),'"'] |
|---|
| 200 |
tagTxt.append('>') |
|---|
| 201 |
return ''.join(tagTxt) |
|---|
| 202 |
|
|---|
| 203 |
def formatMetaTags(self, metaTags): |
|---|
| 204 |
"""format a dict of metaTag definitions into an HTML version""" |
|---|
| 205 |
metaTagsTxt = [] |
|---|
| 206 |
if metaTags.has_key('HTTP-EQUIV'): |
|---|
| 207 |
for http_equiv, contents in metaTags['HTTP-EQUIV'].items(): |
|---|
| 208 |
metaTagsTxt += ['<meta http-equiv="', str(http_equiv), '" content="', |
|---|
| 209 |
str(contents), '" />\n'] |
|---|
| 210 |
|
|---|
| 211 |
if metaTags.has_key('NAME'): |
|---|
| 212 |
for name, contents in metaTags['NAME'].items(): |
|---|
| 213 |
metaTagsTxt += ['<meta name="', str(name), '" content="', str(contents), |
|---|
| 214 |
'" />\n'] |
|---|
| 215 |
return ''.join(metaTagsTxt) |
|---|
| 216 |
|
|---|