Пример #1
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 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
Пример #2
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
    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 tidy_html(body):
    if tidy:
        body = to_utf8(body)
        body = tidy.parseString(body, indent=1, char_encoding='utf8',
                                output_xhtml=1, word_2000=1)
        body = str(body)

    return stream_to_str_as_xhtml(HTMLParser(body))
Пример #4
0
def tidy_html(body):
    if tidy:
        body = to_utf8(body)
        body = tidy.parseString(body,
                                indent=1,
                                char_encoding='utf8',
                                output_xhtml=1,
                                word_2000=1)
        body = str(body)

    return stream_to_str_as_xhtml(HTMLParser(body))
Пример #5
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
Пример #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
    def get_item_value(self, resource, context, item, column, site_root):
        brain, item_resource = item
        if column == 'description':
            view = getattr(item_resource, 'tag_view',
                           getattr(item_resource, 'view'))
            if view:
                content = view.GET(item_resource, context)
                # set prefix
                prefix = site_root.get_pathto(item_resource)
                content = set_prefix(content, '%s/' % prefix, uri=context.uri)
                content = stream_to_str_as_xhtml(content)
                return content.decode('utf-8')
            else:
                return item_resource.get_property('description')

        return BaseRSS.get_item_value(self, resource, context, item,
                                      column, site_root)
Пример #8
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)
Пример #9
0
 def get_item_value(self, resource, context, item, column, site_root):
     brain, item_resource = item
     if column in ('link', 'guid'):
         value = context.uri.resolve(context.get_link(item_resource))
         return str(value)
     elif column == 'pubDate':
         return brain.pub_datetime
     elif column == 'title':
         return item_resource.get_title()
     elif column == 'description':
         if issubclass(item_resource.__class__, WebPage):
             data = item_resource.get_html_data()
             if data is None:
                 # Skip empty content
                 return ''
             # Set the prefix
             prefix = site_root.get_pathto(item_resource)
             data = set_prefix(data, '%s/' % prefix, uri=context.uri)
             data = stream_to_str_as_xhtml(data)
             return data.decode('utf-8')
         else:
             return item_resource.get_property('description')
Пример #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
 def encode(value):
     if value is None:
         return ''
     return stream_to_str_as_xhtml(value)
Пример #12
0
 def encode(value):
     if value is None:
         return ''
     return stream_to_str_as_xhtml(value)