2 # $Id: _SkeletonPage.py,v 1.13 2002/10/01 17:52:02 tavis_rudd Exp $
3 """A baseclass for the SkeletonPage template
7 Author: Tavis Rudd <tavis@damnsimple.com>,
8 Version: $Revision: 1.13 $
10 Last Revision Date: $Date: 2002/10/01 17:52:02 $
12 __author__
= "Tavis Rudd <tavis@damnsimple.com>"
13 __revision__
= "$Revision: 1.13 $"[11:-2]
15 ##################################################
18 import time
, types
, os
, sys
20 # intra-package imports ...
21 from Cheetah
.Template
import Template
24 ##################################################
25 ## GLOBALS AND CONSTANTS ##
30 ##################################################
33 class _SkeletonPage(Template
):
34 """A baseclass for the SkeletonPage template"""
36 docType
= '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ' + \
37 '"http://www.w3.org/TR/html4/loose.dtd">'
39 # docType = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ' + \
40 #'"http://www.w3.org/TR/xhtml1l/DTD/transitional.dtd">'
43 siteDomainName
= 'www.example.com'
44 siteCredits
= 'Designed & Implemented by Tavis Rudd'
45 siteCopyrightName
= "Tavis Rudd"
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',
53 'NAME':{'generator':'Cheetah: The Python-Powered Template Engine'}
55 # metaTags = {'HTTP_EQUIV':{'test':1234}, 'NAME':{'test':1234,'test2':1234} }
56 self
._stylesheets
= {}
57 # stylesheets = {'.cssClassName':'stylesheetCode'}
58 self
._stylesheetsOrder
= []
59 # stylesheetsOrder = ['.cssClassName',]
60 self
._stylesheetLibs
= {}
61 # stylesheetLibs = {'libName':'libSrcPath'}
62 self
._javascriptLibs
= {}
63 self
._javascriptTags
= {}
64 # self._javascriptLibs = {'libName':'libSrcPath'}
65 self
._bodyTagAttribs
= {}
68 """Return a formatted vesion of the self._metaTags dictionary, using the
69 formatMetaTags function from Cheetah.Macros.HTML"""
71 return self
.formatMetaTags(self
._metaTags
)
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
80 stylesheetTagsTxt
= ''
81 for title
, src
in self
._stylesheetLibs
.items():
82 stylesheetTagsTxt
+= '<link rel="stylesheet" type="text/css" href="' + str(src
) + '" />\n'
84 if not self
._stylesheetsOrder
:
85 return stylesheetTagsTxt
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'
93 stylesheetTagsTxt
+= warning
96 attribsDict
= self
._stylesheets
[identifier
]
99 for k
, v
in attribsDict
.items():
100 attribCode
+= str(k
) + ': ' + str(v
) + '; '
101 attribCode
= attribCode
[:-2] # get rid of the last semicolon
103 cssCode
= '\n' + identifier
+ ' {' + attribCode
+ '}'
104 stylesheetTagsTxt
+= cssCode
106 stylesheetTagsTxt
+= '\n//--></style>\n'
108 return stylesheetTagsTxt
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."""
118 javascriptTagsTxt
= []
119 for key
, details
in self
._javascriptTags
.items():
120 if type(details
) not in (types
.ListType
, types
.TupleType
):
121 details
= ['',details
]
123 javascriptTagsTxt
+= ['<script language="JavaScript', str(details
[0]),
124 '" type="text/javascript"><!--\n',
125 str(details
[0]), '\n//--></script>\n']
128 for key
, details
in self
._javascriptLibs
.items():
129 if type(details
) not in (types
.ListType
, types
.TupleType
):
130 details
= ['',details
]
132 javascriptTagsTxt
+= ['<script language="JavaScript', str(details
[0]),
133 '" type="text/javascript" src="',
134 str(details
[1]), '" />\n']
135 return ''.join(javascriptTagsTxt
)
138 """Create a body tag from the entries in the dict bodyTagAttribs."""
139 return self
.formHTMLTag('body', self
._bodyTagAttribs
)
142 def imgTag(self
, src
, alt
='', width
=None, height
=None, border
=0):
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."""
149 src
= self
.normalizePath(src
)
152 if not width
or not height
:
153 try: # see if the dimensions can be calc'd with PIL
156 calcWidth
, calcHeight
= im
.size
158 if not width
: width
= calcWidth
159 if not height
: height
= calcHeight
162 try: # try imageMagick instead
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
172 return ''.join(['<img src="', src
, '" width="', str(width
), '" height="', str(height
),
173 '" alt="', alt
, '" border="', str(border
), '" />'])
175 return ''.join(['<img src="', src
, '" width="', str(width
),
176 '" alt="', alt
, '" border="', str(border
), '" />'])
178 return ''.join(['<img src="', src
, '" height="', str(height
),
179 '" alt="', alt
, '" border="', str(border
), '" />'])
181 return ''.join(['<img src="', src
, '" alt="', alt
, '" border="', str(border
),'" />'])
185 """Return a string representing the current yr."""
186 return time
.strftime("%Y",time
.localtime(time
.time()))
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()))
192 def spacer(self
, width
=1,height
=1):
193 return '<img src="spacer.gif" width="%s" height="%s" alt="" />'% (str(width
), str(height
))
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
),'"']
201 return ''.join(tagTxt
)
203 def formatMetaTags(self
, metaTags
):
204 """format a dict of metaTag definitions into an HTML version"""
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']
211 if metaTags
.has_key('NAME'):
212 for name
, contents
in metaTags
['NAME'].items():
213 metaTagsTxt
+= ['<meta name="', str(name
), '" content="', str(contents
),
215 return ''.join(metaTagsTxt
)