[Zope-dev] DTML Block Parsing - Patch #2

2001-01-24 Thread Casey Duncan

I am sufficiently happy with my second patch to post it here for any
takers. It should be applied to 2.3.0b2 or 2.3.0b1. It *has not* been
tested against 2.2.5.

The new patch supports nested comments so long as they are properly
balanced inside the comment. 

As with my first patch it also allows you to create a custom DTML block
tag that contains something other than valid DTML code. It now should
support block continuation tages as well, although this had not been
tested completely. 

Patch files are attached. Enjoy.
-- 
| Casey Duncan
| Kaivo, Inc.
| [EMAIL PROTECTED]
`-->

*** DT_String.py~   Tue Dec 12 14:20:25 2000
--- DT_String.pyWed Jan 24 08:22:11 2001
***
*** 272,313 
  sname=stag
  sstart=start
  sa=sargs
! while 1:
  
  l=tagre.search(text,start)
  if l < 0: self.parse_error('No closing tag', stag, text, sloc)
  
  try: tag, args, command, coname= self._parseTag(tagre,scommand,sa)
! except ParseError, m: self.parse_error(m[0],m[1], text, l)
! 
! if command:
! start=l+len(tag)
! if hasattr(command, 'blockContinuations'):
! # New open tag.  Need to find closing tag.
! start=self.parse_close(text, start, tagre, tag, l,
!command, args)
  else:
! # Either a continuation tag or an end tag
! section=self.SubTemplate(sname)
! section._v_blocks=section.blocks=self.parse(text[:l],sstart)
! section._v_cooked=None
! blocks.append((tname,sargs,section))
  
! start=self.skip_eol(text,l+len(tag))
  
! if coname:
! tname=coname
! sname=tag
! sargs=args
! sstart=start
! else:
! try:
! r=scommand(blocks)
! if hasattr(r,'simple_form'): r=r.simple_form
! result.append(r)
! except ParseError, m: self.parse_error(m[0],stag,text,l)
  
! return start
  
  parse_close__roles__=()
  def parse_close(self, text, start, tagre, stag, sloc, scommand, sa):
--- 272,324 
  sname=stag
  sstart=start
  sa=sargs
! # Determine if the tag wants dtml parsing
! parse_dtml = not hasattr(scommand, 'disable_dtml_block_parsing')
  
+ while 1:
  l=tagre.search(text,start)
  if l < 0: self.parse_error('No closing tag', stag, text, sloc)
  
  try: tag, args, command, coname= self._parseTag(tagre,scommand,sa)
! except ParseError, m: 
! if parse_dtml: self.parse_error(m[0],m[1], text, l)
! else: start = l + 1 # Not parsing DTML: Skip malformed tags
  else:
! if command:
! start=l+len(tag)
! if hasattr(command, 'blockContinuations'):
! # New open tag.  Need to find closing tag.
! try: start=self.parse_close(text, start, tagre, tag, l,
! command, args)
!   except: 
! if parse_dtml: raise
! else:
! # Either a continuation tag or an end tag 
! if parse_dtml:
! section=self.SubTemplate(sname)
! section._v_blocks=section.blocks=self.parse(text[:l],sstart)
! else:
! section = String(text[sstart:l], __name__=sname)
! section._v_blocks = section.blocks = [section.raw]
! 
! section._v_cooked=None
! blocks.append((tname,sargs,section))
  
! start=self.skip_eol(text,l+len(tag))
  
! if coname:
! tname=coname
! sname=tag
! sargs=args
! sstart=start
! else:
! try:
! r=scommand(blocks)
! if hasattr(r,'simple_form'): r=r.simple_form
! result.append(r)
! except ParseError, m: self.parse_error(m[0],stag,text,l)
  
! return start
  
  parse_close__roles__=()
  def parse_close(self, text, start, tagre, stag, sloc, scommand, sa):


*** DT_Var.old  Mon Jan 22 15:27:30 2001
--- DT_Var.py   Mon Jan 22 15:28:14 2001
***
*** 461,466 
--- 461,467 
  ''' 
  name='comment'
  blockContinuations=()
+ disable_dtml_block_parsing = 1
  
  def __init__(s

[Zope-dev] DTML block parsing - Patch

2001-01-22 Thread Casey Duncan

Ok here goes:

Install these patches on a 2.3.0b1 installation. The files to patch are
found in {Zope Dir}/lib/python/DocumentTemplate.

Thus far it seems stable, and I would like for anyone willing to test it
out.

WHY SHOULD YOU?
Although the original purpose of this patch is solve my own problem, it
also solves a behavioral bug in . That is you no longer
need correctly formed and balanced dtml code inside of a
 pair. This allows them to function as you
would expect. Code inside the comment tags is simply ignored.

If you develop your own dtml tag class that you want to use to create
blocks of something other than dtml, simply add the class attribute
disable_dtml_block_parsing=1 along with blockContinuations=() in the
tag's class definition.

Here are the patches:

*** DT_String.old   Mon Jan 22 13:06:33 2001
--- DT_String.pyMon Jan 22 15:55:57 2001
***
*** 272,277 
--- 272,308 
  sname=stag
  sstart=start
  sa=sargs
+  
+   if hasattr(scommand, 'blockContinuations') and\
+hasattr(scommand, 'disable_dtml_block_parsing'):
+ # Tag doesn't want the block parsed as dtml, so just find
the
+ # end tag and don't parse what's inside
+ 
+ while 1:
+ l=tagre.search(text,start)
+ if l < 0: self.parse_error('No closing tag', stag,
text, sloc)
+ 
+ try: 
+ tag, args, command, coname=
self._parseTag(tagre,scommand,sa)
+ except: 
+ start = l + 1 # Skip malformed tags without
raising exceptions
+ else:
+ start = l + len(tag)
+ 
+ if not command and not coname:
+ # Block end tag
+ section = String(text[sstart:l],
__name__=sname)
+ section._v_blocks = section.blocks =
[section.raw]
+ section._v_cooked = None
+ blocks.append((tname, sargs, section))
+ try:
+ r=scommand(blocks)
+ if hasattr(r,'simple_form'):
r=r.simple_form
+ result.append(r)
+ except ParseError, m:
self.parse_error(m[0],stag,text,l)
+ 
+ return start
+ 
  while 1:
  
  l=tagre.search(text,start)


*** DT_Var.old  Mon Jan 22 15:27:30 2001
--- DT_Var.py   Mon Jan 22 15:28:14 2001
***
*** 461,466 
--- 461,467 
  ''' 
  name='comment'
  blockContinuations=()
+ disable_dtml_block_parsing = 1
  
  def __init__(self, args, fmt=''): pass


Feedback on this is definitely welcome. If it is generally well-liked, I
will submit it to the collector.
-- 
| Casey Duncan
| Kaivo, Inc.
| [EMAIL PROTECTED]
`-->

___
Zope-Dev maillist  -  [EMAIL PROTECTED]
http://lists.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 http://lists.zope.org/mailman/listinfo/zope-announce
 http://lists.zope.org/mailman/listinfo/zope )