[Checkins] SVN: z3c.cssresource/ Initial implementation of a CSS File Resource that supports some very

Stephan Richter srichter at cosmos.phy.tufts.edu
Mon Aug 14 10:22:22 EDT 2006


Log message for revision 69473:
  Initial implementation of a CSS File Resource that supports some very 
  simple string replacement for file paths and other things.
  
  I would not consider the API too stable, but we are going to use this, 
  so we will gain some experience soon.
  
  

Changed:
  A   z3c.cssresource/
  A   z3c.cssresource/branches/
  A   z3c.cssresource/tags/
  A   z3c.cssresource/trunk/
  A   z3c.cssresource/trunk/src/
  A   z3c.cssresource/trunk/src/z3c/
  A   z3c.cssresource/trunk/src/z3c/__init__.py
  A   z3c.cssresource/trunk/src/z3c/cssresource/
  A   z3c.cssresource/trunk/src/z3c/cssresource/README.txt
  A   z3c.cssresource/trunk/src/z3c/cssresource/__init__.py
  A   z3c.cssresource/trunk/src/z3c/cssresource/cssresource.py
  A   z3c.cssresource/trunk/src/z3c/cssresource/tests.py

-=-
Added: z3c.cssresource/trunk/src/z3c/__init__.py
===================================================================
--- z3c.cssresource/trunk/src/z3c/__init__.py	2006-08-14 14:01:36 UTC (rev 69472)
+++ z3c.cssresource/trunk/src/z3c/__init__.py	2006-08-14 14:22:22 UTC (rev 69473)
@@ -0,0 +1 @@
+# Make a package


Property changes on: z3c.cssresource/trunk/src/z3c/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.cssresource/trunk/src/z3c/cssresource/README.txt
===================================================================
--- z3c.cssresource/trunk/src/z3c/cssresource/README.txt	2006-08-14 14:01:36 UTC (rev 69472)
+++ z3c.cssresource/trunk/src/z3c/cssresource/README.txt	2006-08-14 14:22:22 UTC (rev 69473)
@@ -0,0 +1,110 @@
+==================
+CSS File Resources
+==================
+
+One of the design goals of Zope is to allow designers to check in HTML
+template, CSS and Javascript files, which just work (with some additional
+information). For templates we use Zope's Page Templates to accomplish this
+objective. For CSS and Javascript we did not need such a feature, since
+nothing should be dynamic, due to chaching.
+
+However, now URLs -- for example for background images -- are frequently
+inserted into CSS files. However, the path layout for the designer might not
+equal that to the resource file structure. This package provides a simple
+mechanism to replace strings by another.
+
+To accomplish this task, a custom CSS file resource is provided. The replace
+syntax is provided inside CSS comments. The commands are as follows:
+
+- ``/* zope-global-replace: "ORIGINAL" "FINAL" */``
+
+(Yes, for now it is only one.)
+
+To demonstrate this feature, we first have to create a CSS file.
+
+  >>> import tempfile
+  >>> fn = tempfile.mktemp('.css')
+  >>> open(fn, 'w').write('''\
+  ... /* zope-global-replace: "../img1" "++resource++/img" */
+  ... h1 {
+  ...   color: red;
+  ...   background: url('../img1/mybackground.gif');
+  ... }
+  ...
+  ... h2 {
+  ...   color: red;
+  ...   background: url('../img2/mybackground.gif');
+  ... }
+  ... /* zope-global-replace: "../img2" "++resource++/img" */
+  ... ''')
+
+As you can see, the command can be placed anywhere.
+
+Now we create a CSS resource from the resource factory:
+
+  >>> from z3c.cssresource import CSSFileResourceFactory
+  >>> cssFactory = CSSFileResourceFactory(fn, None, 'site.css')
+
+  >>> from zope.publisher.browser import TestRequest
+  >>> css = cssFactory(TestRequest())
+
+  >>> print css.GET()
+  h1 {
+    color: red;
+    background: url('++resource++/img/mybackground.gif');
+  }
+  <BLANKLINE>
+  h2 {
+    color: red;
+    background: url('++resource++/img/mybackground.gif');
+  }
+  <BLANKLINE>
+
+And that's all! In your ZCML you can use this factory as follows::
+
+  <resource
+      name="site.css"
+      path="css/site.css"
+      factory="z3c.cssresource.CSSFileResourceFactory"
+      />
+
+
+Global Replace
+--------------
+
+As seen in the example above, ``zope-global-replace`` calls can be placed
+anywhere in the file. Let's make sure that some special cases work as well:
+
+  >>> from z3c.cssresource import CSSFileResource
+  >>> resource = CSSFileResource(None, None)
+
+  >>> print resource.process('''\
+  ...        /* zope-global-replace: "foo" "bar" */
+  ... foo''')
+  bar
+
+  >>> print resource.process('''\
+  ... /*      zope-global-replace: "foo" "bar"      */
+  ... foo''')
+  bar
+
+  >>> print resource.process('''\
+  ... /* zope-global-replace:   "foo"         "bar" */
+  ... foo''')
+  bar
+
+But the following does not work:
+
+  >>> print resource.process('''\
+  ... /* zope-global-replace : "foo" "bar" */
+  ... foo''')
+  /* zope-global-replace : "foo" "bar" */
+  foo
+
+  >>> print resource.process('''\
+  ... /* zope -global-replace : "foo" "bar" */
+  ... foo''')
+  /* zope -global-replace : "foo" "bar" */
+  foo
+
+


Property changes on: z3c.cssresource/trunk/src/z3c/cssresource/README.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.cssresource/trunk/src/z3c/cssresource/__init__.py
===================================================================
--- z3c.cssresource/trunk/src/z3c/cssresource/__init__.py	2006-08-14 14:01:36 UTC (rev 69472)
+++ z3c.cssresource/trunk/src/z3c/cssresource/__init__.py	2006-08-14 14:22:22 UTC (rev 69473)
@@ -0,0 +1,2 @@
+# Make a package
+from cssresource import CSSFileResource, CSSFileResourceFactory


Property changes on: z3c.cssresource/trunk/src/z3c/cssresource/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.cssresource/trunk/src/z3c/cssresource/cssresource.py
===================================================================
--- z3c.cssresource/trunk/src/z3c/cssresource/cssresource.py	2006-08-14 14:01:36 UTC (rev 69472)
+++ z3c.cssresource/trunk/src/z3c/cssresource/cssresource.py	2006-08-14 14:22:22 UTC (rev 69473)
@@ -0,0 +1,57 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""CSS Resource
+
+$Id$
+"""
+import re
+
+from zope.app.publisher.fileresource import File
+from zope.app.publisher.browser.fileresource import FileResource
+
+# Allows matches of the form: /* ZOPE-COMMAND: "ORIGINAL" "FINAL" */
+directive_regex = r' */\* *%s: *"([^ "]*)" *"([^ "]*)" *\*/'
+global_replace = re.compile(directive_regex %'zope-global-replace')
+
+class CSSFileResource(FileResource):
+
+    def process(self, data):
+        # Find all directives
+        directives = re.compile(
+            directive_regex %'zope-global-replace').findall(data)
+        # Remove directives from file
+        data = re.compile(
+            (directive_regex+'\n') %'zope-global-replace').sub('', data)
+        # Now execute directives
+        for orig, final in directives:
+            data = data.replace(orig, final)
+        return data
+
+    def GET(self):
+        data = super(CSSFileResource, self).GET()
+        return self.process(data)
+
+
+class CSSFileResourceFactory(object):
+
+    def __init__(self, path, checker, name):
+        self.__file = File(path, name)
+        self.__checker = checker
+        self.__name = name
+
+    def __call__(self, request):
+        resource = CSSFileResource(self.__file, request)
+        resource.__Security_checker__ = self.__checker
+        resource.__name__ = self.__name
+        return resource


Property changes on: z3c.cssresource/trunk/src/z3c/cssresource/cssresource.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.cssresource/trunk/src/z3c/cssresource/tests.py
===================================================================
--- z3c.cssresource/trunk/src/z3c/cssresource/tests.py	2006-08-14 14:01:36 UTC (rev 69472)
+++ z3c.cssresource/trunk/src/z3c/cssresource/tests.py	2006-08-14 14:22:22 UTC (rev 69473)
@@ -0,0 +1,33 @@
+##############################################################################
+#
+# Copyright (c) 2006 Lovely Systems and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Tag test setup
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import doctest
+import unittest
+from zope.testing.doctestunit import DocFileSuite
+
+def test_suite():
+
+    return unittest.TestSuite((
+        DocFileSuite('README.txt',
+                     optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
+                     ),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: z3c.cssresource/trunk/src/z3c/cssresource/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id



More information about the Checkins mailing list