-
Notifications
You must be signed in to change notification settings - Fork 7
/
TemplatedPage.py
68 lines (43 loc) · 1.8 KB
/
TemplatedPage.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
#!/usr/bin/env python
# -*- mode: python; indent-tabs-mode: nil; -*- coding: iso-8859-1 -*-
"""
TemplatedPage.py
Copyright 2013 by Marcello Perathoner
Distributable under the GNU General Public License Version 3 or newer.
Output a Genshi page.
"""
from __future__ import unicode_literals
import genshi.output
import genshi.template
from genshi.core import _ensure
import cherrypy
import Formatters
import BaseSearcher
class TemplatedPage (object):
""" Output a page from a genshi template. """
CONTENT_TYPE = 'application/xml; charset=UTF-8'
FORMATTER = 'xml'
def get_serializer (self):
""" Override to get a different serializer. """
return genshi.output.XMLSerializer (strip_whitespace = False)
def output (self, template, **kwargs):
""" Output the page. """
# Send HTTP content-type header.
cherrypy.response.headers['Content-Type'] = self.CONTENT_TYPE
template = Formatters.formatters[self.FORMATTER].templates[template]
ctxt = genshi.template.Context (cherrypy = cherrypy, bs = BaseSearcher, **kwargs)
stream = template.stream
for filter_ in template.filters:
stream = filter_ (iter (stream), ctxt)
serializer = self.get_serializer ()
return genshi.output.encode (serializer (
_ensure (genshi.Stream (stream))), encoding = 'utf-8')
class TemplatedPageXHTML (TemplatedPage):
""" Output a page from a genshi template. """
CONTENT_TYPE = 'text/html; charset=UTF-8'
FORMATTER = 'html'
DOCTYPE = ('html',
'-//W3C//DTD XHTML 1.0 Strict//EN',
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd')
def get_serializer (self):
return genshi.output.XHTMLSerializer (doctype = self.DOCTYPE, strip_whitespace = False)