{ "info": { "author": "Zope Corporation and Contributors", "author_email": "zope-dev@zope.org", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Environment :: Web Environment", "Framework :: Zope3", "Intended Audience :: Developers", "License :: OSI Approved :: Zope Public License", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python", "Topic :: Internet :: WWW/HTTP" ], "description": "This package provides a very simple templating system for non-SGML data files\nsuch as CSS and Javascript.\n\n\n.. contents::\n\n===================\nTemplated Resources\n===================\n\nOne of the design goals of Zope is to allow designers to check in HTML\ntemplate, CSS and Javascript files, which just work (with some additional\ninformation). For HTML code we use Zope's Page Templates to accomplish this\nobjective. For CSS and Javascript we did not need such a feature until now,\nsince those files were largely static or variables could be inserted using\nother ways at runtime.\n\nHowever, in CSS URLs -- for example for background images -- are now\nfrequently inserted into CSS directives. However, the path layout for the\ndesigner might not equal the resource file structure. This package provides a\nsimple mechanism to replace strings by another.\n\nTo accomplish this, a templated resource is provided. The template syntax is\nprovided in a way that it does not interfere with the syntax of the\nresource. For both, Javascript and CSS, this is a comment of the form ``/*\n... */``.\n\nHere is the general syntax::\n\n : \n\nHere is an example for CSS::\n\n /* zrt-replace: \"..\" \"@@\" */\n\nTo demonstrate this feature, we first have to create a CSS file.\n\n >>> import tempfile\n >>> fn = tempfile.mktemp('.css')\n >>> open(fn, 'w').write('''\\\n ... /* zrt-replace: \"../img1\" \"++resource++/img\" */\n ... /* zrt-replace: \"fontName\" \"Arial, Tahoma\" */\n ... h1 {\n ... color: red;\n ... font: fontName;\n ... background: url('../img1/mybackground.gif');\n ... }\n ...\n ... h2 {\n ... color: red;\n ... background: url('../img2/mybackground.gif');\n ... }\n ... /* zrt-replace: \"../img2\" \"++resource++/img\" */\n ... ''')\n\nThe global replace command replaces a string with another. It is only active\nin the lines *after* it was declared. Thus, in this case, the second command\nis meaningless.\n\nNow we create a ZRT resource from the resource factory ...\n\n >>> from z3c.zrtresource import ZRTFileResourceFactory\n >>> cssFactory = ZRTFileResourceFactory(fn, None, 'site.css')\n\n >>> from zope.publisher.browser import TestRequest\n >>> css = cssFactory(TestRequest())\n\nand render the resource:\n\n >>> print css.GET()\n h1 {\n color: red;\n font: Arial, Tahoma;\n background: url('++resource++/img/mybackground.gif');\n }\n \n h2 {\n color: red;\n background: url('../img2/mybackground.gif');\n }\n\nAs you can see only the first URL was replaced, because of the incorrect\nposition of the second statement.\n\nAnd that's all! In your ZCML you can use this factory as follows::\n\n \n\n\nReplacing Strings\n-----------------\n\nThe ``zrt-replace`` command replaces any matches with the output string as\nmany times as specified. Here is the syntax:\n\n zrt-replace: \"\" \"\" \n\nAs seen in the example above, ``zrt-replace`` calls can be placed\nanywhere in the file. Let's make sure that some special cases work as well:\n\n >>> from z3c.zrtresource import processor, replace\n >>> def process(text):\n ... p = processor.ZRTProcessor(\n ... text, commands={'replace': replace.Replace})\n ... return p.process(None, None)\n\n >>> print process('''\\\n ... /* zrt-replace: \"foo\" \"bar\" */\n ... foo''')\n bar\n\n >>> print process('''\\\n ... /* zrt-replace: \"foo\" \"bar\" */\n ... foo''')\n bar\n\n >>> print process('''\\\n ... /* zrt-replace: \"foo\" \"bar\" */\n ... foo''')\n bar\n\nBut the following does not work:\n\n >>> print process('''\\\n ... /* zrt-replace : \"foo\" \"bar\" */\n ... foo''')\n /* zrt-replace : \"foo\" \"bar\" */\n foo\n\n >>> print process('''\\\n ... /* zrt -replace : \"foo\" \"bar\" */\n ... foo''')\n /* zrt -replace : \"foo\" \"bar\" */\n foo\n\nUntil now we have only considered multiple replacements. Let's now restrict\nthe number of replacements with the final argument. Initially all occurences\nof a matching string are replaced:\n\n >>> print process('''\\\n ... /* zrt-replace: \"foo\" \"bar\" */\n ... foo foo foo foo foo''')\n bar bar bar bar bar\n\nWhen we specify a number of replacements, then only that amount is replaced:\n\n >>> print process('''\\\n ... /* zrt-replace: \"foo\" \"bar\" 1 */\n ... foo foo foo foo foo''')\n bar foo foo foo foo\n\n >>> print process('''\\\n ... /* zrt-replace: \"foo\" \"bar\" 3 */\n ... foo foo foo foo foo''')\n bar bar bar foo foo\n\n >>> print process('''\\\n ... /* zrt-replace: \"foo\" \"bar\" 6 */\n ... foo foo foo foo foo''')\n bar bar bar bar bar\n\n\nThe String Expression\n~~~~~~~~~~~~~~~~~~~~~\n\nUntil now we have only dealt with simple string replacement, since it is the\ndefault expression type. Another way of spelling the expression type is:\n\n >>> print process('''\\\n ... /* zrt-replace: str\"foo\" \"bar\" */\n ... foo''')\n bar\n\n >>> print process('''\\\n ... /* zrt-replace: \"foo\" str\"bar\" */\n ... foo''')\n bar\n\n >>> print process('''\\\n ... /* zrt-replace: str\"foo\" str\"bar\" */\n ... foo''')\n bar\n\n\nThe Regex Expression\n~~~~~~~~~~~~~~~~~~~~\n\nRegular expressions make only sense as input expressions, so they are only\nsupported there:\n\n >>> print process('''\\\n ... /* zrt-replace: re\"foo\" \"bar\" */\n ... foo''')\n bar\n\n >>> print process('''\\\n ... /* zrt-replace: re\"[a-z]*foo\" \"bar\" */\n ... myfoo''')\n bar\n\nWe also support groups:\n\n >>> print process('''\\\n ... /* zrt-replace: re\"([a-z]*)foo\" \"bar\" */\n ... myfoo''')\n bar\n\n >>> print process('''\\\n ... /* zrt-replace: re\"([a-z]*)foo\" \"bar\" */\n ... myfoo''')\n bar\n\n >>> print process('''\\\n ... /* zrt-replace: re\"([a-z]*)foo\" \"bar\" */\n ... myfoo mybar''')\n bar mybar\n\nYes, even group replacement works:\n\n >>> print process('''\\\n ... /* zrt-replace: re\"([a-z]*)foo\" \"bar\\\\1\" */\n ... myfoo a9foo''')\n barmy a9bar\n\n >>> print process('''\\\n ... /* zrt-replace: re\"(?P[a-z]*)foo\" \"bar\\\\g\" */\n ... myfoo a9foo''')\n barmy a9bar\n\n\nThe TALES Expression\n~~~~~~~~~~~~~~~~~~~~\n\nWhat would be a Zope-based templating language without TALES expressions? This\nis particularly useful, if you want to create absolute URLs and other dynamic\nbits based on the request and the context:\n\n >>> import zope.interface\n >>> from zope.traversing.interfaces import IContainmentRoot\n >>> class Root(object):\n ... zope.interface.implements(IContainmentRoot)\n\n >>> from zope.publisher.browser import TestRequest\n >>> def process(text):\n ... p = processor.ZRTProcessor(\n ... text, commands={'replace': replace.Replace})\n ... return p.process(Root(), TestRequest())\n\n >>> print process('''\\\n ... /* zrt-replace: \"foo\" tal\"string:${context/@@absolute_url}/@@/foo\" */\n ... foo''')\n http://127.0.0.1/@@/foo\n\n\nCustom ZRT Command\n~~~~~~~~~~~~~~~~~~\n\nWe can create custom ZRT commands. For this we should register\na named IZRTCommandFactory utility\n\n >>> import re\n >>> from zope import interface\n >>> from zope.component import provideUtility\n\n >>> from z3c.zrtresource import interfaces\n\n >>> class MyCustomCommand(object):\n ... interface.implements(interfaces.IZRTCommand)\n ...\n ... data = {'color1': 'red', 'color2': 'green'}\n ...\n ... isAvailable = True\n ...\n ... def __init__(self, args, start, end):\n ... self.args = args\n ... self.start = start\n ... self.end = end\n ...\n ... def process(self, text, context, request):\n ... for key, value in self.data.items():\n ... regex = re.compile(re.escape(key))\n ... text = regex.subn(value, text)[0]\n ...\n ... return text\n\n >>> from zope.component.factory import Factory\n >>> my_command = Factory(MyCustomCommand, 'mycommand')\n >>> interface.directlyProvides(my_command, interfaces.IZRTCommandFactory)\n\n >>> provideUtility(my_command, interfaces.IZRTCommandFactory, name='mycommand')\n\n >>> open(fn, 'w').write('''\\\n ... /* zrt-replace: \"../img1\" \"++resource++/img\" */\n ... /* zrt-replace: \"fontFamily\" \"Arial, Tahoma\" */\n ... /* zrt-mycommand: */\n ... /* oh, and we're testing that when the file changes, it is reloaded */\n ... h1 {\n ... color: color1;\n ... font: fontFamily;\n ... background: url('../img1/mybackground.gif');\n ... }\n ...\n ... h2 {\n ... color: color2;\n ... background: url('../img2/mybackground.gif');\n ... }\n ... /* zrt-replace: \"../img2\" \"++resource++/img\" */\n ... ''')\n\nWe have to recreate the ZRTFileResourceFactory to reload the changed file\ncontents (don't worry -- in real life Zope creates these anew for every\nrequest, since resources are actually registered as IResourceFactoryFactory\nutilities).\n\n >>> cssFactory = ZRTFileResourceFactory(fn, None, 'site.css')\n >>> css = cssFactory(TestRequest())\n\n >>> print css.GET()\n /* oh, and we're testing that when the file changes, it is reloaded */\n h1 {\n color: red;\n font: Arial, Tahoma;\n background: url('++resource++/img/mybackground.gif');\n }\n \n h2 {\n color: green;\n background: url('../img2/mybackground.gif');\n }\n \n\nEdge case\n---------\n\nOn windows blank lines were left in the result\n\n >>> open(fn, 'w').write('''\\\n ... some-head\n ... /* zrt-replace: \"../img1\" \"++resource++/img\" */\n ... /* zrt-replace: \"fontName\" \"Arial, Tahoma\" */\n ... some-in-the-middle\n ... /* zrt-replace: \"../img2\" \"++resource++/img\" */\n ... some-at-the-end\n ... ''')\n\n >>> cssFactory = ZRTFileResourceFactory(fn, None, 'site.css')\n\n >>> from zope.publisher.browser import TestRequest\n >>> css = cssFactory(TestRequest())\n\nand render the resource:\n\n >>> css.GET().splitlines()\n ['some-head', 'some-in-the-middle', 'some-at-the-end']\n\n\n==========================\n``zrt-resource`` Directive\n==========================\n\nThis package provides a new directive to use the special resource\ndirective. Let's register it first:\n\n >>> from zope.configuration import xmlconfig\n >>> context = xmlconfig.string('''\n ... \n ... \n ... \n ... ''')\n\nNow we can register a resource:\n\n >>> import tempfile\n >>> fn = tempfile.mktemp('.css')\n >>> open(fn, 'w').write('''\\\n ... /* zrt-replace: \"../img1\" \"++resource++/img\" */\n ... h1 {\n ... color: red;\n ... background: url('../img1/mybackground.gif');\n ... }\n ... ''')\n\n >>> context = xmlconfig.string('''\n ... \n ... \n ... \n ... ''' %fn, context=context)\n\nNow let's see whether the adapter has been registered:\n\n >>> import zope.component\n >>> import zope.interface\n >>> from zope.publisher.browser import TestRequest\n >>> resource = zope.component.getAdapter(\n ... TestRequest(), zope.interface.Interface, name='test.css')\n\nNow run it:\n\n >>> print resource.GET()\n h1 {\n color: red;\n background: url('++resource++/img/mybackground.gif');\n }\n\nWe can also register a ZRT resource using standard ``browser:resource`` directive.\nThe ``configure.zcml`` file in this package registers a ZRT resource factory for\nfiles with \"zrt\" extension, so any file with \"zrt\" extension will be a ZRT\nresource.\n\nFirst, let's include the ``browser:resource`` directive and the resource factory\nregistration:\n\n >>> context = xmlconfig.string('''\n ... \n ... \n ... \n ... \n ... \n ... ''')\n\nNow we need to create a file with \"zrt\" extension:\n\n >>> fn = tempfile.mktemp('.zrt')\n >>> open(fn, 'w').write('''\\\n ... /* zrt-replace: \"../img1\" \"++resource++/img\" */\n ... h1 {\n ... color: red;\n ... background: url('../img1/mybackground.gif');\n ... }\n ... ''')\n\nAnd register it as a resource using browser:resource directive.\n\n >>> context = xmlconfig.string('''\n ... \n ... \n ... \n ... ''' %fn, context=context)\n\nLet's see whether the adapter has been registered:\n\n >>> resource2 = zope.component.getAdapter(\n ... TestRequest(), zope.interface.Interface, name='test2.css')\n\nNow, let's render it and check if ZRT mechanism works for it.\n\n >>> print resource2.GET()\n h1 {\n color: red;\n background: url('++resource++/img/mybackground.gif');\n }\n \n\n=======\nCHANGES\n=======\n\n1.4.0 (2013-05-31)\n------------------\n\n- Made tests compatible with ``zope.browserresource`` 3.11, thus requiring\n at least this version.\n\n- Fix: work around windows: it has a 2 char next line sequence\n\n\n1.3.0 (2009-08-27)\n------------------\n\n- Use new ``zope.browserresource`` package instead of ``zope.app.publisher``,\n as the resources mechanism was moved there to reduce dependencies.\n\n- Register ZRTFileResourceFactory as a resource factory with name \"zrt\" in\n package's configure.zcml. This makes ZRT resources created automatically\n when using ``browser:resource`` directive for files with \"zrt\" extensions.\n It will also make \\*.zrt files inside resourse directories a ZRTFileResource.\n\n- Get rid of ``zope.app.testing`` test dependency.\n\n- Remove unused SETUP.CFG and z3c.zrtresource-meta.zcml files.\n\n\n1.2.0 (2009-06-25)\n------------------\n\n- Got rid of dependency on ``zope.app.component`` and\n ``zope.app.pagetemplate``.\n\n- Fixed home page and author email address.\n\n\n1.1.0 (2007-12-01)\n------------------\n\n- Fix bug with spaces in replace expression\n\n- Added custom ZRT commands\n\n1.0.1 (2007-10-30)\n------------------\n\n- Fix long description of package to be valid restructured text.\n\n1.0.0 (2007-10-30)\n------------------\n\n- Initial release.", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://pypi.python.org/pypi/z3c.zrtresource", "keywords": "zope3 css javascript resource zope", "license": "ZPL 2.1", "maintainer": null, "maintainer_email": null, "name": "z3c.zrtresource", "package_url": "https://pypi.org/project/z3c.zrtresource/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/z3c.zrtresource/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://pypi.python.org/pypi/z3c.zrtresource" }, "release_url": "https://pypi.org/project/z3c.zrtresource/1.4.0/", "requires_dist": null, "requires_python": null, "summary": "Zope Resource Templates", "version": "1.4.0" }, "last_serial": 802132, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "52aa77f6da8903ee67de88c564ad45a9", "sha256": "122a44d729d5fc9b59a4d5337e72a3dc5d99537a7db4710d9ccd914ebea7454a" }, "downloads": -1, "filename": "z3c.zrtresource-1.0.0.tar.gz", "has_sig": false, "md5_digest": "52aa77f6da8903ee67de88c564ad45a9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10937, "upload_time": "2007-10-30T21:25:06", "url": "https://files.pythonhosted.org/packages/2c/d8/a95a22f1d8a1de26fff3f2801337b76c1e3526856e3f9aadf629655d4b60/z3c.zrtresource-1.0.0.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "5ed9ae3fbd4b8ce946b342e2e54d4ec5", "sha256": "46f257f282d7c3d1edb90deafcc83f299e57f2d921b69f32fe4c0028066f9280" }, "downloads": -1, "filename": "z3c.zrtresource-1.0.1.tar.gz", "has_sig": false, "md5_digest": "5ed9ae3fbd4b8ce946b342e2e54d4ec5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10950, "upload_time": "2007-10-30T21:54:04", "url": "https://files.pythonhosted.org/packages/e3/81/acd41a680215dcf65c384d6820d2872bd9f7708c7e68c8d26a371d7ce415/z3c.zrtresource-1.0.1.tar.gz" } ], "1.1.0": [ { "comment_text": "", "digests": { "md5": "4e7db96100da1918c29889cb6d53490d", "sha256": "87db219fa530ce8eee65d35539c5f7c52de3eae58c14334fbe830c996b0d713c" }, "downloads": -1, "filename": "z3c.zrtresource-1.1.0.tar.gz", "has_sig": false, "md5_digest": "4e7db96100da1918c29889cb6d53490d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11867, "upload_time": "2008-02-01T19:49:47", "url": "https://files.pythonhosted.org/packages/c2/63/1af6b2b312ae7433b1e7430a79466f0d098dee7fb886394c3c4bf8387208/z3c.zrtresource-1.1.0.tar.gz" } ], "1.2.0": [ { "comment_text": "", "digests": { "md5": "d5448f2f4f1c5b331390ac6d944c452c", "sha256": "51ecdae1d67be5d8b1305411df8314b9fbf8c7a0fca19e486375334c8c78b87d" }, "downloads": -1, "filename": "z3c.zrtresource-1.2.0.tar.gz", "has_sig": false, "md5_digest": "d5448f2f4f1c5b331390ac6d944c452c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13438, "upload_time": "2009-06-25T19:08:57", "url": "https://files.pythonhosted.org/packages/08/ac/3cce890b60a5403cf55ca979d563b1f94353dd8f5ba93e6f8eb7d04bd376/z3c.zrtresource-1.2.0.tar.gz" } ], "1.3.0": [ { "comment_text": "", "digests": { "md5": "cc20cd7f6712bc00dcc5249ff6b30340", "sha256": "df011df53fca8355a258edd0d8f6d8ee3ba4d8b9b670adfd5d9a4531ef8d23bf" }, "downloads": -1, "filename": "z3c.zrtresource-1.3.0.tar.gz", "has_sig": false, "md5_digest": "cc20cd7f6712bc00dcc5249ff6b30340", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13144, "upload_time": "2009-08-27T18:22:44", "url": "https://files.pythonhosted.org/packages/6a/ff/14d58577134705614cc4fe28043eeb05661a86648b4c0cbd94e7a2d0d0d2/z3c.zrtresource-1.3.0.tar.gz" } ], "1.4.0": [ { "comment_text": "", "digests": { "md5": "ac91a0705235150e30b63d69bb1d0705", "sha256": "b950527bb51a475b30d1327c3e5fed1957b091a7bcc71501dd9aaf96402f9fad" }, "downloads": -1, "filename": "z3c.zrtresource-1.4.0.zip", "has_sig": false, "md5_digest": "ac91a0705235150e30b63d69bb1d0705", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 30652, "upload_time": "2013-05-31T09:08:25", "url": "https://files.pythonhosted.org/packages/58/4e/71abc6e3b262131165f67332f2ada7a92e8c91260634e393c7accb536dce/z3c.zrtresource-1.4.0.zip" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "ac91a0705235150e30b63d69bb1d0705", "sha256": "b950527bb51a475b30d1327c3e5fed1957b091a7bcc71501dd9aaf96402f9fad" }, "downloads": -1, "filename": "z3c.zrtresource-1.4.0.zip", "has_sig": false, "md5_digest": "ac91a0705235150e30b63d69bb1d0705", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 30652, "upload_time": "2013-05-31T09:08:25", "url": "https://files.pythonhosted.org/packages/58/4e/71abc6e3b262131165f67332f2ada7a92e8c91260634e393c7accb536dce/z3c.zrtresource-1.4.0.zip" } ] }