Skip to content

Commit

Permalink
Major redesign of block rendering. The code inside a block tag is
Browse files Browse the repository at this point in the history
compiled as a template but only the templates blocks are saved, and
later rendered directly with render_blocks.

Added with tag.

Also, for the HTML syntax, we now allow spaces after # and after end
or '/'.  So, the tags::

  <!--#
    with spam
    -->

and::

  <!--#
    end with
    -->

are valid.
  • Loading branch information
Jim Fulton committed Apr 2, 1998
1 parent 9f3825c commit 96f2d92
Show file tree
Hide file tree
Showing 15 changed files with 1,468 additions and 569 deletions.
36 changes: 33 additions & 3 deletions DT_Comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@
The 'comment' tag can be used to simply include comments
in DTML source.
''' # '
__rcs_id__='$Id: DT_Comment.py,v 1.1 1998/03/04 18:19:56 jim Exp $'
For example::
<!--#comment-->
This text is not rendered.
<!--#/comment-->
'''
__rcs_id__='$Id: DT_Comment.py,v 1.2 1998/04/02 17:37:34 jim Exp $'

############################################################################
# Copyright
Expand Down Expand Up @@ -61,7 +69,7 @@
# (540) 371-6909
#
############################################################################
__version__='$Revision: 1.1 $'[11:-2]
__version__='$Revision: 1.2 $'[11:-2]

from DT_Util import *

Expand All @@ -81,6 +89,28 @@ def render(self, md):

############################################################################
# $Log: DT_Comment.py,v $
# Revision 1.2 1998/04/02 17:37:34 jim
# Major redesign of block rendering. The code inside a block tag is
# compiled as a template but only the templates blocks are saved, and
# later rendered directly with render_blocks.
#
# Added with tag.
#
# Also, for the HTML syntax, we now allow spaces after # and after end
# or '/'. So, the tags::
#
# <!--#
# with spam
# -->
#
# and::
#
# <!--#
# end with
# -->
#
# are valid.
#
# Revision 1.1 1998/03/04 18:19:56 jim
# added comment and raise tags
#
Expand Down
4 changes: 2 additions & 2 deletions DT_Doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
is used to insert the variable 'total' with the C format
'12.2f'.
%(Expr)s
%(Var)s
Document templates support conditional and sequence insertion
Expand Down Expand Up @@ -98,8 +100,6 @@
object will have an attribute, AUTHENTICATED_USER that is the
user object that was found if and when Bobo authenticated a user.
%(Expr)s
Document Templates may be created 4 ways:
DocumentTemplate.String -- Creates a document templated from a
Expand Down
51 changes: 33 additions & 18 deletions DT_HTML.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

"""HTML formated DocumentTemplates
$Id: DT_HTML.py,v 1.5 1997/10/27 17:35:32 jim Exp $"""
$Id: DT_HTML.py,v 1.6 1998/04/02 17:37:35 jim Exp $"""

from DT_String import String, FileMixin
import DT_Doc, DT_String, regex
Expand All @@ -12,20 +12,21 @@
class dtml_re_class:

def search(self, text, start=0,
name_match=regex.compile('[a-zA-Z]+[\0- ]*').match):
name_match=regex.compile('[\0- ]*[a-zA-Z]+[\0- ]*').match,
end_match=regex.compile('[\0- ]*\(/\|end\)',
regex.casefold).match,
):
s=find(text,'<!--#',start)
if s < 0: return s
e=find(text,'-->',s)
if e < 0: return e

n=s+5
if text[n:n+1]=='/':
end=text[n:n+1]
n=n+1
elif text[n:n+3]=='end':
end=text[n:n+3]
n=n+3
else:
end=''
l=end_match(text,n)
if l > 0:
end=strip(text[n:n+l])
n=n+l
else: end=''

l=name_match(text,n)
if l < 0: return l
Expand Down Expand Up @@ -59,14 +60,6 @@ class HTML(DT_String.String):

def tagre(self):
return dtml_re_class()
return regex.symcomp(
'<!--#' # beginning
'\(<end>/\|end\)?' # end tag marker
'\(<name>[a-z]+\)' # tag name
'[\0- ]*' # space after tag name
'\(<args>\([^>"]+\("[^"]*"\)?\)*\)' # arguments
'-->' # end
, regex.casefold)

def parseTag(self, tagre, command=None, sargs=''):
"""Parse a tag using an already matched re
Expand Down Expand Up @@ -218,6 +211,28 @@ def manage_edit(self,data,
##########################################################################
#
# $Log: DT_HTML.py,v $
# Revision 1.6 1998/04/02 17:37:35 jim
# Major redesign of block rendering. The code inside a block tag is
# compiled as a template but only the templates blocks are saved, and
# later rendered directly with render_blocks.
#
# Added with tag.
#
# Also, for the HTML syntax, we now allow spaces after # and after end
# or '/'. So, the tags::
#
# <!--#
# with spam
# -->
#
# and::
#
# <!--#
# end with
# -->
#
# are valid.
#
# Revision 1.5 1997/10/27 17:35:32 jim
# Removed old validation machinery.
#
Expand Down
94 changes: 41 additions & 53 deletions DT_If.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@
# (540) 371-6909
#
############################################################################
__rcs_id__='$Id: DT_If.py,v 1.8 1998/01/14 18:23:42 jim Exp $'
__version__='$Revision: 1.8 $'[11:-2]
__rcs_id__='$Id: DT_If.py,v 1.9 1998/04/02 17:37:35 jim Exp $'
__version__='$Revision: 1.9 $'[11:-2]

from DT_Util import *
import sys
Expand All @@ -135,50 +135,35 @@ def __init__(self, blocks):
args=parse_params(args, name='', expr='')
name,expr=name_param(args,'if',1)
self.__name__= name
self.sections=[(name, expr, section)]
if expr is None: cond=name
else: cond=expr.eval
sections=[cond, section.blocks]

if blocks[-1][0]=='else':
tname, args, section = blocks[-1]
blocks=blocks[:-1]
del blocks[-1]
args=parse_params(args, name='')
if args:
ename,expr=name_param(args,'else',1)
if ename != name:
raise ParseError, ('name in else does not match if', 'in')
self.elses=section
elses=section.blocks
else: elses=None

for tname, args, section in blocks[1:]:
if tname=='else':
raise ParseError, (
'more than one else tag for a single if tag', 'in')
args=parse_params(args, name='', expr='')
name,expr=name_param(args,'elif',1)
self.sections.append((name, expr, section))

def render(self,md):
cache={}
md._push(cache)
try:
for name, expr, section in self.sections:
if expr is None:
try: v=md[name]
except KeyError, ev:
if ev is not name:
raise KeyError, name, sys.exc_traceback
v=None
cache[name]=v
else:
v=expr.eval(md)

if v: return section(None,md)

if self.elses: return self.elses(None, md)

finally: md._pop(1)

return ''

__call__=render
if expr is None: cond=name
else: cond=expr.eval
sections.append(cond)
sections.append(section.blocks)

if elses is not None: sections.append(elses)

self.simple_form=tuple(sections)

class Unless:
name='unless'
Expand All @@ -188,28 +173,9 @@ def __init__(self, blocks):
tname, args, section = blocks[0]
args=parse_params(args, name='', expr='')
name,expr=name_param(args,'unless',1)
self.__name__ = name
self.section=section
self.expr=expr

def render(self,md):
name=self.__name__
expr=self.expr
if expr is None:
try: v=md[name]
except KeyError, ev:
if ev is not name: raise KeyError, name, sys.exc_traceback
v=None
if not v:
md._push({name:v})
try: return self.section(None,md)
finally: md._pop(1)
else:
if not expr.eval(md): return self.section(None,md)

return ''

__call__=render
if expr is None: cond=name
else: cond=expr.eval
self.simple_form=(cond,None,section.blocks)

class Else(Unless):
# The else tag is included for backward compatibility and is deprecated.
Expand All @@ -219,6 +185,28 @@ class Else(Unless):
##########################################################################
#
# $Log: DT_If.py,v $
# Revision 1.9 1998/04/02 17:37:35 jim
# Major redesign of block rendering. The code inside a block tag is
# compiled as a template but only the templates blocks are saved, and
# later rendered directly with render_blocks.
#
# Added with tag.
#
# Also, for the HTML syntax, we now allow spaces after # and after end
# or '/'. So, the tags::
#
# <!--#
# with spam
# -->
#
# and::
#
# <!--#
# end with
# -->
#
# are valid.
#
# Revision 1.8 1998/01/14 18:23:42 jim
# Added expr to unless.
#
Expand Down
Loading

0 comments on commit 96f2d92

Please sign in to comment.