Example #1
0
 def __init__(self):
     return MultipleReplacer.__init__(self, {
         r'<head.*?>': self._injector_for_location('head'),
         r'</head.*?>': self._injector_for_location('headbottom', False),
         r'<body.*?>': self._injector_for_location('bodytop'),
         r'</body.*?>': self._injector_for_location('bodybottom', False)
         }, re.I|re.M)
Example #2
0
    def __call__(self, html, resources=None, encoding=None, render_filter=None, environ=None):
        """Injects resources, if any, into html string when called.

        .. note::
           Ignore the ``self`` parameter if seeing this as
           :func:`tw.core.resource_injector.inject_resources` docstring
           since it is an alias for an instance method of a private class.

        ``html`` must be a ``encoding`` encoded string. If ``encoding`` is not
        given it will be tried to be derived from a <meta>.

        Resources for current request can be obtained by calling
        ``tw.framework.pop_resources()``. This will remove resources
        from request and a further call to ``pop_resources()`` will return an
        empty dict.
        """
        if resources is None:
            # it crucial to us tw.framework here, and
            # not import framework from tw, as it used
            # to be, because otherwise we end up fetching
            # the wrong framework - always the default (which
            # defies the use of the StackedObjectProxy)
            resources = tw.framework.pop_resources()
        if resources:
            # Only inject if there are resources registered for injection
            encoding = encoding or find_charset(html) or 'ascii'
            html = MultipleReplacer.__call__(self, html, resources, encoding, render_filter=render_filter, environ=None)
        return html
Example #3
0
    def __call__(self, html, resources=None, encoding=None):
        """Injects resources, if any, into html string when called.

        .. note::
           Ignore the ``self`` parameter if seeing this as
           :func:`tw.core.resource_injector.inject_resources` docstring
           since it is an alias for an instance method of a private class.

        ``html`` must be a ``encoding`` encoded string. If ``encoding`` is not
        given it will be tried to be derived from a <meta>.
        
        Resources for current request can be obtained by calling
        ``tw.framework.pop_resources()``. This will remove resources
        from request and a further call to ``pop_resources()`` will return an
        empty dict.
        """
        if resources is None:
            resources = framework.pop_resources()
        if resources:
            # Only inject if there are resources registered for injection
            encoding = encoding or find_charset(html) or 'ascii'
            html = MultipleReplacer.__call__(self, html, resources, encoding)
        return html
    def __call__(self, html, resources=None, encoding=None):
        """Injects resources, if any, into html string when called.

        .. note::
           Ignore the ``self`` parameter if seeing this as
           :func:`tw.core.resource_injector.inject_resources` docstring
           since it is an alias for an instance method of a private class.

        ``html`` must be a ``encoding`` encoded string. If ``encoding`` is not
        given it will be tried to be derived from a <meta>.
        
        Resources for current request can be obtained by calling
        ``tw.framework.pop_resources()``. This will remove resources
        from request and a further call to ``pop_resources()`` will return an
        empty dict.
        """
        if resources is None:
            resources = framework.pop_resources()
        if resources:
            # Only inject if there are resources registered for injection
            encoding = encoding or find_charset(html) or 'ascii'
            html = MultipleReplacer.__call__(self, html, resources, encoding)
        return html