--- a/Admin/website/build/obfusmail.py Wed May 03 05:56:11 2006 +0200
+++ b/Admin/website/build/obfusmail.py Wed May 03 09:45:09 2006 +0200
@@ -31,11 +31,12 @@
pass
- def __init__(self, dtd, mails):
+ def __init__(self, dtd, filename, mails):
super(FindHandler, self).__init__(self.DevZero(), outputEncoding, dtd)
+ self.filename = filename
+ self.mails = mails
self.pending_mail = None
- self.mails = mails
def startElement(self, name, attrs):
@@ -51,7 +52,7 @@
if self.pending_mail is not None:
if self.currentContent() != self.pending_mail:
raise Exception("Inconsistent mail address: '%s' vs. '%s'" % (self.currentContent(), self.pending_mail))
- self.mails[self.pending_mail] = True
+ self.mails[(self.filename, self.pending_mail)] = True
self.pending_mail = None
super(FindHandler, self).endElement(name)
@@ -61,9 +62,10 @@
class ReplaceHandler(TransformerHandler):
- def __init__(self, out, dtd, mails):
+ def __init__(self, out, dtd, filename, mails):
super(ReplaceHandler, self).__init__(out, outputEncoding, dtd)
+ self.filename = filename
self.pending_mail = None
self.mails = mails
@@ -82,7 +84,7 @@
if name == u'a':
if self.pending_mail is not None:
self.flushCharacterBuffer()
- self._out.write(self.mails[self.pending_mail])
+ self._out.write(self.mails[(self.filename, self.pending_mail)])
self.pending_mail = None
return
@@ -97,21 +99,25 @@
pass
-def obfuscate(mailaddr, dsturl, dstfile):
+def obfuscate(mailaddr, htmlfile):
def mk_line(s):
return u"document.write('%s');" % s.replace("'", "\\'")
def mk_script(s):
- return u'<script type="text/javascript">%s</script>' % s
+ return u'<script type="text/javascript">/*<![CDATA[*/%s/*]]>*/</script>' % s
+ def cmd(s):
+ print "[shell cmd] %s" % s
+ n = os.system(s)
+ if n != 0:
+ raise Exception("shell cmd error: %s" % n)
name, host = mailaddr.split("@", 2)
imgname = (name + "_" + host).replace(".", "_"). replace("?", "_") + ".png"
- imgfile = path.join(dstfile, imgname)
- os.system("convert label:'%s' '%s'" % (mailaddr, imgfile))
+ imgfile = path.join(path.split(htmlfile)[0], imgname)
+ cmd("convert label:'%s' '%s'" % (mailaddr, imgfile))
mailsimple = u"{%s} AT [%s]" % (name, host)
- imgurl = posixpath.join(dsturl, imgname)
mailscript = u" ".join(map(mk_line, ['<a href="', "mailto:", name, "@", host, '">']));
- mailimg = '<img src=%s style="vertical-align:middle" alt=%s />' % (quoteattr(imgurl), quoteattr(mailsimple))
+ mailimg = '<img src=%s style="vertical-align:middle" alt=%s />' % (quoteattr(imgname), quoteattr(mailsimple))
return (mk_script(mailscript) + mailimg + mk_script(mk_line("</a>")))
@@ -124,14 +130,6 @@
description = '''Protecting mail adresses in html files by obfuscating''',
add_help_option = True,
)
- cmdlineparser.add_option("-d", "--dstroot",
- action="store", dest="dstroot",
- type="string", default=".",
- help="root destination of generated images", metavar='location')
- cmdlineparser.add_option("-D", "--dstdir",
- action="store", dest="dstdir",
- type="string", default=".",
- help="root destination of generated images", metavar='location')
cmdlineparser.add_option("-t", "--dtd",
action="store", dest="dtd",
type="string", default=".",
@@ -143,20 +141,20 @@
mails = {}
for filename in filenames:
istream = open(filename, 'r')
- findhandler = FindHandler(options.dtd, mails)
+ findhandler = FindHandler(options.dtd, filename, mails)
parseWithER(istream, findhandler)
istream.close()
# transform mails
mails_subst = {}
- for mail in mails.keys():
- mails_subst[mail] = obfuscate(mail, options.dstdir, path.join(options.dstroot, options.dstdir))
+ for filename, mail in mails.iterkeys():
+ mails_subst[(filename, mail)] = obfuscate(mail, filename)
# transform pages
for filename in filenames:
istream = StringIO(open(filename, 'r').read())
ostream = open(filename, 'wb')
- replacehandler = ReplaceHandler(ostream, options.dtd, mails_subst)
+ replacehandler = ReplaceHandler(ostream, options.dtd, filename, mails_subst)
parseWithER(istream, replacehandler)
ostream.close()
istream.close()