Package advene :: Package model :: Package view :: Module tal
[hide private]
[frames] | no frames]

Source Code for Module advene.model.view.tal

 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  # general handler interface 
12   
13 -def claims_for_handle(mimetype):
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
25 -def get_output_mimetype(view):
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
34 -def apply_to(view, obj, refpkg=None):
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 # It is a bit ugly to suppress XML declaration, but necessary when XML 44 # views are used inside other XML views. 45 # Furthermore, this does not seem to serious a ugliness, since we use 46 # UTF-8 # encoding, which appears to be the default (at least for 47 # simpleTAL generator), and since the XML spec allows well-formed 48 # documents to have no XML declaration. 49 f.close() 50 51 # should we cache the compiled template for future uses, 52 # and recompile it only when the content is modified? 53 # the problem is that external contents may be modified without notification 54 # (or rely on f.headers['date'], but that would require to hack content.py 55 # to make that field *always* present - might be a good idea...) 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 # specific to this handler 70