def finalizeContent(self, resource, content): """Finalize the resource content.""" compression = resource.getCompression() if compression != 'none' and not self.getDebugMode(): orig_url = "%s/%s?original=1" % (self.absolute_url(), resource.getId()) content = "/* %s */\n%s" % (orig_url, self._compressCSS(content, compression)) m = resource.getMedia() if m: content = '@media %s {\n%s\n}\n' % (m, content) if resource.getApplyPrefix() and not self.getDebugMode(): prefix = aq_parent(self).absolute_url_path() if prefix.endswith('/'): prefix = prefix[:-1] resourceName = resource.getId() if '/' in resourceName: prefix += '/' + '/'.join(resourceName.split('/')[:-1]) content = applyPrefix(content, prefix) return content
def test_deep_prefix(self): self.assertEqual( """\ @import url(/some/prefix/common.css); #foo { background-image: url ( '/some/prefix/spacer.jpg' ); } .bar { background-image: url('/some/images/test.jpg' ); } p { background: url("/some/prefix/test.jpg") repeat-x; } table { background: url("/absolute.jpg") repeat-x; } table p { background: url("http://example.org/absolute.jpg") repeat-x; } li { background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="); } """, applyPrefix(self.cssSource, '/some/prefix'))
def test_deep_prefix(self): self.assertEqual("""\ @import url(/some/prefix/common.css); #foo { background-image: url ( '/some/prefix/spacer.jpg' ); } .bar { background-image: url('/some/images/test.jpg' ); } p { background: url("/some/prefix/test.jpg") repeat-x; } table { background: url("/absolute.jpg") repeat-x; } table p { background: url("http://example.org/absolute.jpg") repeat-x; } li { background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="); } """, applyPrefix(self.cssSource, '/some/prefix'))
def test_deep_prefix(self): self.assertEqual( """\ @import url(/some/prefix/common.css); #foo { background-image: url ( '/some/prefix/spacer.jpg' ); } .bar { background-image: url('/some/images/test.jpg' ); } p { background: url("/some/prefix/test.jpg") repeat-x; } table { background: url("/absolute.jpg") repeat-x; } table p { background: url("http://example.org/absolute.jpg") repeat-x; } """, applyPrefix(self.cssSource, '/some/prefix'))
def get_resources_content(self, resources): """Resources must be a list of resource ids. This method return the content of each resources appended in one string """ data = StringIO() if resources is None: resources = self.get_resources() upath = self.context.absolute_url_path() for resourceid in resources: try: resource = self.context.restrictedTraverse( resourceid) except KeyError, e: logger.error(e) continue if not resource: continue if self._header_template: data.write( self._header_template % ( resource.context.__name__ ) ) data.write(u"\n") fic = open(resource.context.path, 'r') content = fic.read() fic.close() try: content = unicode(content) except Exception, e: content = unicode( content.decode('utf-8')) if self._css: upath = upath.rstrip('/') prefix = '/'.join([upath, resourceid]) if prefix.endswith('/'): prefix = prefix[:-1] prefix = '/'.join( prefix.split('/')[:-1]) content = applyPrefix(content, prefix) data.write( self.pack(content) ) data.write(u"\n")
def test_deep_prefix(self): self.assertEquals("""\ @import url(/some/prefix/common.css); #foo { background-image: url ( '/some/prefix/spacer.jpg' ); } .bar { background-image: url('/some/images/test.jpg' ); } p { background: url("/some/prefix/test.jpg") repeat-x; } table { background: url("/absolute.jpg") repeat-x; } table p { background: url("http://example.org/absolute.jpg") repeat-x; } """, applyPrefix(self.cssSource, '/some/prefix'))
def get_resources_content(self, resources): """Resources must be a list of resource ids. This method return the content of each resources appended in one string """ data = StringIO() if resources is None: resources = self.get_resources() upath = self.context.absolute_url_path() for resourceid in resources: try: resource = self.context.restrictedTraverse(resourceid) except KeyError, e: logger.error(e) continue if not resource: continue if self._header_template: data.write(self._header_template % (resource.context.__name__)) data.write(u"\n") fic = open(resource.context.path, 'r') content = fic.read() fic.close() try: content = unicode(content) except Exception, e: content = unicode(content.decode('utf-8')) if self._css: upath = upath.rstrip('/') prefix = '/'.join([upath, resourceid]) if prefix.endswith('/'): prefix = prefix[:-1] prefix = '/'.join(prefix.split('/')[:-1]) content = applyPrefix(content, prefix) data.write(self.pack(content)) data.write(u"\n")
class Resources(BrowserView): _key = None _header_template = u"\n/* collective.js.jqueryui: %s */\n" _mimetype = None _tool = None _toolid = None _packer = None _js = False _css = False @property def tool(self): if self._tool is None: if self._toolid: self._tool = getToolByName(self.context, self._toolid) else: self._tool = False return self._tool @property def debug(self): ret = False if self.tool: ret = self.tool.getDebugMode() return ret def settings(self): """Return records from portal_registry corresponding to IJQueryUIPlugins """ registry = component.queryUtility(IRegistry) if registry is None or self._key is None: return return registry.forInterface(self._key, None) def __call__(self): settings = self.settings if settings is None: return resources = self.get_resources() if self._mimetype: self.request.response.setHeader('Content-Type', self._mimetype) if resources is None: resources = "" return self.get_resources_content(resources) def pack(self, content): if self._packer and not self.debug: return self._packer('safe').pack(content) return content def get_resources_content(self, resources): """Resources must be a list of resource ids. This method return the content of each resources appended in one string """ data = StringIO() if resources is None: resources = self.get_resources() upath = self.context.absolute_url_path() for resourceid in resources: try: resource = self.context.restrictedTraverse(resourceid) except KeyError, e: logger.error(e) continue if not resource: continue if self._header_template: data.write(self._header_template % (resource.context.__name__)) data.write(u"\n") # read the content of the resource fic = open(resource.context.path, 'r') content = fic.read() fic.close() try: content = unicode(content) except Exception, e: content = unicode(content.decode('utf-8')) #css applyPrefix if self._css: upath = upath.rstrip('/') prefix = '/'.join([upath, resourceid]) if prefix.endswith('/'): prefix = prefix[:-1] prefix = '/'.join(prefix.split('/')[:-1]) content = applyPrefix(content, prefix) #content is already minified data.write(content) data.write(u"\n")