OSDN Git Service

add htproc
authorhylom <hylom@users.sourceforge.jp>
Tue, 23 Jun 2009 10:38:55 +0000 (19:38 +0900)
committerhylom <hylom@users.sourceforge.jp>
Tue, 23 Jun 2009 10:38:55 +0000 (19:38 +0900)
htproc.py [new file with mode: 0755]

diff --git a/htproc.py b/htproc.py
new file mode 100755 (executable)
index 0000000..702e580
--- /dev/null
+++ b/htproc.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python\r
+#\r
+"""htproc.py - HTML Processor"""\r
+\r
+import sys\r
+from BeautifulSoup import BeautifulSoup\r
+\r
+from rules import rules\r
+\r
+usage = """%s <src> <dest>""" % sys.argv[0]\r
+\r
+try:\r
+    src = sys.argv[1]\r
+    dest = sys.argv[2]\r
+except IndexError:\r
+    sys.exit(usage)\r
+\r
+f_src = open(src, "r")\r
+f_dest = open(dest, "w")\r
+\r
+html = f_src.read()\r
+bsp = BeautifulSoup(html)\r
+\r
+for tag in rules:\r
+    for result in bsp.findAll(tag):\r
+        if rules[tag].has_key("begin"):\r
+            result.insert(0, rules[tag]["begin"])\r
+        if rules[tag].has_key("pre"):\r
+            rawtag = result.prettify()\r
+            if rules[tag].has_key("post"):\r
+                rawtag = rules[tag]["pre"] + rawtag + rules[tag]["post"]\r
+            else:\r
+                rawtag = rules[tag]["pre"] + rawtag\r
+            result.replaceWith(rawtag)\r
+\r
+        elif rules[tag].has_key("pre"):\r
+            rawtag = result.prettify() + rules[tag]["post"]\r
+            result.replaceWith(rawtag)\r
+\r
+#f_dest.write(bsp.prettify())\r
+f_dest.write(bsp.renderContents())\r
+\r
+f_src.close()\r
+f_dest.close()\r
+\r