OSDN Git Service

Strip spaces at the head and end of line, but first line head is unable.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Fri, 25 Aug 2006 19:06:28 +0000 (04:06 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Fri, 25 Aug 2006 19:06:28 +0000 (04:06 +0900)
src/Hage1/barehtmlparser.py
src/Hage1/thread_window.py

index 8761e01..0b8cc68 100644 (file)
@@ -27,6 +27,8 @@ class BareHTMLParser(HTMLParserEx):
     where untied_data is non markuped string
     and is_bold is whether untied_data is bold or not
     and href is url anchor if exists
+
+    strip spaces at the head and end of line, but first line's head is unable.
     """
 
     def __init__(self, to_out_func):
@@ -34,29 +36,52 @@ class BareHTMLParser(HTMLParserEx):
         self.to_out_func = to_out_func
         self.bold = False
         self.href = None
+        self.buffer = ""
 
     def reset_func(self, to_out_func):
         self.to_out_func = to_out_func
 
     def to_out(self, data):
-        self.to_out_func(data, self.bold, self.href)
+        n = len(self.buffer)
+        if n > 0 and self.buffer[n-1] == "\n":
+            data = data.lstrip(" ")
+        self.buffer = self.buffer + data
+
+    def flush(self):
+        if self.buffer:
+            self.to_out_func(self.buffer, self.bold, self.href)
+            self.buffer = ""
+
+    def newline(self):
+        self.buffer = self.buffer.rstrip(" ")
+        self.to_out("\n")
 
-    # handle_* are overriden methods
+    # override
+    # flush after closing
+    def close(self):
+        HTMLParserEx.close(self)
+        self.flush()
 
+    # override handle_*
+    
     def handle_starttag(self, tag, attr):
         if tag == "b":
+            self.flush()
             self.bold = True
         elif tag == "br":
-            self.to_out("\n")
+            self.newline()
         elif tag == "a":
+            self.flush()
             for item in attr:
                 if item[0] == "href":
                     self.href = item[1]
 
     def handle_endtag(self, tag):
         if tag == "b":
+            self.flush()
             self.bold = False
         elif tag == "a":
+            self.flush()
             self.href = None
 
     def handle_data(self, data):
index 7f56e39..7d3a786 100644 (file)
@@ -224,28 +224,26 @@ class WinWrap:
         datfile.split_line_to_elems(line.decode("cp932", "replace"), h)
         
     def reselems_to_buffer(self, num, name, mail, date, msg):
+        p = barehtmlparser.BareHTMLParser(self.untiedata_to_buffer)
         # number
-        self.textbuffer.insert(self.enditer, str(num) + " ")
+        p.feed(str(num) + " ")
 
         # name
-        p = barehtmlparser.BareHTMLParser(self.untiedata_to_buffer)
         p.feed("<b>" + name + "</b>")
-        p.close()
 
         # mail
-        self.textbuffer.insert(self.enditer, "[" + mail + "]")
+        p.feed("[" + mail + "]")
 
         # date
         p.feed(date)
-        p.close()
-        self.textbuffer.insert(self.enditer, "\n")
+        p.feed("<br>")
 
         # msg
         p.reset_func(self.untiedata_to_buffer_with_leftmargin)
         p.feed(msg)
-        p.close()
 
-        self.textbuffer.insert(self.enditer, "\n\n")
+        p.feed("<br><br>")
+        p.close()
 
     def untiedata_to_buffer(self, data, bold, href):
         if bold: