2
0
Fork 0

Corrected more things.

This commit is contained in:
Thomas Touhey 2018-01-19 15:15:35 +01:00
parent fcaf69c540
commit a436262a8c
No known key found for this signature in database
GPG Key ID: 2ECEB0517AD947FB
5 changed files with 68 additions and 44 deletions

View File

@ -100,7 +100,7 @@ def _wrap_test(inp, res):
_cnt += 1
return _templ.format(n = _cnt, l = _len, i = repr(inp), r = repr(res))
exec("class TextoutHTMLTest(unittest.TestCase):\n" + \
exec("class TextoutHTMLTest(unittest.TestCase):\n maxDiff = None\n" + \
'\n'.join(map(lambda args: _wrap_test(*args), __test_cases.items())),
globals())

View File

@ -44,7 +44,7 @@ class TextoutLinkTag(TextoutRawInlineTag):
def begin_html(self):
return '<a href="{}" target="_blank" rel="noopener">' \
.format(_htmlescape(self.url))
.format(self.url.replace('"', '%22'))
def end_html(self):
return '</a>'

View File

@ -79,7 +79,7 @@ class TextoutTextTag(TextoutInlineTag):
if self.font:
props.append('font-family: ' + self.font)
if self.color:
if self.color[3] < 255:
if self.color[3] < 1.0:
col = 'rgba({}, {}, {}, {})'.format(*self.color)
else:
col = '#%02X%02X%02X' \

View File

@ -8,7 +8,7 @@ from .Link import TextoutLinkTag
__all__ = ["TextoutVideoTag"]
_hexcode = _re.compile('^[a-zA-Z0-9_]+$')
_hexcode = _re.compile('[a-zA-Z0-9_]+')
_numcode = _re.compile('^/[0-9]+$')
_dailypath = _re.compile('^/video/([a-z0-9]+)$')
@ -30,22 +30,22 @@ class TextoutVideoTag(TextoutRawBlockTag):
content = cin.read()
url = _urlparse.urlparse(content)
if url.host == "youtu.be":
if url.netloc == "youtu.be":
self.id = url.path[1:]
if not _hexcode.match(self.id):
raise Exception
self.type = "youtube"
elif url.host in ('youtube.com', 'www.youtube.com'):
elif url.netloc in ('youtube.com', 'www.youtube.com'):
if url.path != '/watch':
raise Exception
self.id = _urlparse.parse_qs(url.query)['v']
if not _hexcode.match(self.id):
self.id = _urlparse.parse_qs(url.query)['v'][0]
if not _hexcode.fullmatch(self.id):
raise Exception
self.type = "youtube"
elif url.host in ('dailymotion.com', 'www.dailymotion.com'):
elif url.netloc in ('dailymotion.com', 'www.dailymotion.com'):
self.code = _dailypath.match(url.path).groups()[0]
self.type = "dailymotion"
elif url.host in ('vimeo.com', 'www.vimeo.com'):
elif url.netloc in ('vimeo.com', 'www.vimeo.com'):
self.code = url.path[1:]
if not _numcode.match(self.code):
raise Exception

View File

@ -12,12 +12,6 @@ from .Tags import TextoutRawTag, TextoutInlineTag, TextoutBlockTag, get_tag
__all__ = ["Translator"]
def process_text_group(text):
""" Process text groups for naked URLs and stuff. """
# TODO: naked URLs, smileys, etc.
return _htmlescape(text)
class Translator:
""" One-time usage class for translating. """
@ -49,6 +43,13 @@ class Translator:
self.text_group = ""
# `raw_mode` is whether the no evaluating mode is on or not.
# `raw_deg` is the number of times the raw tag has to be closed
# to exit.
self.raw_mode = False
self.raw_deg = 0
def put_text(self, text):
""" Output some text. """
@ -56,11 +57,21 @@ class Translator:
return
self.text_group += text
def process_text_group(self):
""" Process text groups for naked URLs and stuff. """
text = _htmlescape(self.text_group)
if not self.raw_mode:
# TODO: naked URLs, smileys, etc.
pass
return text
def flush_text(self):
""" Flush the text that has been output. """
if not self.cproc:
self.outp.write(process_text_group(self.text_group))
self.outp.write(self.process_text_group())
else:
self.last_queue[0] += self.text_group
@ -88,16 +99,24 @@ class Translator:
self.cign += 1
if hasattr(tag, 'begin'):
self.put_code(tag.begin())
self.queue.insert(0, tag)
if isinstance(tag, TextoutRawTag):
self.raw_mode = True
self.raw_deg = 0
def pop_tag(self):
""" Pop a tag from the tag stack. """
# Flush the text as we're about to put CODE. (run away!)
self.flush_text()
# Queue processing.
tag = self.queue.pop(0)
if isinstance(tag, TextoutRawTag):
self.raw_mode = False
if hasattr(tag, 'preprocess'):
ct = last_queue.pop(0)
ct = self.last_queue.pop(0)
content = tag.preprocess(_sio(ct))
if not content: content = ct
self.cproc -= 1
@ -114,16 +133,14 @@ class Translator:
if hasattr(tag, 'end'):
self.put_code(tag.end())
# Deactivate as soon as we pop a tag.
# This is useful for when the tag is popped automatically
# at the end of the input stream.
self.raw_mode = False
def process(self):
""" Main function of the translator. """
# `raw_mode` is whether the no evaluating mode is on or not.
# `raw_deg` is the number of times the raw tag has to be closed
# to exit.
raw_mode = False
raw_deg = 0
# Main processing loop, here we go!
for element in TextoutStream(self.inp):
@ -132,28 +149,25 @@ class Translator:
continue
tagdata = element
# Raw mode processing.
if raw_mode:
if not tagdata.type in (tagdata.BEGIN, tagdata.END, \
tagdata.SPECIAL) or tagdata.name != name_queue[0]:
self.put_text(tag.full)
continue
if tagdata.type == tagdata.BEGIN:
raw_deg += 1
continue
else:
raw_deg -= 1
if raw_deg < 0:
self.pop_tag()
raw_mode = False
# XXX: temporary, do not manage paragraphs for now
if not tagdata.type in (tagdata.BEGIN, tagdata.END, \
tagdata.SPECIAL):
self.put_text(tagdata.full)
continue
# Check if it is a tag end.
if tagdata.type in (tagdata.END, tagdata.SPECIAL):
if self.raw_mode:
if tagdata.name != self.queue[0].name:
self.put_text(tagdata.full)
continue
if self.raw_deg > 0:
self.raw_deg -= 1
continue
pos = 0
if not tagdata.name:
if queue:
pos = 0
else:
if not queue:
self.put_text(tagdata.full)
continue
else:
@ -164,6 +178,7 @@ class Translator:
if tagdata.type == tagdata.END:
self.put_text(tagdata.full)
continue
pos = -1
while pos >= 0:
self.pop_tag()
@ -171,7 +186,16 @@ class Translator:
continue
# If it is a beginning tag, try to get it.
# It is a beginning tag, process it as it is.
if self.raw_mode:
if tagdata.name != self.queue[0].name:
self.put_text(tagdata.full)
continue
self.raw_deg += 1
continue
tag = get_tag(tagdata.name, tagdata.value, 'html')
if not tag:
self.put_text(tagdata.full)