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