def stl(document=None, namespace=freeze({}), prefix=None, events=None, mode='events', skip=(DOCUMENT_TYPE,)): # Input encoding = 'utf-8' if events is None: events = document.events # Prefix if prefix is not None: stream = set_prefix(events, prefix) events = list(stream) elif type(events) in (GeneratorType, XMLParser): events = list(events) # Initialize the namespace stacks stack = NamespaceStack() stack.append(namespace) repeat = NamespaceStack() # Process stream = process(events, 0, len(events), stack, repeat, encoding, skip) # Return if mode == 'events': return stream elif mode == 'xml': return stream_to_str(stream, encoding) elif mode == 'xhtml': return stream_to_str_as_xhtml(stream, encoding) elif mode == 'html': return stream_to_str_as_html(stream, encoding) raise ValueError, 'unexpected mode "%s"' % mode
def stl(document=None, namespace=freeze({}), prefix=None, events=None, mode='events', skip=(DOCUMENT_TYPE,)): # Input encoding = 'utf-8' if events is None: events = document.events # Prefix if prefix is not None: stream = set_prefix(events, prefix) events = list(stream) elif isinstance(events, (GeneratorType, XMLParser)): events = list(events) # Initialize the namespace stacks stack = NamespaceStack() stack.append(namespace) repeat = NamespaceStack() # Process stream = process(events, 0, len(events), stack, repeat, encoding, skip) # Return if mode == 'events': return stream elif mode == 'xml': return stream_to_str(stream, encoding) elif mode == 'xhtml': return stream_to_str_as_xhtml(stream, encoding) elif mode == 'html': return stream_to_str_as_html(stream, encoding) raise ValueError, 'unexpected mode "%s"' % mode
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 default(self, o): if isinstance(o, Decimal): return str(o) elif isinstance(o, datetime): return o.isoformat() elif isinstance(o, date): return o.isoformat() elif isinstance(o, time): return o.isoformat() elif isinstance(o, MSG): return o.gettext() elif isinstance(o, XMLParser): return stream_to_str_as_html(o) return JSONEncoder.default(self, o)
def to_str(text, format, encoding='utf-8'): if format == 'xml': events = block_stream(text) return stream_to_str(events, encoding) elif format == 'xhtml': events = to_html_events(text) return stream_to_str_as_xhtml(events, encoding) elif format == 'html': events = to_html_events(text) return stream_to_str_as_html(events, encoding) elif format == 'latex': events = block_stream(text) return stream_to_str_as_latex(events, encoding) raise ValueError, "unexpected format '%s'" % format
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 stl(document=None, namespace=freeze({}), prefix=None, events=None, mode='events', skip=(DOCUMENT_TYPE, )): # Input encoding = 'utf-8' if events is None: events = document.events # Prefix if prefix is not None: stream = set_prefix(events, prefix) events = list(stream) elif type(events) in (GeneratorType, XMLParser): events = list(events) # Initialize the namespace stacks stack = NamespaceStack() stack.append(namespace) repeat = NamespaceStack() # Process stream = process(events, 0, len(events), stack, repeat, encoding, skip) # Return try: if mode == 'events': return stream elif mode == 'xml': return stream_to_str(stream, encoding) elif mode == 'xhtml': return stream_to_str_as_xhtml(stream, encoding) elif mode == 'html': return stream_to_str_as_html(stream, encoding) except STLError, e: error = 'Error in generation of {0}\n'.format(mode) if document: error += 'Template {0}\n'.format(document.key) raise STLError(error + e.message)
def stl(document=None, namespace=freeze({}), prefix=None, events=None, mode='events', skip=(DOCUMENT_TYPE,)): # Input encoding = 'utf-8' if events is None: events = document.events # Prefix if prefix is not None: stream = set_prefix(events, prefix) events = list(stream) elif type(events) in (GeneratorType, XMLParser): events = list(events) # Initialize the namespace stacks stack = NamespaceStack() stack.append(namespace) repeat = NamespaceStack() # Process stream = process(events, 0, len(events), stack, repeat, encoding, skip) # Return try: if mode == 'events': return stream elif mode == 'xml': return stream_to_str(stream, encoding) elif mode == 'xhtml': return stream_to_str_as_xhtml(stream, encoding) elif mode == 'html': return stream_to_str_as_html(stream, encoding) except STLError, e: error = 'Error in generation of {0}\n'.format(mode) if document: error += 'Template {0}\n'.format(document.key) raise STLError(error + e.message)