author | Tero Marttila <terom@paivola.fi> |
Sun, 14 Sep 2014 13:54:07 +0300 | |
changeset 226 | 26ec457d0eb2 |
parent 224 | cea4578f7910 |
child 99 | 88fa55fa0194 |
permissions | -rw-r--r-- |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
1 |
from django.conf import settings |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
2 |
|
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
3 |
import codecs |
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
4 |
import datetime |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
5 |
import logging; log = logging.getLogger('qmsk_www_pages.pages') |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
6 |
import os, os.path |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
7 |
|
218 | 8 |
import markdown |
226
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
9 |
import mako.template |
218 | 10 |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
11 |
class NotFound (Exception): |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
12 |
pass |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
13 |
|
226
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
14 |
class RenderError (Exception): |
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
15 |
pass |
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
16 |
|
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
17 |
class Site (object): |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
18 |
@classmethod |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
19 |
def lookup (cls): |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
20 |
return cls( |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
21 |
root = settings.QMSK_WWW_PAGES_DIR, |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
22 |
name = settings.QMSK_WWW_PAGES_SITE, |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
23 |
) |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
24 |
|
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
25 |
def __init__ (self, root, name): |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
26 |
self.root = root |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
27 |
self.name = name |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
28 |
|
199 | 29 |
class Tree (object): |
200 | 30 |
INDEX = 'index' |
31 |
||
199 | 32 |
@classmethod |
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
33 |
def lookup (cls, site, parts): |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
34 |
""" |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
35 |
Returns Tree |
199 | 36 |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
37 |
Raises NotFound |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
38 |
""" |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
39 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
40 |
parents = ( ) |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
41 |
tree = cls(site.root, None, parents, site, |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
42 |
title = site.name, |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
43 |
) |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
44 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
45 |
for name in parts: |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
46 |
if name.startswith('.'): |
199 | 47 |
# evil |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
48 |
raise NotFound() |
199 | 49 |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
50 |
if not name: |
199 | 51 |
continue |
52 |
||
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
53 |
path = os.path.join(tree.path, name) |
199 | 54 |
|
55 |
if not os.path.exists(path): |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
56 |
raise NotFound() |
199 | 57 |
|
58 |
if not os.path.isdir(path): |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
59 |
raise NotFound() |
199 | 60 |
|
215 | 61 |
# title |
62 |
title = tree.item_title(name) |
|
63 |
||
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
64 |
parents += (tree, ) |
215 | 65 |
tree = cls(path, name, parents, site, |
66 |
title = title, |
|
67 |
) |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
68 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
69 |
return tree |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
70 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
71 |
def __init__ (self, path, name, parents, site, |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
72 |
title = None, |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
73 |
): |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
74 |
""" |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
75 |
path: filesystem path |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
76 |
name: subtree name, or None for root |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
77 |
parents: (Tree) |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
78 |
site: Site |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
79 |
""" |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
80 |
|
199 | 81 |
self.path = path |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
82 |
self.name = name |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
83 |
self.parents = parents |
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
84 |
self.site = site |
200 | 85 |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
86 |
self.title = title or name |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
87 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
88 |
def hierarchy (self): |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
89 |
""" |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
90 |
Yield Tree. |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
91 |
""" |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
92 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
93 |
for tree in self.parents: |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
94 |
yield tree |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
95 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
96 |
yield self |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
97 |
|
200 | 98 |
def url (self, tree=None, page=None): |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
99 |
path = '/'.join(tree.name for tree in self.hierarchy() if tree.name is not None) |
200 | 100 |
|
101 |
if path: |
|
102 |
path += '/' |
|
103 |
||
104 |
if tree: |
|
105 |
path = tree + '/' |
|
106 |
||
107 |
if page: |
|
108 |
path += page |
|
109 |
||
110 |
return path |
|
111 |
||
112 |
def scan (self): |
|
113 |
""" |
|
114 |
Scan for files in tree. |
|
115 |
""" |
|
116 |
||
117 |
for filename in os.listdir(self.path): |
|
118 |
if filename.startswith('.'): |
|
119 |
continue |
|
120 |
||
121 |
if '.' in filename: |
|
122 |
file_name, file_type = filename.rsplit('.', 1) |
|
123 |
else: |
|
124 |
file_name = filename |
|
125 |
file_type = None |
|
126 |
||
127 |
if not file_name: |
|
128 |
continue |
|
129 |
||
130 |
path = os.path.join(self.path, filename) |
|
131 |
||
132 |
yield path, file_name, file_type |
|
133 |
||
209 | 134 |
def item_title (self, name): |
135 |
""" |
|
136 |
Lookup item title if exists. |
|
137 |
""" |
|
138 |
||
139 |
title_path = os.path.join(self.path, name + '.title') |
|
140 |
||
141 |
log.info("%s: %s title_path=%s", self, name, title_path) |
|
142 |
||
143 |
if os.path.exists(title_path): |
|
144 |
return open(title_path).read().strip() |
|
145 |
else: |
|
146 |
return None |
|
147 |
||
200 | 148 |
def list (self): |
149 |
""" |
|
211 | 150 |
Lists all Trees and Pages for this Tree. |
151 |
||
152 |
Yields (name, url, page_type or None, title) |
|
200 | 153 |
""" |
154 |
||
209 | 155 |
for path, name, file_type in self.scan(): |
156 |
title = self.item_title(name) or name |
|
157 |
||
200 | 158 |
# trees |
159 |
if os.path.isdir(path): |
|
211 | 160 |
yield name, self.url(tree=name), None, title |
200 | 161 |
|
209 | 162 |
if name == self.INDEX: |
200 | 163 |
continue |
164 |
||
165 |
# pages |
|
166 |
if not file_type: |
|
167 |
continue |
|
168 |
||
169 |
if file_type not in TYPES: |
|
170 |
continue |
|
209 | 171 |
|
211 | 172 |
yield name, self.url(page=name), file_type, title |
173 |
||
174 |
def list_sorted (self): |
|
175 |
return sorted(list(self.list())) |
|
200 | 176 |
|
199 | 177 |
def page (self, name): |
178 |
""" |
|
179 |
Scans through tree looking for a matching page. |
|
180 |
||
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
181 |
Returns Page or None. |
199 | 182 |
""" |
200 | 183 |
|
184 |
if not name: |
|
185 |
name = self.INDEX |
|
215 | 186 |
title_default = self.title |
187 |
else: |
|
188 |
title_default = None |
|
199 | 189 |
|
217
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
190 |
parents = self.parents + (self, ) |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
191 |
|
200 | 192 |
for path, file_name, file_type in self.scan(): |
193 |
# match on name |
|
224
cea4578f7910
qmsk_www_pages: handle non-typed filenames when looking up
Tero Marttila <terom@paivola.fi>
parents:
218
diff
changeset
|
194 |
if file_name == name: |
cea4578f7910
qmsk_www_pages: handle non-typed filenames when looking up
Tero Marttila <terom@paivola.fi>
parents:
218
diff
changeset
|
195 |
pass |
cea4578f7910
qmsk_www_pages: handle non-typed filenames when looking up
Tero Marttila <terom@paivola.fi>
parents:
218
diff
changeset
|
196 |
elif file_type and (file_name + '.' + file_type == name): |
cea4578f7910
qmsk_www_pages: handle non-typed filenames when looking up
Tero Marttila <terom@paivola.fi>
parents:
218
diff
changeset
|
197 |
pass |
cea4578f7910
qmsk_www_pages: handle non-typed filenames when looking up
Tero Marttila <terom@paivola.fi>
parents:
218
diff
changeset
|
198 |
else: |
199 | 199 |
continue |
217
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
200 |
|
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
201 |
# redirects? |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
202 |
if os.path.islink(path): |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
203 |
target = os.readlink(path) |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
204 |
|
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
205 |
# XXX: this should be some kind of common code |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
206 |
if '.' in target: |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
207 |
target, target_type = target.rsplit('.', 1) |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
208 |
|
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
209 |
log.info("%s: %s -> %s", self, name, target) |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
210 |
|
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
211 |
return RedirectPage(path, name, self, parents, |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
212 |
target = target, |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
213 |
) |
199 | 214 |
|
200 | 215 |
# match on type |
216 |
if not file_type: |
|
217 |
continue |
|
218 |
||
199 | 219 |
page_type = TYPES.get(file_type) |
220 |
||
221 |
if not page_type: |
|
222 |
continue |
|
200 | 223 |
|
224 |
# out |
|
215 | 225 |
title = self.item_title(file_name) or title_default |
209 | 226 |
|
217
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
227 |
return page_type(path, name, self, parents, |
209 | 228 |
title = title, |
199 | 229 |
) |
230 |
||
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
231 |
class Page (object): |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
232 |
ENCODING = 'utf-8' |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
233 |
|
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
234 |
@classmethod |
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
235 |
def lookup (cls, site, page): |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
236 |
""" |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
237 |
Returns Page. |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
238 |
|
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
239 |
Raises NotFound |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
240 |
""" |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
241 |
|
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
242 |
log.info("page=%r", page) |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
243 |
|
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
244 |
if page: |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
245 |
parts = page.split('/') |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
246 |
else: |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
247 |
parts = [ ] |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
248 |
|
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
249 |
if parts: |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
250 |
page_name = parts.pop(-1) |
199 | 251 |
tree_parts = parts |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
252 |
else: |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
253 |
page_name = '' |
199 | 254 |
tree_parts = [] |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
255 |
|
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
256 |
# scan dir |
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
257 |
tree = Tree.lookup(site, tree_parts) |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
258 |
|
199 | 259 |
# scan page |
260 |
page = tree.page(page_name) |
|
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
261 |
|
199 | 262 |
if not page: |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
263 |
raise NotFound() |
199 | 264 |
|
265 |
return page |
|
266 |
||
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
267 |
def __init__ (self, path, name, tree, parents=(), encoding=ENCODING, title=None): |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
268 |
self.path = path |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
269 |
self.name = name |
199 | 270 |
self.tree = tree |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
271 |
self.parents = parents |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
272 |
|
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
273 |
self.encoding = encoding |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
274 |
self.title = title or name |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
275 |
|
206
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
276 |
def hierarchy (self): |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
277 |
""" |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
278 |
Yield (Tree, name) pairs |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
279 |
""" |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
280 |
|
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
281 |
parent = None |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
282 |
|
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
283 |
for tree in self.parents: |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
284 |
if parent: |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
285 |
yield parent, tree.name |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
286 |
|
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
287 |
parent = tree |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
288 |
|
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
289 |
yield parent, self.name |
440e6769976c
pages: page.hierarchy() for navigation
Tero Marttila <terom@paivola.fi>
parents:
205
diff
changeset
|
290 |
|
201 | 291 |
def url (self): |
292 |
return self.tree.url(page=self.name) |
|
293 |
||
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
294 |
def open (self): |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
295 |
return codecs.open(self.path, encoding=self.encoding) |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
296 |
|
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
297 |
def stat (self): |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
298 |
return os.stat(self.path) |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
299 |
|
198 | 300 |
def breadcrumb (self): |
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
301 |
for tree in self.tree.hierarchy(): |
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
302 |
yield tree.url(), tree.title |
200 | 303 |
|
304 |
if self.name != self.tree.INDEX: |
|
205
f1adf52f485a
pages: tree parents, hierarchy; tree/page titles
Tero Marttila <terom@paivola.fi>
parents:
202
diff
changeset
|
305 |
yield self.url(), self.title |
198 | 306 |
|
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
307 |
def modified (self): |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
308 |
return datetime.datetime.utcfromtimestamp(self.stat().st_mtime) |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
309 |
|
217
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
310 |
def redirect_page (self, request): |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
311 |
return None |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
312 |
|
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
313 |
def render_html (self, request): |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
314 |
raise NotImplementedError() |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
315 |
|
217
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
316 |
# TODO: tree redirects |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
317 |
class RedirectPage (Page): |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
318 |
def __init__ (self, path, name, tree, parents, |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
319 |
target, |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
320 |
**opts |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
321 |
) : |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
322 |
super(RedirectPage, self).__init__(path, name, tree, parents, **opts) |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
323 |
|
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
324 |
self.target = target |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
325 |
|
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
326 |
def redirect_page (self, request): |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
327 |
return os.path.normpath(self.tree.url() + '/' + self.target) |
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
328 |
|
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
329 |
class HTML_Page (Page): |
217
26df557482d8
qmsk_www_pages: symlink support for page redirects
Tero Marttila <terom@paivola.fi>
parents:
215
diff
changeset
|
330 |
def render_html (self, request): |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
331 |
return self.open().read() |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
332 |
|
218 | 333 |
class MarkdownPage (Page): |
334 |
FORMAT = 'html5' |
|
335 |
||
336 |
def __init__ (self, path, name, tree, parents, |
|
337 |
format=FORMAT, |
|
338 |
**opts |
|
339 |
) : |
|
340 |
super(MarkdownPage, self).__init__(path, name, tree, parents, **opts) |
|
341 |
||
342 |
self.format = format |
|
343 |
||
344 |
def render_html (self, request): |
|
345 |
return markdown.markdown(self.open().read(), |
|
346 |
output_format = self.format, |
|
347 |
) |
|
348 |
||
226
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
349 |
class TemplatePage (Page): |
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
350 |
def render_html (self, request): |
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
351 |
""" |
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
352 |
Raises RenderError if !DEBUG, arbitrary error with stack trace otherwise. |
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
353 |
""" |
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
354 |
|
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
355 |
try: |
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
356 |
return mako.template.Template(filename=self.path).render() |
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
357 |
except Exception as error: |
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
358 |
if settings.DEBUG: |
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
359 |
raise |
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
360 |
else: |
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
361 |
raise RenderError(error) |
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
362 |
|
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
363 |
SITE = Site.lookup() |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
364 |
|
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
365 |
TYPES = { |
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
366 |
'html': HTML_Page, |
218 | 367 |
'md': MarkdownPage, |
368 |
'markdown': MarkdownPage, |
|
226
26ec457d0eb2
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
Tero Marttila <terom@paivola.fi>
parents:
224
diff
changeset
|
369 |
'tmpl': TemplatePage, |
197
11a6d8fea463
qmsk_www_pages: pages.Page.lookup() and .html support; logging
Tero Marttila <terom@paivola.fi>
parents:
diff
changeset
|
370 |
} |
202
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
371 |
|
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
372 |
def page (page): |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
373 |
return Page.lookup(SITE, page) |
da5de3a97efb
pages: Site, footer with modified, basic css styles, page tree types
Tero Marttila <terom@paivola.fi>
parents:
201
diff
changeset
|
374 |