Commit 9aa44e53 by Timothée Peignier

add defaut template function. close #12

parent f52ae576
......@@ -19,6 +19,9 @@ MHTML_START = "/*\r\nContent-Type: multipart/related; boundary=\"MHTML_MARK\"\r\
MHTML_SEPARATOR = "--MHTML_MARK\r\n"
MHTML_END = "\r\n--MHTML_MARK--\r\n*/\r\n"
DEFAULT_TEMPLATE_FUNC = "template"
TEMPLATE_FUNC = """var template = function(str){var fn = new Function('obj', 'var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push(\''+str.replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/<%=([\s\S]+?)%>/g,function(match,code){return "',"+code.replace(/\\'/g, "'")+",'";}).replace(/<%([\s\S]+?)%>/g,function(match,code){return "');"+code.replace(/\\'/g, "'").replace(/[\r\n\t]/g,' ')+"__p.push('";}).replace(/\r/g,'\\r').replace(/\n/g,'\\n').replace(/\t/g,'\\t')+"');}return __p.join('');");return fn;};"""
MIME_TYPES = {
'.png': 'image/png',
'.jpg': 'image/jpeg',
......@@ -95,8 +98,10 @@ class Compressor(object):
settings.PIPELINE_TEMPLATE_FUNC,
contents
)
compiler = TEMPLATE_FUNC if settings.PIPELINE_TEMPLATE_FUNC == DEFAULT_TEMPLATE_FUNC else ""
return "\n".join([
"%(namespace)s = %(namespace)s || {};" % {'namespace': namespace},
compiler,
compiled
])
......
......@@ -22,7 +22,7 @@ PIPELINE_JS = getattr(settings, 'PIPELINE_JS', {})
PIPELINE_TEMPLATE_NAMESPACE = getattr(settings, 'PIPELINE_TEMPLATE_NAMESPACE', "window.JST")
PIPELINE_TEMPLATE_EXT = getattr(settings, 'PIPELINE_TEMPLATE_EXT', ".jst")
PIPELINE_TEMPLATE_FUNC = getattr(settings, 'PIPELINE_TEMPLATE_FUNC', "_.template")
PIPELINE_TEMPLATE_FUNC = getattr(settings, 'PIPELINE_TEMPLATE_FUNC', "template")
PIPELINE_CSSTIDY_BINARY = getattr(settings, 'PIPELINE_CSSTIDY_BINARY', '/usr/local/bin/csstidy')
PIPELINE_CSSTIDY_ARGUMENTS = getattr(settings, 'PIPELINE_CSSTIDY_ARGUMENTS', '--template=highest')
......
import os
try:
from staticfiles.finders import DefaultStorageFinder
except ImportError:
......
import os
import urlparse
from django.core.files.base import ContentFile
from pipeline.conf import settings
......
.concat {
display: none;
}
\ No newline at end of file
}
.concatenate {
display: block;
}
\ No newline at end of file
}
function() {
alert('this is a test');
}
\ No newline at end of file
}
function concat() {
console.log(arguments);
}
\ No newline at end of file
}
function cat() {
console.log("hello world");
}
\ No newline at end of file
}
......@@ -36,7 +36,6 @@ STATICFILES_FINDERS = (
)
PIPELINE_ROOT = local_path('assets/')
PIPELINE_TEMPLATE_EXT = ".jst"
TEMPLATE_DIRS = (
local_path('templates'),
......
......@@ -6,7 +6,7 @@ from mock import patch
from django.test import TestCase
from pipeline.conf import settings
from pipeline.compressors import Compressor
from pipeline.compressors import Compressor, TEMPLATE_FUNC
from pipeline.compressors.yui import YUICompressor
......@@ -26,14 +26,14 @@ class CompressorTest(TestCase):
'css/first.css',
'css/second.css'
])
self.assertEquals(""".concat {\n display: none;\n}\n.concatenate {\n display: block;\n}""", css)
self.assertEquals(""".concat {\n display: none;\n}\n\n.concatenate {\n display: block;\n}\n""", css)
def test_concatenate(self):
js = self.compressor.concatenate([
'js/first.js',
'js/second.js'
])
self.assertEquals("""function concat() {\n console.log(arguments);\n}\nfunction cat() {\n console.log("hello world");\n}""", js)
self.assertEquals("""function concat() {\n console.log(arguments);\n}\n\nfunction cat() {\n console.log("hello world");\n}\n""", js)
@patch.object(base64, 'b64encode')
def test_encoded_content(self, mock):
......@@ -71,12 +71,12 @@ class CompressorTest(TestCase):
def test_compile_templates(self):
templates = self.compressor.compile_templates(['templates/photo/list.jst'])
self.assertEquals(templates, """window.JST = window.JST || {};\nwindow.JST['list'] = _.template('<div class="photo"> <img src="<%= src %>" /> <div class="caption"> <%= caption %> </div></div>');\n""")
self.assertEquals(templates, """window.JST = window.JST || {};\n%s\nwindow.JST['list'] = template('<div class="photo"> <img src="<%%= src %%>" /> <div class="caption"> <%%= caption %%> </div></div>');\n""" % TEMPLATE_FUNC)
templates = self.compressor.compile_templates([
'templates/video/detail.jst',
'templates/photo/detail.jst'
])
self.assertEqual(templates, """window.JST = window.JST || {};\nwindow.JST['video_detail'] = _.template('<div class="video"> <video src="<%= src %>" /> <div class="caption"> <%= description %> </div></div>');\nwindow.JST[\'photo_detail\'] = _.template(\'<div class="photo"> <img src="<%= src %>" /> <div class="caption"> <%= caption %> by <%= author %> </div></div>\');\n""")
self.assertEqual(templates, """window.JST = window.JST || {};\n%s\nwindow.JST['video_detail'] = template('<div class="video"> <video src="<%%= src %%>" /> <div class="caption"> <%%= description %%> </div></div>');\nwindow.JST[\'photo_detail\'] = template(\'<div class="photo"> <img src="<%%= src %%>" /> <div class="caption"> <%%= caption %%> by <%%= author %%> </div></div>\');\n""" % TEMPLATE_FUNC)
def test_embeddable(self):
self.assertFalse(self.compressor.embeddable('images/sprite.png', None))
......
import os
from django.test import TestCase
from pipeline.conf import settings
from pipeline.packager import Packager, PackageNotFound
......@@ -38,20 +35,3 @@ class PackagerTest(TestCase):
}
})
self.assertEqual(packages['templates'].templates, ['templates/photo/list.jst'])
def test_individual_url(self):
"""Check that individual URL is correctly generated"""
packager = Packager()
filename = os.path.join(settings.PIPELINE_ROOT, u'js/application.js')
individual_url = packager.individual_url(filename)
self.assertEqual(individual_url, "/static/js/application.js")
def test_periods_safe_individual_url(self):
"""Check that the periods in file names do not get replaced by individual_url when
PIPELINE_ROOT/STATIC_ROOT is not set, such as in development
"""
settings.PIPELINE_ROOT = settings.STATIC_ROOT = settings.MEDIA_ROOT = ""
packager = Packager()
filename = os.path.join(settings.PIPELINE_ROOT, u'js/application.js')
individual_url = packager.individual_url(filename)
self.assertEqual(individual_url, "/static/js/application.js")
......@@ -13,8 +13,10 @@ class StorageTest(TestCase):
),
'manifest': False,
'output_filename': 'testing.css',
}
}
}
settings.PIPELINE_JS_COMPRESSOR = None
settings.PIPELINE_CSS_COMPRESSOR = None
self.storage = PipelineStorage()
def test_post_process_dry_run(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment