Пример #1
0
def normalize_result(result, headers):
    if hasattr(result, 'read'): content = result  # file-like object
    else: content = tostring(result)
    headers = dict([(name.replace('_', '-').title(), str(value))
                    for name, value in headers.items()])
    media_type = headers.pop('Type', None)
    charset = headers.pop('Charset', None)
    content_type = headers.get('Content-Type')
    if content_type:
        media_type, type_params = cgi.parse_header(content_type)
        charset = type_params.get('charset', 'iso-8859-1')
    else:
        if media_type is None: media_type = getattr(result, 'media_type', None)
        if media_type is None:
            if isinstance(content, (Html, StrHtml)): media_type = 'text/html'
            else: media_type = 'text/plain'
        if charset is None: charset = getattr(result, 'charset', 'UTF-8')
        content_type = '%s; charset=%s' % (media_type, charset)
        headers['Content-Type'] = content_type
    if hasattr(content, 'read') \
       or media_type != 'text/html' \
       or isinstance(content, (Html, StrHtml)):
        pass
    elif isinstance(content, unicode):
        content = Html(content)
    elif isinstance(content, str):
        content = StrHtml(content)
    else:
        assert False  # pragma: no cover
    return content, headers
Пример #2
0
def css_link(link):
    if isinstance(link, basestring): link = (link,)
    elif len(link) > 3: raise TypeError('too many parameters for CSS reference')
    href, media, cond = (link + (None, None))[:3]
    result = '<link rel="stylesheet" href="%s" type="text/css"%s>' \
             % (href, ' media="%s"' % media if media else '')
    if cond: result = '<!--[%s]>%s<![endif]-->' % (cond, result)
    return StrHtml(result)
Пример #3
0
def postprocess(content, stylesheets, component_stylesheets, scripts):
    assert isinstance(content, basestring)
    if isinstance(content, (Html, StrHtml)): pass
    elif isinstance(content, str): content = StrHtml(content)
    elif isinstance(content, unicode): content = Html(content)

    if not stylesheets: stylesheets = options.STD_STYLESHEETS
    base_css = css_links(stylesheets)
    if base_css: base_css += StrHtml('\n')
    component_css = css_links(component_stylesheets)
    if component_css: component_css += StrHtml('\n')
    scripts = script_links(scripts)
    if scripts: scripts += StrHtml('\n')

    doctype = ''
    try:
        match = element_re.search(content)
        if match is None or match.group(2).lower() not in header_tags:
            doctype = StrHtml(options.STD_DOCTYPE)
            head = ''
            body = content
        else:
            first_element = match.group(2).lower()

            for match in element_re.finditer(content):
                element = match.group(2).lower()
                if element not in header_tags: break
                last_match = match
            bound = last_match.end(1)
            head = content.__class__(content[:bound])
            body = content.__class__(content[bound:])

            if first_element in ('!doctype', 'html'): raise _UsePlaceholders
            doctype = StrHtml(options.STD_DOCTYPE)

        match = element_re.search(body)
        if match is None or match.group(2).lower() != 'body':
            if 'blueprint' in base_css: body = StrHtml('<div class="container">\n%s\n</div>\n') % body
            body = StrHtml('<body>\n%s</body>') % body

        match = element_re.search(head)
        if match is not None and match.group(2).lower() == 'head': raise _UsePlaceholders
        if css_re.search(head) is not None: base_css = ''
        head = StrHtml('<head>') + favicon_links + base_css + head + component_css + scripts + StrHtml('</head>')

    except _UsePlaceholders:
        head = head.replace(options.BASE_STYLESHEETS_PLACEHOLDER, base_css, 1)
        head = head.replace(options.COMPONENT_STYLESHEETS_PLACEHOLDER, component_css, 1)
        head = head.replace(options.SCRIPTS_PLACEHOLDER, scripts, 1)
        head = content.__class__(head)

    if doctype: return StrHtml('\n').join([doctype, head, body])
    else: return StrHtml('\n').join([head, body])
Пример #4
0
 def __str__(field):
     return StrHtml(unicode(field).encode('ascii', 'xmlcharrefreplace'))
def postprocess(content, stylesheets, component_stylesheets, scripts):
    assert isinstance(content, basestring)
    if isinstance(content, (Html, StrHtml)): pass
    elif isinstance(content, str): content = StrHtml(content)
    elif isinstance(content, unicode): content = Html(content)

    if not stylesheets: stylesheets = options.STD_STYLESHEETS
    base_css = css_links(stylesheets)
    if base_css: base_css += StrHtml('\n')
    component_css = css_links(component_stylesheets)
    if component_css: component_css += StrHtml('\n')
    scripts = script_links(scripts)
    if scripts: scripts += StrHtml('\n')

    doctype = ''
    try:
        match = element_re.search(content)
        if match is None or match.group(2).lower() not in header_tags:
            doctype = StrHtml(options.STD_DOCTYPE)
            head = ''
            body = content
        else:
            first_element = match.group(2).lower()

            for match in element_re.finditer(content):
                element = match.group(2).lower()
                if element not in header_tags: break
                last_match = match
            bound = last_match.end(1)
            head = content.__class__(content[:bound])
            body = content.__class__(content[bound:])

            if first_element in ('!doctype', 'html'): raise _UsePlaceholders
            doctype = StrHtml(options.STD_DOCTYPE)

        match = element_re.search(body)
        if match is None or match.group(2).lower() != 'body':
            if 'blueprint' in base_css:
                body = StrHtml('<div class="container">\n%s\n</div>\n') % body
            body = StrHtml('<body>\n%s</body>') % body

        match = element_re.search(head)
        if match is not None and match.group(2).lower() == 'head':
            raise _UsePlaceholders
        if css_re.search(head) is not None: base_css = ''
        head = StrHtml(
            '<head>'
        ) + favicon_links + base_css + head + component_css + scripts + StrHtml(
            '</head>')

    except _UsePlaceholders:
        head = head.replace(options.BASE_STYLESHEETS_PLACEHOLDER, base_css, 1)
        head = head.replace(options.COMPONENT_STYLESHEETS_PLACEHOLDER,
                            component_css, 1)
        head = head.replace(options.SCRIPTS_PLACEHOLDER, scripts, 1)
        head = content.__class__(head)

    if doctype: return StrHtml('\n').join([doctype, head, body])
    else: return StrHtml('\n').join([head, body])
def script_links(links):
    return StrHtml('\n').join(script_link(link) for link in links)
def script_link(link):
    return StrHtml('<script type="text/javascript" src="%s"></script>') % link
def css_links(links):
    return StrHtml('\n').join(css_link(link) for link in links)