An idea - request for comments

Rafal Smotrzyk rav at smo3k.z.pl
Wed Aug 4 16:05:36 CEST 1999


#Simple but working

import re

class Response:
   def __init__(self):
      self.HTMLDocument=''
   def write(self,adata):
      self.HTMLDocument=self.HTMLDocument+adata

def GetTextAsHTMLText(s,repldict={}):
   for akey in repldict.keys():
      c=re.compile('\<\%\= *'+akey+' *\%\>', re.I)
      s=c.sub(repldict[akey],s)
   c=re.compile('\<\%', re.I)
   s=c.sub('""")',s)
   c=re.compile('\%\>', re.I)
   s=c.sub('Response.write("""',s)
   s='Response.write("""'+s+'""")'
   aresponse=Response()
   gdict={'Response':aresponse}
   exec s in gdict,repldict
   return aresponse.HTMLDocument

def DoTest():
   ahtmlsource=r"""
<HTML>
<BODY>
<%
Response.write('<h1>'+ScriptName+'</h1>\n')
Response.write('<table>')
x=0
while x<10:
   s='<tr><td>%s</td><td>%s</td></tr>\n'%('d1_'+`x`,'d2_'+`x`)
   Response.write(s)
   x=x+1
Response.write('</table>')
%>
<p>
<%
for i in range(10):
   Response.write(`i`+',')
Response.write('<br>')
%>
Test line 1 = <%= Test1 %>
Test line 2 = <%=Test2%>
</p>
</BODY></HTML>
"""
   adict={'Test1':'this is page constant','Test2':'another
constant','ScriptName':'This is script name'}
   s=GetTextAsHTMLText(ahtmlsource,adict)
   print s

if __name__=='__main__':
   DoTest()

# Rafal Smotrzyk







More information about the Python-list mailing list