[Checkins] SVN: zc.resourcelibrary/trunk/src/zc/resourcelibrary/publication.py act on HTML and XML content only!

Adam Groszer adamg at fw.hu
Fri Jun 2 05:23:36 EDT 2006


Log message for revision 68454:
  act on HTML and XML content only!

Changed:
  U   zc.resourcelibrary/trunk/src/zc/resourcelibrary/publication.py

-=-
Modified: zc.resourcelibrary/trunk/src/zc/resourcelibrary/publication.py
===================================================================
--- zc.resourcelibrary/trunk/src/zc/resourcelibrary/publication.py	2006-06-02 07:15:20 UTC (rev 68453)
+++ zc.resourcelibrary/trunk/src/zc/resourcelibrary/publication.py	2006-06-02 09:23:35 UTC (rev 68454)
@@ -17,6 +17,7 @@
 from zope import interface
 from zope.app.publication.interfaces import IBrowserRequestFactory
 from zope.publisher.browser import BrowserRequest, BrowserResponse
+from zope.publisher.browser import isHTML
 import zc.resourcelibrary.zcml
 
 class Request(BrowserRequest):
@@ -32,51 +33,60 @@
 class Response(BrowserResponse):
 
     def _implicitResult(self, body):
-
-        # add any libraries that the explicitly referenced libraries require
-        libs = list(self.resource_libraries)
-        while libs:
-            lib = libs.pop()
-            try:
-                required = zc.resourcelibrary.getRequired(lib)
-            except KeyError:
-                raise RuntimeError('Unknown resource library: "%s"' % lib)
-            for lib in required:
-                if lib not in self.resource_libraries:
-                    self.resource_libraries.append(lib)
-                    libs.append(lib)
-
-        # reverse the order of the libs in order to have the
-        # dependencies first. TODO: this does not work if the
-        # dependency is needed directly in the page before the
-        # dependent lib is needed.
-        self.resource_libraries.reverse()
         
-        # generate the HTML that will be included in the response
-        html = []
-        for lib in self.resource_libraries:
-            included = zc.resourcelibrary.getIncluded(lib)
-            for file_name in included:
-                if file_name.endswith('.js'):
-                    html.append('<script src="/@@/%s/%s" '
-                                'language="Javascript1.1"' % (lib, file_name))
-                    html.append('    type="text/javascript">')
-                    html.append('</script>')
-                elif file_name.endswith('.css'):
-                    html.append('<style type="text/css" media="all">')
-                    html.append('  <!--')
-                    html.append('    @import url("/@@/%s/%s");'
-                                % (lib, file_name))
-                    html.append('  -->')
-                    html.append('</style>')
-                else:
-                    # shouldn't get here; zcml.py is supposed to check includes
-                    raise RuntimeError('Resource library doesn\'t know how to '
-                                       'include this file: "%s"' % file_name)
+        #figure out the content type
+        content_type = self.getHeader('content-type')
+        if content_type is None:
+            if isHTML(body):
+                content_type = 'text/html'
+        
+        if content_type == 'text/html' or content_type == 'text/xml':
+            #act on HTML and XML content only!
+            
+            # add any libraries that the explicitly referenced libraries require
+            libs = list(self.resource_libraries)
+            while libs:
+                lib = libs.pop()
+                try:
+                    required = zc.resourcelibrary.getRequired(lib)
+                except KeyError:
+                    raise RuntimeError('Unknown resource library: "%s"' % lib)
+                for lib in required:
+                    if lib not in self.resource_libraries:
+                        self.resource_libraries.append(lib)
+                        libs.append(lib)
+    
+            # reverse the order of the libs in order to have the
+            # dependencies first. TODO: this does not work if the
+            # dependency is needed directly in the page before the
+            # dependent lib is needed.
+            self.resource_libraries.reverse()
+            
+            # generate the HTML that will be included in the response
+            html = []
+            for lib in self.resource_libraries:
+                included = zc.resourcelibrary.getIncluded(lib)
+                for file_name in included:
+                    if file_name.endswith('.js'):
+                        html.append('<script src="/@@/%s/%s" '
+                                    'language="Javascript1.1"' % (lib, file_name))
+                        html.append('    type="text/javascript">')
+                        html.append('</script>')
+                    elif file_name.endswith('.css'):
+                        html.append('<style type="text/css" media="all">')
+                        html.append('  <!--')
+                        html.append('    @import url("/@@/%s/%s");'
+                                    % (lib, file_name))
+                        html.append('  -->')
+                        html.append('</style>')
+                    else:
+                        # shouldn't get here; zcml.py is supposed to check includes
+                        raise RuntimeError('Resource library doesn\'t know how to '
+                                           'include this file: "%s"' % file_name)
+    
+            if html:
+                body = body.replace('<head>', '<head>\n    %s\n' %
+                                    '\n    '.join(html))
 
-        if html:
-            body = body.replace('<head>', '<head>\n    %s\n' %
-                                '\n    '.join(html))
 
-
         return super(Response, self)._implicitResult(body)



More information about the Checkins mailing list