[Zope] [PATCH] <dtml-comment> doesn't? Please fix... ;-)

Kip Rugger kbr@pangea.ca
7 Sep 2000 22:53:27 -0500


Andrew Kenneth Milton  <akm@mail.theinternet.com.au> wrote:
>+-------[ Kip Rugger ]----------------------
>|
>| [snip]
>| 
>| <dtml-if "0">
>| crap
>| more crap
>| <dtml-if "0">
>| Lots of crap
>| </dtml-if>
>| </dtml-if>
>| 
>| by analogy with the common practice of #if 0 in C
>
>Won't work, the container will still be parsed for correctness at 'save' time.
>The problem isn't getting nested comments per se, we already have that. 
>The problem is to completely ignore the contents of the comment tag whilst
>still allowing nested comments.
>
>Th second part of the problem is that ChrisW(hinger) really wants it, but,
>doesn't want to do what is necessary to do it. So the problem is simplified
>to not actually doing anything except talking about doing it. d8)
>
>Thankyou for playing d8)


Since I put my stupid foot into it, I just had to write the patch.
For penitence and to learn about DocumentTemplate.

This should let you nest <dtml-comment> arb stuff </dtml-comment>
without any syntactical value being ascribed to the contents.

I am too new to Zope to know if this is even desirable :-) so I
just offer it as more of an amusement than anything else.

Patch against 2.2.1 source.

--- DT_String.py.orig	Thu Aug 17 08:46:48 2000
+++ DT_String.py	Thu Sep  7 22:27:42 2000
@@ -237,7 +237,11 @@
             if s: result.append(s)
             start=l+len(tag)
 
-            if hasattr(command,'blockContinuations'):
+            if command == Comment:
+                start = self.parse_comment(text, start, result, tagre, tag, l,
+                                           command, args)
+
+            elif hasattr(command,'blockContinuations'):
                 start=self.parse_block(text, start, result, tagre,
                                        tag, l, args, command)
             else:
@@ -280,7 +284,11 @@
             try: tag, args, command, coname= self._parseTag(tagre,scommand,sa)
             except ParseError, m: self.parse_error(m[0],m[1], text, l)
             
-            if command:
+            if command == Comment:
+                start=l+len(tag)
+                start = self.parse_comment(text, start, result, tagre, tag, l,
+                                           command, args)
+            elif command:
                 start=l+len(tag)
                 if hasattr(command, 'blockContinuations'):
                     # New open tag.  Need to find closing tag.
@@ -325,6 +333,35 @@
                     start=self.parse_close(text, start, tagre, tag, l,
                                            command,args)
             elif not coname: return start
+
+    def parse_comment(self, text, start, result, tagre,
+                    stag, sloc, sargs, scommand):
+        nest = 1
+        while 1:
+
+            l=tagre.search(text,start)
+            if l < 0: self.parse_error('No closing tag', stag, text, sloc)
+
+            tag, name = tagre.group(0, 'name')
+            start = l + len(tag)
+            if name == 'comment':
+                try:
+                    end = tagre.group('end')
+                except:
+                    try:
+                        end = tagre.group('fmt')
+                        if end != ']': end = ''
+                    except:
+                        self.parse_error('Delimiter problems',
+                                         stag, text, sloc)
+                if end == '':
+                    nest = nest + 1
+                else:
+                    nest = nest - 1
+
+            if nest == 0:
+                start=self.skip_eol(text, start)
+                return start
 
     shared_globals__roles__=()
     shared_globals={}