From 2c2ce72719cabe69ab929d8d48df38da2273aeb3 Mon Sep 17 00:00:00 2001 From: "Thomas \"Cakeisalie5\" Touhey" Date: Thu, 24 May 2018 14:43:17 +0200 Subject: [PATCH] Fixed a buffer problem, still a queue problem to fix. --- textoutpc/stream.py | 9 +++++++++ textoutpc/tags/base.py | 6 +++++- textoutpc/translate.py | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/textoutpc/stream.py b/textoutpc/stream.py index 3ebf2c9..8484bc8 100755 --- a/textoutpc/stream.py +++ b/textoutpc/stream.py @@ -125,18 +125,22 @@ class TextoutStream: def __init__(self, stream): # If the 'stream' is a string, we want to use standard stream # functions, so we're gonna enforce them using the `StringIO` class. + if isinstance(stream, str): stream = _io.StringIO(stream) # Buffer management. + self.stream = stream self.buf = "" # Management of the last tag match. + self.result = None self.last = None # Error position. + self.pos = 0 self.line = 0 self.col = 0 @@ -148,18 +152,21 @@ class TextoutStream: def __next__(self): # If we have a result, process it. + if self.result: data, self.result = TextoutUnit(self.result), None self.last = data return data # Make sure to have enough data to read. + self.buf += self.stream.read(self.BUFFER_SIZE - len(self.buf)) if not self.buf: self.last = None raise StopIteration # Check that we have a result. + result = self._Tag.search(self.buf, partial = True) if not result: text = self.buf @@ -169,6 +176,7 @@ class TextoutStream: # If there is some text, return it. # Eventually store the result so we can process it later. + if result.start() > 0: ret = self.buf[:result.start()] self.buf = self.buf[result.end():] @@ -178,6 +186,7 @@ class TextoutStream: return ret # Process the result now! + self.buf = self.buf[result.end():] data = TextoutUnit(result) self.last = data diff --git a/textoutpc/tags/base.py b/textoutpc/tags/base.py index 4bd47b2..8cab7f3 100755 --- a/textoutpc/tags/base.py +++ b/textoutpc/tags/base.py @@ -97,12 +97,16 @@ class TextoutTag: return ret def __out(self, name): - """ Generic function to call two output functions of the same type. """ + """ Generic function to call two output functions of the same + type. """ getattr(self, '__' + name)() getattr(self, name + '_' + self.__output_type)() def __after_preprocess(self): + """ After preprocessing, check the begin, content and end that may + have been set by the preprocessing function. """ + ot = self.__output_type for otype in ('begin', 'content', 'end'): diff --git a/textoutpc/translate.py b/textoutpc/translate.py index 36e0175..2ec70cb 100755 --- a/textoutpc/translate.py +++ b/textoutpc/translate.py @@ -317,6 +317,11 @@ class Translator: if dat.ign: self.cign += 1 + # If we're about to put a tag or anything, empty the text block + # here. + + self.flush_text() + # If it is a block, end the current block. if dat.type == dat.BLOCK: @@ -411,6 +416,7 @@ class Translator: # XXX: I'm unsure about this. Shall raw tags return code # or text? The text will only be escaped as raw mode is # still enabled at this point. + self.put_text(content) else: self.put_code(content) @@ -506,6 +512,8 @@ class Translator: def end_block(self): """ End the current block. """ + # Get the original queue. + queue = self.queue.copy() # We want to collect inline and block tags, in the order they @@ -517,8 +525,10 @@ class Translator: # Check if the tag has been started and if it is a super # block (which means we want to stop here). - if dat.super: break - if not dat.started: continue + if dat.super: + break + if not dat.started: + continue # Then put the tag in the appropriate queue.