From d9de5ee12f06b2455a1b2179f419a43500b1afa2 Mon Sep 17 00:00:00 2001 From: simultech Date: Thu, 3 Sep 2015 17:40:11 +1000 Subject: [PATCH] Added the ability to inline JS and CSS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds the ability to do inline JS/CSS with pipeline by adding ‘inline’ to the template tag, aka: {% javascript 'head_js' 'inline' %} --- pipeline/templates/pipeline/inline_css.html | 3 ++ pipeline/templates/pipeline/inline_css.jinja | 3 ++ pipeline/templatetags/pipeline.py | 43 +++++++++++++++++--- 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 pipeline/templates/pipeline/inline_css.html create mode 100644 pipeline/templates/pipeline/inline_css.jinja diff --git a/pipeline/templates/pipeline/inline_css.html b/pipeline/templates/pipeline/inline_css.html new file mode 100644 index 00000000..29658e2e --- /dev/null +++ b/pipeline/templates/pipeline/inline_css.html @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/pipeline/templates/pipeline/inline_css.jinja b/pipeline/templates/pipeline/inline_css.jinja new file mode 100644 index 00000000..29658e2e --- /dev/null +++ b/pipeline/templates/pipeline/inline_css.jinja @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/pipeline/templatetags/pipeline.py b/pipeline/templatetags/pipeline.py index a6962077..9623f9e9 100644 --- a/pipeline/templatetags/pipeline.py +++ b/pipeline/templatetags/pipeline.py @@ -11,6 +11,7 @@ from ..conf import settings from ..packager import Packager, PackageNotFound from ..utils import guess_type +import os register = template.Library() @@ -63,8 +64,9 @@ def render_compressed(self, package, package_type): class StylesheetNode(PipelineMixin, template.Node): - def __init__(self, name): + def __init__(self, name, inline): self.name = name + self.inline = inline def render(self, context): super(StylesheetNode, self).render(context) @@ -77,6 +79,12 @@ def render(self, context): return self.render_compressed(package, 'css') def render_css(self, package, path): + if self.inline == 'inline': + data = "" + with open (os.getcwd()+"/static/"+path, "r") as myfile: + data=myfile.read() + if data != "": + return self.render_inline(package=package, css=data) template_name = package.template_name or "pipeline/css.html" context = package.extra_context context.update({ @@ -85,14 +93,22 @@ def render_css(self, package, path): }) return render_to_string(template_name, context) + def render_inline(self, package, css): + context = package.extra_context + context.update({ + 'source': css + }) + return render_to_string("pipeline/inline_css.html", context) + def render_individual_css(self, package, paths, **kwargs): tags = [self.render_css(package, path) for path in paths] return '\n'.join(tags) class JavascriptNode(PipelineMixin, template.Node): - def __init__(self, name): + def __init__(self, name, inline): self.name = name + self.inline = inline def render(self, context): super(JavascriptNode, self).render(context) @@ -105,6 +121,12 @@ def render(self, context): return self.render_compressed(package, 'js') def render_js(self, package, path): + if self.inline == 'inline': + data = "" + with open (os.getcwd()+"/static/"+path, "r") as myfile: + data=myfile.read() + if data != "": + return self.render_inline(package=package, js=data) template_name = package.template_name or "pipeline/js.html" context = package.extra_context context.update({ @@ -127,19 +149,28 @@ def render_individual_js(self, package, paths, templates=None): return '\n'.join(tags) + @register.tag def stylesheet(parser, token): + inline = "" try: - tag_name, name = token.split_contents() + try: + tag_name, name, inline = token.split_contents() + except ValueError: + tag_name, name = token.split_contents() except ValueError: raise template.TemplateSyntaxError('%r requires exactly one argument: the name of a group in the PIPELINE_CSS setting' % token.split_contents()[0]) - return StylesheetNode(name) + return StylesheetNode(name, inline) @register.tag def javascript(parser, token): + inline = "" try: - tag_name, name = token.split_contents() + try: + tag_name, name, inline = token.split_contents() + except ValueError: + tag_name, name = token.split_contents() except ValueError: raise template.TemplateSyntaxError('%r requires exactly one argument: the name of a group in the PIPELINE_JS setting' % token.split_contents()[0]) - return JavascriptNode(name) + return JavascriptNode(name, inline)