changeset 3914:bd0aa9e93ed6

ParserBase / text_python parser: only use a single request.write
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 02 Aug 2008 16:32:27 +0200
parents ab09415de20b
children b4bdfaaa79b0
files MoinMoin/parser/_ParserBase.py MoinMoin/parser/text_python.py
diffstat 2 files changed, 36 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/parser/_ParserBase.py	Sat Aug 02 15:35:36 2008 +0200
+++ b/MoinMoin/parser/_ParserBase.py	Sat Aug 02 16:32:27 2008 +0200
@@ -220,42 +220,48 @@
             re_flags |= re.I
         scan_re = re.compile("|".join(formatting_regexes), re_flags)
 
-        self.lastpos = 0
         self.text = self.raw
         self.text_len = len(self.text)
 
+        result = [] # collects output
+
         self._code_id = sha.new(self.raw.encode(config.charset)).hexdigest()
-        self.request.write(formatter.code_area(1, self._code_id, self.parsername, self.show_nums, self.num_start, self.num_step))
+        result.append(formatter.code_area(1, self._code_id, self.parsername, self.show_nums, self.num_start, self.num_step))
 
-        self.request.write(formatter.code_line(1))
+        result.append(formatter.code_line(1))
             #formatter, len('%d' % (self.line_count,)))
 
+        self.lastpos = 0
         match = scan_re.search(self.text)
         while match and self.lastpos < self.text_len:
             # add the match we found
-            self.write_normal_text(formatter,
-                                   self.text[self.lastpos:match.start()])
+            result.extend(self.format_normal_text(formatter,
+                                                  self.text[self.lastpos:match.start()]))
             self.lastpos = match.end() + (match.end() == self.lastpos)
 
-            self.write_match(formatter, match)
+            result.extend(self.format_match(formatter, match))
 
             # search for the next one
             match = scan_re.search(self.text, self.lastpos)
 
-        self.write_normal_text(formatter, self.text[self.lastpos:])
+        result.extend(self.format_normal_text(formatter, self.text[self.lastpos:]))
 
-        self.request.write(formatter.code_area(0, self._code_id))
+        result.append(formatter.code_area(0, self._code_id))
+        self.request.write(''.join(result))
 
-    def write_normal_text(self, formatter, text):
+    def format_normal_text(self, formatter, text):
+        result = []
         first = True
         for line in text.expandtabs(self.tabwidth).split('\n'):
             if not first:
-                self.request.write(formatter.code_line(1))
+                result.append(formatter.code_line(1))
             else:
                 first = False
-            self.request.write(formatter.text(line))
+            result.append(formatter.text(line))
+        return result
 
-    def write_match(self, formatter, match):
+    def format_match(self, formatter, match):
+        result = []
         for n, hit in match.groupdict().items():
             if not hit:
                 continue
@@ -267,8 +273,9 @@
             first = True
             for line in s.expandtabs(self.tabwidth).split('\n'):
                 if not first:
-                    self.request.write(formatter.code_line(1))
+                    result.append(formatter.code_line(1))
                 else:
                     first = False
-                self.request.write(c.formatString(formatter, line))
+                result.append(c.formatString(formatter, line))
+        return result
 
--- a/MoinMoin/parser/text_python.py	Sat Aug 02 15:35:36 2008 +0200
+++ b/MoinMoin/parser/text_python.py	Sat Aug 02 16:32:27 2008 +0200
@@ -60,10 +60,12 @@
             self.lines.append(pos)
         self.lines.append(len(self.raw))
 
+        self.result = [] # collects output
+
         self._code_id = sha.new(self.raw.encode(config.charset)).hexdigest()
-        self.request.write(formatter.code_area(1, self._code_id, 'ColorizedPython', self.show_num, self.num_start, self.num_step))
+        self.result.append(formatter.code_area(1, self._code_id, 'ColorizedPython', self.show_num, self.num_start, self.num_step))
         self.formatter = formatter
-        self.request.write(formatter.code_line(1))
+        self.result.append(formatter.code_line(1))
         #len('%d' % (len(self.lines)-1, )))
 
         # parse the source and write it
@@ -76,7 +78,7 @@
             errmsg = (self.formatter.linebreak() +
                       self.formatter.strong(1) + "ERROR: %s" % msg + self.formatter.strong(0) +
                       self.formatter.linebreak())
-            self.request.write(errmsg)
+            self.result.append(errmsg)
         except tokenize.TokenError, ex:
             msg = ex[0]
             line = ex[1][0]
@@ -84,9 +86,10 @@
                       self.formatter.strong(1) + "ERROR: %s" % msg + self.formatter.strong(0) +
                       self.formatter.linebreak() +
                       wikiutil.escape(self.raw[self.lines[line]:]))
-            self.request.write(errmsg)
-        self.request.write(self.formatter.code_line(0))
-        self.request.write(formatter.code_area(0, self._code_id))
+            self.result.append(errmsg)
+        self.result.append(self.formatter.code_line(0))
+        self.result.append(formatter.code_area(0, self._code_id))
+        self.request.write(''.join(self.result))
 
     def __call__(self, toktype, toktext, (srow, scol), (erow, ecol), line):
         """ Token handler.
@@ -98,13 +101,13 @@
 
         # handle newlines
         if toktype in [token.NEWLINE, tokenize.NL]:
-            self.request.write(self.formatter.code_line(0))
-            self.request.write(self.formatter.code_line(1))
+            self.result.append(self.formatter.code_line(0))
+            self.result.append(self.formatter.code_line(1))
             return
 
         # send the original whitespace, if needed
         if newpos > oldpos:
-            self.request.write(self.formatter.text(self.raw[oldpos:newpos]))
+            self.result.append(self.formatter.text(self.raw[oldpos:newpos]))
 
         # skip indenting tokens
         if toktype in [token.INDENT, token.DEDENT]:
@@ -122,11 +125,11 @@
         first = True
         for part in toktext.split('\n'):
             if not first:
-                self.request.write(self.formatter.code_line(0))
-                self.request.write(self.formatter.code_line(1))
+                self.result.append(self.formatter.code_line(0))
+                self.result.append(self.formatter.code_line(1))
             else:
                 first = False
-            self.request.write(self.formatter.code_token(1, tokid) +
+            self.result.append(self.formatter.code_token(1, tokid) +
                                self.formatter.text(part) +
                                self.formatter.code_token(0, tokid))