-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.py
191 lines (151 loc) · 5.61 KB
/
template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#
# Danlann - Memory Jail - an easy to use photo gallery generator.
#
# Copyright (C) 2006-2008 by Artur Wroblewski <wrobell@pld-linux.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import re
import stringtemplate3 as stringtemplate
from danlann.bc import Gallery, Album, Photo
import danlann.config
class Template(object):
"""
A template.
@ivar gallery: gallery reference
@ivar copyright: copyright text
@ivar st_group: StringTemplate reference
@ivar css: list of css files
@ivar js: list of javascript files
"""
def __init__(self, name, gallery, override=None):
"""
Create new instance of a template with gallery instance reference.
@param name: template name
@param gallery: gallery reference
@param override: directory overriding template
"""
super(Template, self).__init__()
self.name = name
self.gallery = gallery
self.css = ['css/danlann.css']
self.js = ['js/jquery.js', 'js/danlann.js']
self.copyright = ''
st_group = stringtemplate.StringTemplateGroup('basic',
'%s/tmpl' % danlann.config.libpath)
st_group.registerRenderer(str, XMLRenderer());
st_group.registerRenderer(unicode, XMLRenderer());
# set template override if defined
# template override is realized with StringTemplate
# supergroup/subgroup functionality
if override:
self.st_group = stringtemplate.StringTemplateGroup(name, override)
self.st_group.registerRenderer(str, XMLRenderer());
self.st_group.registerRenderer(unicode, XMLRenderer());
self.st_group.superGroup = st_group
else:
self.st_group = st_group # no override
self.tmpl_gallery = '%s/gallery' % self.name
self.tmpl_page = '%s/page' % self.name
self.tmpl_album = '%s/album' % self.name
self.tmpl_photo = '%s/photo' % self.name
self.tmpl_exif = '%s/exif' % self.name
def getPage(self, tmpl, rootdir, cls):
"""
Get page template for gallery page, album page or photo page.
@param tmpl: page template name
@param rootdir: relative path to gallery directory
@param cls: page class
"""
page = self.st_group.getInstanceOf(self.tmpl_page)
page['gallery'] = self.gallery
page['class'] = cls
page['tmpl'] = tmpl
page['rootdir'] = rootdir
page['copyright'] = self.copyright
page['css'] = self.css
page['js'] = self.js
return page
def write(self, f, page):
"""
Write page template to file.
@param f: page output file
@param page: page to be saved
"""
print >> f, unicode(page)
def galleryPage(self, f):
"""
Generate gallery page.
"""
# rootdir is current directory
page = self.getPage(self.tmpl_gallery, '.', 'gallery')
self.write(f, page)
def albumPage(self, f, album, parent):
"""
Generate album page.
"""
rootdir = self.gallery.rootdir(album)
page = self.getPage(self.tmpl_album, rootdir, 'album')
page['parent'] = parent
page['album'] = album
page['prev'] = parent.prev(album)
page['next'] = parent.next(album)
self.write(f, page)
def photoPage(self, f, photo):
"""
Generate photo page.
"""
album = photo.album
rootdir = self.gallery.rootdir(photo.album)
page = self.getPage(self.tmpl_photo, rootdir, 'photo')
page['photo'] = photo
page['album'] = album
page['prev'] = album.prev(photo)
page['next'] = album.next(photo)
self.write(f, page)
def exifPage(self, f, photo):
"""
Generate photo exif page.
"""
album = photo.album
rootdir = self.gallery.rootdir(photo.album)
page = self.getPage(self.tmpl_exif, rootdir, 'exif')
page['photo'] = photo
page['album'] = album
page['prev'] = album.prev(photo)
page['next'] = album.next(photo)
self.write(f, page)
class XMLRenderer(stringtemplate.AttributeRenderer):
"""
XML renderer for StringTemplate library to convert special characters
into XML entities.
"""
def __init__(self, *args, **kw):
super(XMLRenderer, self).__init__(*args, **kw)
# url starts with http and ends with a printable character or slash
# <> is not in a link
self.link = re.compile(r'(\bhttps?://\w[^<>\s]+[\w/])')
def toString(self, val, fmt):
assert val is not None
val = val.replace('&', '&')
val = val.replace('\'', ''')
val = val.replace('"', '"')
val = val.replace('<', '<')
val = val.replace('>', '>')
# new line to <br/> tag
val = val.replace('\\n', '<br/>')
# support links
val = self.link.sub('<a href = \'\\1\'>\\1</a>', val)
assert val is not None
return val