[svnbook commit] r2491 - trunk/www/tools

cmpilato noreply at red-bean.com
Fri Oct 20 01:39:15 CDT 2006


Author: cmpilato
Date: Fri Oct 20 01:39:15 2006
New Revision: 2491

Modified:
   trunk/www/tools/make-ad-sense.py

Log:
Tweak algorithm to handle multi-line <body> tags.

Patch by: cmpilato
          me

* make-ad-sense.py
  (_body_open_re): Remove.
  (add_adsense_html): Use string.find() in two loops instead of a
    regular expression in one loop.


Modified: trunk/www/tools/make-ad-sense.py
==============================================================================
--- trunk/www/tools/make-ad-sense.py	(original)
+++ trunk/www/tools/make-ad-sense.py	Fri Oct 20 01:39:15 2006
@@ -47,17 +47,23 @@
     sys.stderr.write(msg + "\n")
     sys.exit(1)
 
-_body_open_re = re.compile('^(.*<body[^>]*>)(.*)$')
-
 def add_adsense_html(file):
     lines = open(file, 'r').readlines()
     for i in range(len(lines)):
-        match = _body_open_re.match(lines[i])
-        if match and match.groups():
-            lines[i] = '%s%s%s' \
-                       % (match.group(1), adsense_data, match.group(2))
-            open(file, 'w').writelines(lines)
-            return
+        start_offset = lines[i].find('<body')
+        if start_offset == -1:
+            continue
+        for j in range(i, len(lines)):
+            end_offset = lines[j][start_offset:].find('>')
+            if end_offset == -1:
+                start_offset = 0
+            else:
+                end_offset = start_offset + end_offset
+                lines[j] = '%s%s%s' \
+                           % (lines[j][:end_offset + 1],
+                              adsense_data, lines[j][end_offset + 1:])
+                open(file, 'w').writelines(lines)
+                return
     raise Exception, "Never found <body> tag in file '%s'" % (file)
 
 def add_adsense_css(file):




More information about the svnbook-dev mailing list