def _injector(environ, start_response): req = Request(environ) resp = req.get_response(app) content_type = resp.headers.get('Content-Type','text/plain').lower() if 'html' in content_type: resources = framework.pop_resources() if resources: resp.body = inject_resources(resp.body, resources, resp.charset) return resp(environ, start_response)
def _injector(environ, start_response): req = Request(environ) resp = req.get_response(app) content_type = resp.headers.get('Content-Type', 'text/plain').lower() if 'html' in content_type: resources = framework.pop_resources() if resources: resp.body = inject_resources(resp.body, resources, resp.charset) return resp(environ, start_response)
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