[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

Re: [Zope-dev] DTML block parsing

2001-01-23 Thread Steve Alexander

Casey Duncan wrote:

> 
> OK, I have developed a new patch that almost completely fixes this
> issue. In fact I am happier with it in general than my first patch.
> There is only one flaw, although you can nest comments inside of one
> another, and you can have any manner of broken dtml inside, if you open
> another comment tag inside it, it must be properly balanced or you will
> get a parse error. Here are some examples:



> I am a bit concerned that  should ever generate errors at
> all, but this is certainly an improvement. Anybody have any thoughts?

I'm very happy with the way you describe the patched tag works. I'll try 
it out when I return from the EuroZope meeting in Amsterdam.

I don't think it is reasonable to try to catch unbalanced dtml-comment 
tags. In fact, I like the fact that it will raise an error if I leave 
them unbalanced.

Thanks.

--
Steve Alexander
Software Engineer
Cat-Box limited
http://www.cat-box.net


___
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 )




Re: [Zope-dev] DTML block parsing

2001-01-23 Thread Casey Duncan

Casey Duncan wrote:
> 
> Steve Alexander wrote:
> > With your patch applied, will nested dtml-comment tags still work?
> >
> > 
> >Some code commented out
> >  
> >Documentation in a comment
> >  
> >Rest of code commented out
> > 
> >
> > --
> > Steve Alexander
> > Software Engineer
> > Cat-Box limited
> > http://www.cat-box.net
> 
> In present form no. It also does not support block continuation tags.
> 
> I will see what I can do about this.

OK, I have developed a new patch that almost completely fixes this
issue. In fact I am happier with it in general than my first patch.
There is only one flaw, although you can nest comments inside of one
another, and you can have any manner of broken dtml inside, if you open
another comment tag inside it, it must be properly balanced or you will
get a parse error. Here are some examples:

These do not generate errors with my new patch:














Blah Blah



These do:












I am a bit concerned that  should ever generate errors at
all, but this is certainly an improvement. Anybody have any thoughts?
-- 
| 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 )




Re: [Zope-dev] DTML block parsing

2001-01-23 Thread Casey Duncan

Steve Alexander wrote:
> With your patch applied, will nested dtml-comment tags still work?
> 
> 
>Some code commented out
>  
>Documentation in a comment
>  
>Rest of code commented out
> 
> 
> --
> Steve Alexander
> Software Engineer
> Cat-Box limited
> http://www.cat-box.net

In present form no. It also does not support block continuation tags.

I will see what I can do about this.
-- 
| 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 )




Re: [Zope-dev] DTML block parsing

2001-01-23 Thread Steve Alexander

Casey Duncan wrote:

> 
> If not, I will make a patch for DT_String to support this. The
> beneficial side affect would be that  could be "fixed" so
> that its contents need not be parsable anymore, just like every other
> language I know of.

With your patch applied, will nested dtml-comment tags still work?


   Some code commented out
 
   Documentation in a comment
 
   Rest of code commented out


--
Steve Alexander
Software Engineer
Cat-Box limited
http://www.cat-box.net



___
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 )




[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 )




Re: [Zope-dev] DTML block parsing

2001-01-22 Thread ender

On Monday 22 January 2001 08:54, Casey Duncan wrote:
> I am working on a custom DTML tag (that I may unleash on the world when
> finished) that has brought to my attention an interesting feature of the
> DTML parser:
>
> It preparses everything in the block(s) between the start and end tags
> before the document containing the code is committed.
>
> My tag is a little different in that I don't want to put DTML inside it,
> therefore this parsing is undesirable. There is however one built-in tag
> that this is true for as well: . Many a thread has been
> burned about that issue.


i've been bitten by this 'feature' as well. i hacked out a bad solution
where i checked the length of the block, and raised an error if  it was
larger than one. but this didn't solve the problem that the nested dtml
was still evaluated. which troubles me since i think it could be a security
problem in the context of my tag.

> I propose a change to DT_String to allow a tag to turn off this
> preparsing feature. It should be easy enough to implement by adding a
> class attribute to the tag class such as disable_dtml_block_parsing = 1.
> Is this abhorrent to anyone?

sounds good to me.

> If not, I will make a patch for DT_String to support this. The
> beneficial side affect would be that  could be "fixed" so
> that its contents need not be parsable anymore, just like every other
> language I know of.

cool.

Kapil Thangavelu

___
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 )




[Zope-dev] DTML block parsing

2001-01-22 Thread Casey Duncan

I am working on a custom DTML tag (that I may unleash on the world when
finished) that has brought to my attention an interesting feature of the
DTML parser:

It preparses everything in the block(s) between the start and end tags
before the document containing the code is committed.

My tag is a little different in that I don't want to put DTML inside it,
therefore this parsing is undesirable. There is however one built-in tag
that this is true for as well: . Many a thread has been
burned about that issue.

I propose a change to DT_String to allow a tag to turn off this
preparsing feature. It should be easy enough to implement by adding a
class attribute to the tag class such as disable_dtml_block_parsing = 1.
Is this abhorrent to anyone? 

If not, I will make a patch for DT_String to support this. The
beneficial side affect would be that  could be "fixed" so
that its contents need not be parsable anymore, just like every other
language I know of.
-- 
| 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 )