From ad945ad8a058a04478675bafc0f8a1d8096bbdbc Mon Sep 17 00:00:00 2001 From: dd Date: Mon, 6 Nov 2017 03:10:14 +0300 Subject: [PATCH 1/8] add inline arg to templatetags --- pipeline/templatetags/pipeline.py | 68 ++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 14 deletions(-) diff --git a/pipeline/templatetags/pipeline.py b/pipeline/templatetags/pipeline.py index da1b88a1..e9a4c5b2 100644 --- a/pipeline/templatetags/pipeline.py +++ b/pipeline/templatetags/pipeline.py @@ -53,6 +53,12 @@ def render(self, context): except VariableDoesNotExist: pass + if self.inline_var: + try: + self.inline = template.Variable(self.inline_var).resolve(context) + except VariableDoesNotExist: + self.inline = True + def render_compressed(self, package, package_name, package_type): """Render HTML for the package. @@ -115,6 +121,13 @@ def render_compressed_sources(self, package, package_name, package_type): return method(package, paths, templates=templates) + def render_inline(self, package, source, package_type): + context = package.extra_context + context.update({ + 'source': source + }) + return render_to_string("pipeline/inline_%s.html" % package_type, context) + def render_error(self, package_type, package_name, e): return render_to_string('pipeline/compile_error.html', { 'package_type': package_type, @@ -125,8 +138,12 @@ def render_error(self, package_type, package_name, e): class StylesheetNode(PipelineMixin, template.Node): - def __init__(self, name): + inline = False + inline_var = None + + def __init__(self, name, inline): self.name = name + self.inline_var = inline def render(self, context): super(StylesheetNode, self).render(context) @@ -137,9 +154,18 @@ def render(self, context): except PackageNotFound: logger.warn("Package %r is unknown. Check PIPELINE['STYLESHEETS'] in your settings.", package_name) return '' # fail silently, do not return anything if an invalid group is specified + return self.render_compressed(package, package_name, 'css') def render_css(self, package, path): + if self.inline: + src = "" + with open (staticfiles_storage.path(path), "r") as resourse: + src = resourse.read() + src = src.replace('../', staticfiles_storage.url('/'.join(path.split('/')[:-2])+'/')) + if src: + return self.render_inline(package, src, 'css') + template_name = package.template_name or "pipeline/css.html" context = package.extra_context context.update({ @@ -158,8 +184,12 @@ def render_error_css(self, package_name, e): class JavascriptNode(PipelineMixin, template.Node): - def __init__(self, name): + inline = False + inline_var = None + + def __init__(self, name, inline): self.name = name + self.inline_var = inline def render(self, context): super(JavascriptNode, self).render(context) @@ -170,6 +200,7 @@ def render(self, context): except PackageNotFound: logger.warn("Package %r is unknown. Check PIPELINE['JAVASCRIPT'] in your settings.", package_name) return '' # fail silently, do not return anything if an invalid group is specified + return self.render_compressed(package, package_name, 'js') def render_js(self, package, path): @@ -179,19 +210,20 @@ def render_js(self, package, path): 'type': guess_type(path, 'text/javascript'), 'url': mark_safe(staticfiles_storage.url(path)) }) - return render_to_string(template_name, context) - def render_inline(self, package, js): - context = package.extra_context - context.update({ - 'source': js - }) - return render_to_string("pipeline/inline_js.html", context) + if self.inline: + src = "" + with open (staticfiles_storage.path(package.output_filename), "r") as resourse: + src = resourse.read() + if src: + return self.render_inline(package, src, 'js') + + return render_to_string(template_name, context) def render_individual_js(self, package, paths, templates=None): tags = [self.render_js(package, js) for js in paths] if templates: - tags.append(self.render_inline(package, templates)) + tags.append(self.render_inline(package, templates, 'js')) return '\n'.join(tags) def render_error_js(self, package_name, e): @@ -202,16 +234,24 @@ def render_error_js(self, package_name, e): @register.tag def stylesheet(parser, token): try: - tag_name, name = token.split_contents() + try: + tag_name, name, inline = token.split_contents() + except ValueError: + tag_name, name = token.split_contents() + inline = False except ValueError: raise template.TemplateSyntaxError('%r requires exactly one argument: the name of a group in the PIPELINE.STYLESHEETS setting' % token.split_contents()[0]) - return StylesheetNode(name) + return StylesheetNode(name, inline) @register.tag def javascript(parser, token): try: - tag_name, name = token.split_contents() + try: + tag_name, name, inline = token.split_contents() + except ValueError: + tag_name, name = token.split_contents() + inline = False except ValueError: raise template.TemplateSyntaxError('%r requires exactly one argument: the name of a group in the PIPELINE.JAVASVRIPT setting' % token.split_contents()[0]) - return JavascriptNode(name) + return JavascriptNode(name, inline) From aee7da87af2d03c52132062acdf92fc592a98c64 Mon Sep 17 00:00:00 2001 From: dd Date: Mon, 6 Nov 2017 03:18:41 +0300 Subject: [PATCH 2/8] add css inline template --- pipeline/templates/pipeline/inline_css.html | 1 + 1 file changed, 1 insertion(+) create mode 100644 pipeline/templates/pipeline/inline_css.html diff --git a/pipeline/templates/pipeline/inline_css.html b/pipeline/templates/pipeline/inline_css.html new file mode 100644 index 00000000..72d941a3 --- /dev/null +++ b/pipeline/templates/pipeline/inline_css.html @@ -0,0 +1 @@ + From 99ffd9540bd8cd4302b5a147d7401b025a15ef87 Mon Sep 17 00:00:00 2001 From: dd Date: Tue, 7 Nov 2017 01:42:24 +0300 Subject: [PATCH 3/8] some changes --- pipeline/templatetags/pipeline.py | 42 ++++++++++++------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/pipeline/templatetags/pipeline.py b/pipeline/templatetags/pipeline.py index e9a4c5b2..e5380e04 100644 --- a/pipeline/templatetags/pipeline.py +++ b/pipeline/templatetags/pipeline.py @@ -8,7 +8,9 @@ from django import template from django.template.base import Context, VariableDoesNotExist from django.template.loader import render_to_string +from django.template import Template from django.utils.safestring import mark_safe +from django.contrib.staticfiles import finders from ..collector import default_collector from ..conf import settings @@ -25,6 +27,13 @@ class PipelineMixin(object): request = None _request_var = None + inline = False + inline_var = None + + def __init__(self, name, inline=False): + self.name = name + self.inline_var = inline + @property def request_var(self): if not self._request_var: @@ -57,7 +66,8 @@ def render(self, context): try: self.inline = template.Variable(self.inline_var).resolve(context) except VariableDoesNotExist: - self.inline = True + if self.inline_var == 'inline': + self.inline = True def render_compressed(self, package, package_name, package_type): """Render HTML for the package. @@ -138,12 +148,6 @@ def render_error(self, package_type, package_name, e): class StylesheetNode(PipelineMixin, template.Node): - inline = False - inline_var = None - - def __init__(self, name, inline): - self.name = name - self.inline_var = inline def render(self, context): super(StylesheetNode, self).render(context) @@ -184,12 +188,6 @@ def render_error_css(self, package_name, e): class JavascriptNode(PipelineMixin, template.Node): - inline = False - inline_var = None - - def __init__(self, name, inline): - self.name = name - self.inline_var = inline def render(self, context): super(JavascriptNode, self).render(context) @@ -213,7 +211,7 @@ def render_js(self, package, path): if self.inline: src = "" - with open (staticfiles_storage.path(package.output_filename), "r") as resourse: + with open(staticfiles_storage.path(path), "r") as resourse: src = resourse.read() if src: return self.render_inline(package, src, 'js') @@ -234,24 +232,16 @@ def render_error_js(self, package_name, e): @register.tag def stylesheet(parser, token): try: - try: - tag_name, name, inline = token.split_contents() - except ValueError: - tag_name, name = token.split_contents() - inline = False + args = token.split_contents()[1:] except ValueError: raise template.TemplateSyntaxError('%r requires exactly one argument: the name of a group in the PIPELINE.STYLESHEETS setting' % token.split_contents()[0]) - return StylesheetNode(name, inline) + return StylesheetNode(*args) @register.tag def javascript(parser, token): try: - try: - tag_name, name, inline = token.split_contents() - except ValueError: - tag_name, name = token.split_contents() - inline = False + args = token.split_contents()[1:] except ValueError: raise template.TemplateSyntaxError('%r requires exactly one argument: the name of a group in the PIPELINE.JAVASVRIPT setting' % token.split_contents()[0]) - return JavascriptNode(name, inline) + return JavascriptNode(*args) From 4fd8a217c174d0ae2d3cf85fc6a2bdf83fe4d812 Mon Sep 17 00:00:00 2001 From: dd Date: Tue, 7 Nov 2017 14:48:04 +0300 Subject: [PATCH 4/8] clear --- pipeline/templatetags/pipeline.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pipeline/templatetags/pipeline.py b/pipeline/templatetags/pipeline.py index e5380e04..96f70c75 100644 --- a/pipeline/templatetags/pipeline.py +++ b/pipeline/templatetags/pipeline.py @@ -164,7 +164,7 @@ def render(self, context): def render_css(self, package, path): if self.inline: src = "" - with open (staticfiles_storage.path(path), "r") as resourse: + with open(staticfiles_storage.path(path), "r") as resourse: src = resourse.read() src = src.replace('../', staticfiles_storage.url('/'.join(path.split('/')[:-2])+'/')) if src: From 64cb4ece8a406cb8617d5e16800950c8d58d6f20 Mon Sep 17 00:00:00 2001 From: dd Date: Tue, 7 Nov 2017 15:16:45 +0300 Subject: [PATCH 5/8] change error in templatetags --- pipeline/templatetags/pipeline.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/pipeline/templatetags/pipeline.py b/pipeline/templatetags/pipeline.py index 96f70c75..5e343e35 100644 --- a/pipeline/templatetags/pipeline.py +++ b/pipeline/templatetags/pipeline.py @@ -231,17 +231,15 @@ def render_error_js(self, package_name, e): @register.tag def stylesheet(parser, token): - try: - args = token.split_contents()[1:] - except ValueError: - raise template.TemplateSyntaxError('%r requires exactly one argument: the name of a group in the PIPELINE.STYLESHEETS setting' % token.split_contents()[0]) - return StylesheetNode(*args) + args = token.split_contents() + if len(args) < 2: + raise template.TemplateSyntaxError('%r requires first argument: the name of a group in the PIPELINE.STYLESHEETS setting' % args[0]) + return StylesheetNode(*args[1:]) @register.tag def javascript(parser, token): - try: - args = token.split_contents()[1:] - except ValueError: - raise template.TemplateSyntaxError('%r requires exactly one argument: the name of a group in the PIPELINE.JAVASVRIPT setting' % token.split_contents()[0]) - return JavascriptNode(*args) + args = token.split_contents() + if len(args) < 2: + raise template.TemplateSyntaxError('%r requires first argument: the name of a group in the PIPELINE.JAVASVRIPT setting' % args[0]) + return JavascriptNode(*args[1:]) From 6745ec6da5f4e3cb868eba780ae8a8f33d8c9bbc Mon Sep 17 00:00:00 2001 From: dd Date: Tue, 7 Nov 2017 16:06:45 +0300 Subject: [PATCH 6/8] docs --- docs/usage.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/usage.rst b/docs/usage.rst index f453a93b..f0345b7e 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -40,6 +40,14 @@ with the name “scripts”, you would use the following code to output them all {% stylesheet 'stats' %} {% javascript 'scripts' %} +Also you can set second argument to specify inline output :: + + {% stylesheet 'stats' inline %} + {% javascript 'scripts' inline %} + +.. note:: + You can use reserved argument “inline” or other variable + Form Media ========== From 3702c0492ed43df138c0cb7fffdf9b9d6db1d401 Mon Sep 17 00:00:00 2001 From: dd Date: Mon, 29 Jan 2018 16:30:45 +0300 Subject: [PATCH 7/8] gitignore fix --- .gitignore | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 9f977e9d..01819dd7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,29 +1,24 @@ -.AppleDouble -*.pyc -:2e_* -*.tmproj -.*.swp -*.swo -build -dist -MANIFEST -docs/_build/ +*.egg *.egg-info -.coverage +*.log +*.py[cod] +*.swo +*.tmproj +.* +:2e_* + +/bin +/build +/dist +/docs/_build +/include +/lib coverage/ -tests/static/ +django-pipeline-*/ +MANIFEST +node_modules/ +pip-selfcheck.json tests/assets/js/dummy.js tests/node_modules/ -.tox/ -.DS_Store -.idea -.venv -.project -.pydevproject -.ropeproject -__pycache__ -npm-debug.log tests/npm-cache -django-pipeline-*/ -.tags -node_modules/ +tests/static/ From 113d7dfcc8ef393515ec2db88de2811d1c33f896 Mon Sep 17 00:00:00 2001 From: dd Date: Mon, 29 Jan 2018 18:41:49 +0300 Subject: [PATCH 8/8] fix jinja2 --- pipeline/jinja2/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pipeline/jinja2/__init__.py b/pipeline/jinja2/__init__.py index d5aeb8f9..cc57a292 100644 --- a/pipeline/jinja2/__init__.py +++ b/pipeline/jinja2/__init__.py @@ -10,7 +10,7 @@ from ..templatetags.pipeline import PipelineMixin -class PipelineExtension(PipelineMixin, Extension): +class PipelineExtension(Extension, PipelineMixin): tags = set(['stylesheet', 'javascript']) def parse(self, parser):