def encode(value): if value is None: return '' if type(value) is unicode: return value.encode('utf-8') if not is_xml_stream(value): value = value.get_body().get_content_elements() return stream_to_str_as_html(value)
def after_traverse(self, context): body = context.entity is_str = type(body) is str is_xml = is_xml_stream(body) if not is_str and not is_xml: return # If there is not a content type, just serialize the content if context.content_type: if is_xml: context.entity = stream_to_str_as_html(body) return # Standard page, wrap the content into the general template if is_str: body = XMLParser(body, doctype=xhtml_doctype) context.entity = self.get_skin(context).template(body) context.content_type = 'text/html; charset=UTF-8'
def substitute(data, stack, repeat_stack, encoding='utf-8'): """Interprets the given data as a substitution string with the "${expr}" format, where the expression within the brackets is an STL expression. Returns a tuple with the interpreted string and the number of substitutions done. """ if type(data) is not str: raise ValueError, 'byte string expected, not %s' % type(data) segments = subs_expr.split(data) for i, segment in enumerate(segments): if i % 2: # Evaluate expression value = evaluate(segment, stack, repeat_stack) # An STL template (duck typing) render = getattr(value, 'render', None) if render: value = render() # Ignore if None if value is None: continue # Case MSG: it returns <unicode> or <XMLParser> if is_prototype(value, MSG): value = value.gettext() # Yield if type(value) is unicode: yield TEXT, value.encode(encoding), 0 elif is_xml_stream(value): for x in value: if type(x) is not tuple: raise STLError, ERR_EXPR_XML % (type(x), segment) yield x else: yield TEXT, str(value), 0 elif segment: yield TEXT, segment, 0