示例#1
0
文件: stl.py 项目: Nabellaleen/itools
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
示例#2
0
文件: stl.py 项目: pombredanne/itools
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
示例#3
0
 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)
示例#4
0
 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)
示例#5
0
文件: utils.py 项目: nkhine/itools
 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)
示例#6
0
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
示例#7
0
文件: rest.py 项目: kennym/itools
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
示例#8
0
    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'
示例#9
0
    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'
示例#10
0
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)
示例#11
0
文件: stl.py 项目: hforge/itools
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)