Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to inline JS and CSS #491

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pipeline/templates/pipeline/inline_css.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<style type="{{ type }}"{% if media %} media="{{ media }}"{% endif %}{% if title %} title="{{ title|default:"all" }}"{% endif %}{% if charset %} charset="{{ charset }}"{% endif %}>
{{ source|safe }}
</style>
3 changes: 3 additions & 0 deletions pipeline/templates/pipeline/inline_css.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<style type="{{ type }}"{% if media %} media="{{ media }}"{% endif %}{% if title %} title="{{ title|default:"all" }}"{% endif %}{% if charset %} charset="{{ charset }}"{% endif %}>
{{ source|safe }}
</style>
43 changes: 37 additions & 6 deletions pipeline/templatetags/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ..conf import settings
from ..packager import Packager, PackageNotFound
from ..utils import guess_type
import os


register = template.Library()
Expand Down Expand Up @@ -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)
Expand All @@ -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({
Expand All @@ -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)
Expand All @@ -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({
Expand All @@ -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)