1 """I am the content handler for any mimetype of the form X/Y+tal.
2
3 Note that X/Y must be either text/html or an XML based mimetype.
4 """
5
6 from advene.model.tales import AdveneContext
7
8 from cStringIO import StringIO
9 from simpletal import simpleTAL
10
11
12
14 """Is this view_handler likely to handle a view with that mimetype.
15
16 Return an int between 00 and 99, indicating the likelyhood of this handler
17 to handle correctly the given mimetype. 70 is used as a standard value when
18 the hanlder is pretty sure it can handle the mimetype.
19 """
20 if mimetype.endswith("+tal"):
21 return 70
22 else:
23 return 0
24
26 """Return the mimetype of the content produced by that view.
27
28 Note that the output mimetype may depend on the mimetype of the view, as
29 well as the content of the view itself, but should not depend on the
30 element the view is applied to.
31 """
32 return view.content_mimetype[:-4]
33
35 f = view.content_as_file
36 html = view.content_mimetype.startswith("text/html")
37 if html:
38 t = simpleTAL.compileHTMLTemplate(f, "utf-8")
39 kw = {}
40 else:
41 t = simpleTAL.compileXMLTemplate(f)
42 kw = { "suppressXMLDeclaration": 1 }
43
44
45
46
47
48
49 f.close()
50
51
52
53
54
55
56
57 c = AdveneContext(here=obj)
58 c.addGlobal("view", view)
59 if refpkg is None:
60 if hasattr(obj, "ADVENE_TYPE"):
61 refpkg = obj.owner
62 else:
63 refpkg = obj
64 c.addGlobal("package", refpkg)
65 out = StringIO()
66 t.expand(c, out, outputEncoding="utf-8", **kw)
67 return out.getvalue()
68
69
70