Commit 41079120 authored by Evan Simpson's avatar Evan Simpson

Add "prefix" attribute to dtml-in and dtml-tree tags.

parent bdab8faf
This diff is collapsed.
...@@ -85,10 +85,9 @@ ...@@ -85,10 +85,9 @@
__doc__='''Sequence variables support __doc__='''Sequence variables support
$Id: DT_InSV.py,v 1.19 2001/04/27 18:07:10 andreas Exp $''' $Id: DT_InSV.py,v 1.20 2001/05/16 19:07:02 evan Exp $'''
__version__='$Revision: 1.19 $'[11:-2] __version__='$Revision: 1.20 $'[11:-2]
from string import lower, rfind, split, join
from math import sqrt from math import sqrt
import re import re
TupleType=type(()) TupleType=type(())
...@@ -100,11 +99,16 @@ except: mv=None ...@@ -100,11 +99,16 @@ except: mv=None
class sequence_variables: class sequence_variables:
def __init__(self,items=None,query_string='',start_name_re=None): alt_prefix = None
def __init__(self,items=None,query_string='',start_name_re=None,
alt_prefix=''):
self.items=items self.items=items
self.query_string=query_string self.query_string=query_string
self.start_name_re=start_name_re self.start_name_re=start_name_re
if alt_prefix:
self.alt_prefix = alt_prefix + '_'
self.data=data={ self.data=data={
'previous-sequence': 0, 'previous-sequence': 0,
...@@ -126,7 +130,7 @@ class sequence_variables: ...@@ -126,7 +130,7 @@ class sequence_variables:
if type(i) is tt and len(i)==2: return i[1] if type(i) is tt and len(i)==2: return i[1]
return i return i
def roman(self,index): return lower(self.Roman(index)) def roman(self,index): return self.Roman(index).lower()
def Roman(self,num): def Roman(self,num):
# Force number to be an integer value # Force number to be an integer value
...@@ -165,12 +169,12 @@ class sequence_variables: ...@@ -165,12 +169,12 @@ class sequence_variables:
# Replaces special cases in Roman Numerals # Replaces special cases in Roman Numerals
roman = sub('DCCCC', 'CM', roman) roman = roman.replace('DCCCC', 'CM')
roman = sub('CCCC', 'CD', roman) roman = roman.replace('CCCC', 'CD')
roman = sub('LXXXX', 'XC', roman) roman = roman.replace('LXXXX', 'XC')
roman = sub('XXXX', 'XL', roman) roman = roman.replace('XXXX', 'XL')
roman = sub('VIIII', 'IX', roman) roman = roman.replace('VIIII', 'IX')
roman = sub('IIII', 'IV', roman) roman = roman.replace('IIII', 'IV')
return roman return roman
...@@ -196,7 +200,7 @@ class sequence_variables: ...@@ -196,7 +200,7 @@ class sequence_variables:
return self.value(index,name) != self.value(index+1,name) return self.value(index,name) != self.value(index+1,name)
def length(self, ignored): def length(self, ignored):
l=self.data['sequence-length']=len(self.items) l=self['sequence-length']=len(self.items)
return l return l
def query(self, *ignored): def query(self, *ignored):
...@@ -227,7 +231,7 @@ class sequence_variables: ...@@ -227,7 +231,7 @@ class sequence_variables:
query_string='?'+query_string[1:] query_string='?'+query_string[1:]
else: query_string='?' else: query_string='?'
self.data['sequence-query']=query_string self['sequence-query']=query_string
return query_string return query_string
...@@ -249,7 +253,11 @@ class sequence_variables: ...@@ -249,7 +253,11 @@ class sequence_variables:
for item in items: for item in items:
try: try:
if mapping: item=item[name] if mapping: item=item[name]
else: item=getattr(item,name) else:
try: item=getattr(item,name)
except:
if name != 'item':
raise
try: try:
if item is mv: if item is mv:
item = None item = None
...@@ -389,6 +397,12 @@ class sequence_variables: ...@@ -389,6 +397,12 @@ class sequence_variables:
} }
for n in statistic_names: special_prefixes[n]=statistics for n in statistic_names: special_prefixes[n]=statistics
def __setitem__(self, key, value):
self.data[key] = value
if self.alt_prefix:
if key.startswith('sequence-'): key = key[9:]
self.data[self.alt_prefix + key] = value
def __getitem__(self,key, def __getitem__(self,key,
special_prefixes=special_prefixes, special_prefixes=special_prefixes,
special_prefix=special_prefixes.has_key special_prefix=special_prefixes.has_key
...@@ -396,9 +410,19 @@ class sequence_variables: ...@@ -396,9 +410,19 @@ class sequence_variables:
data=self.data data=self.data
if data.has_key(key): return data[key] if data.has_key(key): return data[key]
l=rfind(key,'-') l=key.rfind('-')
if l < 0: raise KeyError, key if l < 0:
alt_prefix = self.alt_prefix
if not (alt_prefix and key.startswith(alt_prefix)):
raise KeyError, key
suffix = key[len(alt_prefix):].replace('_', '-')
if '-' in suffix:
try: return self[suffix]
except KeyError: pass
prefix = 'sequence'
key = 'sequence-' + suffix
else:
suffix=key[l+1:] suffix=key[l+1:]
prefix=key[:l] prefix=key[:l]
...@@ -420,11 +444,6 @@ class sequence_variables: ...@@ -420,11 +444,6 @@ class sequence_variables:
raise KeyError, key raise KeyError, key
def sub(s1, s2, src):
return join(split(src, s1), s2)
def opt(start,end,size,orphan,sequence): def opt(start,end,size,orphan,sequence):
if size < 1: if size < 1:
if start > 0 and end > 0 and end >= start: if start > 0 and end > 0 and end >= start:
......
...@@ -82,11 +82,10 @@ ...@@ -82,11 +82,10 @@
# attributions are listed in the accompanying credits file. # attributions are listed in the accompanying credits file.
# #
############################################################################## ##############################################################################
'''$Id: DT_Util.py,v 1.76 2001/04/30 14:46:00 shane Exp $''' '''$Id: DT_Util.py,v 1.77 2001/05/16 19:07:02 evan Exp $'''
__version__='$Revision: 1.76 $'[11:-2] __version__='$Revision: 1.77 $'[11:-2]
import re, os import re, os
from string import lower
from RestrictedPython.Guards import safe_builtins from RestrictedPython.Guards import safe_builtins
from RestrictedPython.Utilities import utility_builtins from RestrictedPython.Utilities import utility_builtins
from RestrictedPython.Eval import RestrictionCapableEval from RestrictedPython.Eval import RestrictionCapableEval
...@@ -223,6 +222,25 @@ class Eval(RestrictionCapableEval): ...@@ -223,6 +222,25 @@ class Eval(RestrictionCapableEval):
md._push(kw) md._push(kw)
return self.eval(md) return self.eval(md)
simple_name = re.compile('[a-z][a-z0-9_]*', re.I).match
class Add_with_prefix:
def __init__(self, map, defprefix, prefix):
self.map = map
self.defprefix = defprefix
self.prefix = prefix
def __setitem__(self, name, value):
map = self.map
map[name] = value
dp = self.defprefix
if name.startswith(dp + '-'):
map[self.prefix + name[len(dp):].replace('-', '_')] = value
else:
map['%s_%s' % (self.prefix, name)] = value
def add_with_prefix(map, defprefix, prefix):
if not prefix: return map
return Add_with_prefix(map, defprefix, prefix)
def name_param(params,tag='',expr=0, attr='name', default_unnamed=1): def name_param(params,tag='',expr=0, attr='name', default_unnamed=1):
used=params.has_key used=params.has_key
...@@ -377,11 +395,11 @@ def parse_params(text, ...@@ -377,11 +395,11 @@ def parse_params(text,
mo_unq = qunparmre.match(text) mo_unq = qunparmre.match(text)
if mo_p: if mo_p:
name=lower(mo_p.group(2)) name=mo_p.group(2).lower()
value=mo_p.group(3) value=mo_p.group(3)
l=len(mo_p.group(1)) l=len(mo_p.group(1))
elif mo_q: elif mo_q:
name=lower(mo_q.group(2)) name=mo_q.group(2).lower()
value=mo_q.group(3) value=mo_q.group(3)
l=len(mo_q.group(1)) l=len(mo_q.group(1))
elif mo_unp: elif mo_unp:
......
...@@ -50,6 +50,11 @@ in: Loops over sequences ...@@ -50,6 +50,11 @@ in: Loops over sequences
next -- Iterates once if there is a next batch. Sets batch variables next -- Iterates once if there is a next batch. Sets batch variables
for the next sequence. for the next sequence.
prefix=string -- Provide versions of the tag variables that start
with this prefix instead of "sequence", and that use underscores
(_) instead of hyphens (-). The prefix must start with a letter and
contain only alphanumeric characters and underscores (_).
Tag Variables Tag Variables
Current Item Variables Current Item Variables
...@@ -203,6 +208,19 @@ in: Loops over sequences ...@@ -203,6 +208,19 @@ in: Loops over sequences
title: <dtml-var title><br> title: <dtml-var title><br>
</dtml-in> </dtml-in>
Looping over two sets of objects, using prefixes::
<dtml-let rows="(1,2,3)" cols="(4,5,6)">
<dtml-in rows prefix="row">
<dtml-in cols prefix="col">
<dtml-var expr="row_item * col_item"><br>
<dtml-if col_end>
<dtml-var expr="col_total_item * row_mean_item">
</dtml-if>
</dtml-in>
</dtml-in>
</dtml-let>
Looping over a list of '(key, value)' tuples:: Looping over a list of '(key, value)' tuples::
<dtml-in objectItems> <dtml-in objectItems>
......
...@@ -70,6 +70,11 @@ tree: Inserts a tree widget ...@@ -70,6 +70,11 @@ tree: Inserts a tree widget
expanding and contracting widget links. This attribute is for expanding and contracting widget links. This attribute is for
advanced usage only. advanced usage only.
prefix=string -- Provide versions of the tag variables that start
with this prefix instead of "tree", and that use underscores
(_) instead of hyphens (-). The prefix must start with a letter and
contain only alphanumeric characters and underscores (_).
Tag Variables Tag Variables
tree-item-expanded -- True if the current node is expanded. tree-item-expanded -- True if the current node is expanded.
......
...@@ -84,16 +84,17 @@ ...@@ -84,16 +84,17 @@
############################################################################## ##############################################################################
"""Rendering object hierarchies as Trees """Rendering object hierarchies as Trees
""" """
__rcs_id__='$Id: TreeTag.py,v 1.46 2001/04/27 20:27:57 shane Exp $' __rcs_id__='$Id: TreeTag.py,v 1.47 2001/05/16 19:07:06 evan Exp $'
__version__='$Revision: 1.46 $'[11:-2] __version__='$Revision: 1.47 $'[11:-2]
from DocumentTemplate.DT_Util import * from DocumentTemplate.DT_Util import *
from DocumentTemplate.DT_String import String from DocumentTemplate.DT_String import String
from string import join, split, rfind, find, translate from string import join, split, rfind, find, translate, replace
from urllib import quote, unquote from urllib import quote, unquote
from zlib import compress, decompress from zlib import compress, decompress
from binascii import b2a_base64, a2b_base64 from binascii import b2a_base64, a2b_base64
import re
tbl=join(map(chr, range(256)),'') tbl=join(map(chr, range(256)),'')
tplus=tbl[:ord('+')]+'-'+tbl[ord('+')+1:] tplus=tbl[:ord('+')]+'-'+tbl[ord('+')+1:]
...@@ -116,7 +117,7 @@ class Tree: ...@@ -116,7 +117,7 @@ class Tree:
# closed_decoration=None, # closed_decoration=None,
# childless_decoration=None, # childless_decoration=None,
assume_children=1, assume_children=1,
urlparam=None) urlparam=None, prefix=None)
has_key=args.has_key has_key=args.has_key
if has_key('') or has_key('name') or has_key('expr'): if has_key('') or has_key('name') or has_key('expr'):
...@@ -138,6 +139,11 @@ class Tree: ...@@ -138,6 +139,11 @@ class Tree:
if not has_key('childless_decoration'): if not has_key('childless_decoration'):
args['childless_decoration']='' args['childless_decoration']=''
prefix = args.get('prefix')
if prefix and not simple_name(prefix):
raise ParseError, _tm(
'prefix is not a simple name', 'tree')
self.__name__ = name self.__name__ = name
self.section=section.blocks self.section=section.blocks
self.args=args self.args=args
...@@ -252,6 +258,11 @@ def tpRender(self, md, section, args, ...@@ -252,6 +258,11 @@ def tpRender(self, md, section, args,
'tree-colspan': colspan, 'tree-colspan': colspan,
'tree-state': state } 'tree-state': state }
prefix = args.get('prefix')
if prefix:
for k, v in treeData.items():
treeData[prefix + replace(k[4:], '-', '_')] = v
md._push(InstanceDict(self, md)) md._push(InstanceDict(self, md))
md._push(treeData) md._push(treeData)
...@@ -283,9 +294,10 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data, ...@@ -283,9 +294,10 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data,
url = (url and ('%s/%s' % (url, tpUrl))) or tpUrl url = (url and ('%s/%s' % (url, tpUrl))) or tpUrl
root_url = root_url or tpUrl root_url = root_url or tpUrl
treeData['tree-item-url']=url ptreeData = add_with_prefix(treeData, 'tree', args.get('prefix'))
treeData['tree-level']=level ptreeData['tree-item-url']=url
treeData['tree-item-expanded']=0 ptreeData['tree-level']=level
ptreeData['tree-item-expanded']=0
idattr=args['id'] idattr=args['id']
output=data.append output=data.append
...@@ -404,7 +416,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data, ...@@ -404,7 +416,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data,
param = "" param = ""
if exp: if exp:
treeData['tree-item-expanded']=1 ptreeData['tree-item-expanded']=1
output('<A NAME="%s" HREF="%s?%stree-c=%s#%s">' output('<A NAME="%s" HREF="%s?%stree-c=%s#%s">'
'<IMG SRC="%s/p_/mi" ALT="-" BORDER=0></A>' % '<IMG SRC="%s/p_/mi" ALT="-" BORDER=0></A>' %
(id, root_url, param, s, id, script)) (id, root_url, param, s, id, script))
...@@ -463,7 +475,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data, ...@@ -463,7 +475,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data,
else: doc=None else: doc=None
if doc is not None: if doc is not None:
treeData['-tree-substate-']=sub treeData['-tree-substate-']=sub
treeData['tree-level']=level ptreeData['tree-level']=level
md._push(treeData) md._push(treeData)
try: output(doc( try: output(doc(
None,md, None,md,
...@@ -482,7 +494,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data, ...@@ -482,7 +494,7 @@ def tpRenderTABLE(self, id, root_url, url, state, substate, diff, data,
else: doc=None else: doc=None
if doc is not None: if doc is not None:
treeData['-tree-substate-']=sub treeData['-tree-substate-']=sub
treeData['tree-level']=level ptreeData['tree-level']=level
md._push(treeData) md._push(treeData)
try: output(doc( try: output(doc(
None,md, None,md,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment