Exemple #1
0
    def pisaPreLoop2(node, context, collect=False):
        """
        Collect all CSS definitions
        """

        data = u""
        if node.nodeType == Node.TEXT_NODE and collect:
            data = node.data

        elif node.nodeType == Node.ELEMENT_NODE:
            name = node.tagName.lower()

            # print name, node.attributes.items()
            if name in ("style", "link"):
                attr = pisaGetAttributes(context, name, node.attributes)
                print " ", attr
                media = [
                    x.strip() for x in attr.media.lower().split(",")
                    if x.strip()
                ]
                # print repr(media)

                if (attr.get("type", "").lower() in ("", "text/css")
                        and (not media or "all" in media or "print" in media
                             or "pdf" in media)):

                    if name == "style":
                        for node in node.childNodes:
                            data += pisaPreLoop2(node, context, collect=True)
                        #context.addCSS(data)
                        return u""
                        #collect = True

                    if name == "link" and attr.href and attr.rel.lower(
                    ) == "stylesheet":
                        # print "CSS LINK", attr
                        context.addCSS('\n@import "%s" %s;' %
                                       (attr.href, ",".join(media)))
                        # context.addCSS(unicode(file(attr.href, "rb").read(), attr.charset))
        #else:
        #    print node.nodeType

        for node in node.childNodes:
            result = pisaPreLoop2(node, context, collect=collect)
            if collect:
                data += result

        return data
Exemple #2
0
    def pisaPreLoop2(node, context, collect=False):
        """
        Collect all CSS definitions
        """

        data = u""
        if node.nodeType == Node.TEXT_NODE and collect:
            data = node.data

        elif node.nodeType == Node.ELEMENT_NODE:
            name = node.tagName.lower()

            # print name, node.attributes.items()
            if name in ("style", "link"):
                attr = pisaGetAttributes(context, name, node.attributes)
                print " ", attr
                media = [x.strip() for x in attr.media.lower().split(",") if x.strip()]
                # print repr(media)

                if (attr.get("type", "").lower() in ("", "text/css") and (
                    not media or
                    "all" in media or
                    "print" in media or
                    "pdf" in media)):

                    if name == "style":
                        for node in node.childNodes:
                            data += pisaPreLoop2(node, context, collect=True)
                        #context.addCSS(data)
                        return u""
                        #collect = True

                    if name == "link" and attr.href and attr.rel.lower() == "stylesheet":
                        # print "CSS LINK", attr
                        context.addCSS('\n@import "%s" %s;' % (attr.href, ",".join(media)))
                        # context.addCSS(unicode(file(attr.href, "rb").read(), attr.charset))
        #else:
        #    print node.nodeType

        for node in node.childNodes:
            result = pisaPreLoop2(node, context, collect=collect)
            if collect:
                data += result

        return data