2
0
Fork 0

It works far better!

This commit is contained in:
Thomas Touhey 2018-06-21 02:26:07 +02:00
parent 167438a97f
commit d9e6f27979
No known key found for this signature in database
GPG Key ID: 2ECEB0517AD947FB
1 changed files with 53 additions and 47 deletions

View File

@ -186,14 +186,14 @@ class Translator:
# Text outputting utilities.
# ---
def process_text_group(self):
def process_text(self, text):
""" Process text groups for naked URLs and stuff. """
# In all cases, we want to escape for HTML things, so that the
# user doesn't insert raw HTML tags (which would be a security flaw!).
if self.output_type == 'html':
text = _htmlescape(self.text_group)
text = _htmlescape(text)
# For non-raw HTML, we want to add smiley and URLs conversion,
# because it's nicer!
@ -226,40 +226,69 @@ class Translator:
if not self.text_group or self.cign > 0:
return
# Pop the text group and put the code, with the process function in
# case it is given to a non-raw processing tag or given to the
# output.
text = self.text_group
self.text_group = ""
self.add_text(text, process_func = lambda x: self.process_text(x))
# ---
# Code outputting utilities.
# ---
def add_text(self, text, process_func = lambda x: x, start_tags = True):
""" Add text to the higher blocks if available. """
# The last queue is composed of booleans (does the group contain
# something or not) and texts for content processing.
# We want to set all of the booleans to True until the first text
# group, to which we want to add the current text.
# If there is no content preprocessing and we have to output it,
# we want to start the tags first: `dat == None` will be our signal!
#
# Think about resetting `text_group` as its content has been used
# somewhere (unbuffer data).
blockfound = False
for dat in self.queue:
# Check if it is a tag we want to contribute to.
if dat.type == dat.BLOCK:
if dat.super or not blockfound:
blockfound = True
else:
continue
# Contribute to it, either by or-ing the content if it is
# a boolean (but anything or True == True), or by contributing
# to the buffer otherwise.
if isinstance(dat.last, bool):
dat.last = True
continue
dat.last += self.text_group
if not dat.raw:
text = process_func(text)
dat.last += text
break
else:
dat = None
text = self.process_text_group()
# No `break` has been encountered, which means the content has
# not been added to any preprocessing tag. Please process it!
self.text_group = ""
if start_tags:
self.start_tags()
self.outp.write(process_func(text))
# Start the tags that haven't been started, and stuff.
return False
self.start_tags()
# The content has been given for preprocessing.
# If the content has to be written, we ought to.
return True
if dat == None:
self.outp.write(text)
def put_debug(self, message):
""" Put a debug message directly into the output. """
# ---
# Code outputting utilities.
# ---
self.outp.write(message)
def put_code(self, code, start_tags = True):
""" Put some code. """
@ -274,31 +303,9 @@ class Translator:
if not code or self.cign > 0:
return
# As in `flush_text()`, the last queue is composed of booleans.
# We want to set all of the booleans to True until the first text
# group, to which we want to add the current text.
# If there is no content preprocessing and we have to output it,
# we want to start the tags first: `dat == None` will be our signal!
# Add the code.
for dat in self.queue:
if isinstance(dat.last, bool):
dat.last = True
continue
dat.last += code
break
else:
dat = None
# Start the surrounding tags first, if we are not used by the
# `start_tags()` method itself.
if start_tags:
self.start_tags()
# If the content has to be written, we ought to.
if dat == None:
self.outp.write(code)
self.add_text(code, start_tags = start_tags)
def put_newline(self):
""" Put a newline. """
@ -411,9 +418,9 @@ class Translator:
# Output the beginning and the content. If there was no content,
# just put the content that we got earlier.
dat.started = True
if hasattr(tag, 'begin'):
self.put_code(tag.begin())
self.put_code(tag.begin(), False)
dat.started = True
if hasattr(tag, 'content'):
self.put_code(tag.content())
@ -459,7 +466,7 @@ class Translator:
if dat.type == dat.BLOCK:
self.close_inline_tags()
if hasattr(tag, 'end'):
self.put_code(tag.end())
self.put_code(tag.end(), False)
# Disable raw mode if it was a raw tag (which means that it enabled it,
# as tags into raw tags cannot be processed).
@ -680,12 +687,11 @@ class Translator:
self.push_tag(_TagData(_TextoutParagraphTag(None, None,
self.output_type, self.tweaks), None, ''))
# End of file, it seems! Let's close the tags, flush the text
# and just resume our lives from there.
# End of file, it seems! Let's close the tags and just resume our
# lives from there.
while self.queue:
self.pop_tag()
self.flush_text()
# And don't forget to return the output for the user to chain
# stuff easily ;)