2
0
Fork 0

Corrected 'process' into 'content', setup script and other few things, doesn't work correctly yet

This commit is contained in:
Thomas Touhey 2018-01-19 20:56:26 +01:00
parent 128ab62a8a
commit d50b4a0967
No known key found for this signature in database
GPG Key ID: 2ECEB0517AD947FB
6 changed files with 120 additions and 22 deletions

View File

@ -9,7 +9,7 @@ setup(name='textoutpc',
description='Textout() equivalent from Planète Casio',
author='Thomas "Cakeisalie5" Touhey',
author_email='thomas@touhey.fr',
url='https://github.com/cakeisalie5/PlaneteCasioTextout',
url='https://forge.touhey.fr/pc/textout.git/',
keywords='planète casio textout bbcode',
packages=['textoutpc'],

View File

@ -82,8 +82,7 @@ __test_cases = {
# Quotes.
'[quote=Test 1 :)]lel[/quote]': \
'<div class="citation"><b>Test 1 ' \
'<img src="/images/smileys/smile.gif"> a écrit :</b><br />lel</div>'
'<div class="citation"><b>Test 1 :) a écrit:</b><br />lel</div>'
# Paragraphs (that's the most hardcore tests, I've not even settled on
# what the result should be yet).

View File

@ -35,7 +35,7 @@ class TextoutQuoteTag(TextoutBlockTag):
def begin_html(self):
f = '<div class="citation">'
if self.value:
f += '<b>{} a écrit:</b> <br />'.format(self.value)
f += '<b>{} a écrit:</b><br />'.format(self.value)
return f
def end_html(self):

View File

@ -24,12 +24,13 @@ class TextoutVideoTag(TextoutRawBlockTag):
aliases = ('[video]', '[video tiny]')
def preprocess(self, cin):
self.type = None
self.w, self.h = (470, 300) if "tiny" in self.name else (560, 340)
def _getvideo(self, url):
""" Try to get the video type for preprocessing. """
content = cin.read()
url = _urlparse.urlparse(content)
if not url.scheme in ('http', 'https'):
raise Exception
if url.netloc == "youtu.be":
self.id = url.path[1:]
if not _hexcode.match(self.id):
@ -50,9 +51,21 @@ class TextoutVideoTag(TextoutRawBlockTag):
if not _numcode.match(self.code):
raise Exception
self.type = "vimeo"
elif not url.scheme in ('http', 'https') and not url.path:
raise Exception("No allowed prefix")
else:
raise Exception
def preprocess(self, cin):
self.w, self.h = (470, 300) if "tiny" in self.name else (560, 340)
content = cin.read()
try:
self._getvideo(content)
except:
url = _urlparse.urlparse(content)
if not url.scheme in ('http', 'https'):
raise Exception("No allowed prefix!")
self.type = None
self.url = content
def process_html(self):
@ -72,7 +85,7 @@ class TextoutVideoTag(TextoutRawBlockTag):
'frameborder="0" webkitAllowFullScreen allowFullScreen>' \
'</iframe>'.format(self.id, self.w, self.h)
else:
link = _htmlescape(self.url)
link = self.url.replace('"', '%22')
return '<a href="{}" target="_blank" rel="noopener">{}</a>' \
.format(link, link)

View File

@ -31,7 +31,8 @@ def get_tag(name, value, output_type = 'html'):
try:
als = _aliases[name]
return als(name, value, output_type)
als = als(name, value, output_type)
return als
except:
return None

View File

@ -1,10 +1,10 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" Main translation function.
!!! TODO SOME WORK IN PROGRESS !!!
See the `Translator` class documentation for more information.
"""
import regex as _re
from io import StringIO as _sio
from html import escape as _htmlescape
from .Stream import *
@ -12,6 +12,86 @@ from .Tags import TextoutRawTag, TextoutInlineTag, TextoutBlockTag, get_tag
__all__ = ["Translator"]
# ---
# Naked URLs regex.
# ---
def _NakedURLs_sub(m):
sp = m.groups('sp')
url = m.groups('url')
aft = ''
# Hack for the last comma.
if url[-1] == ',':
url, aft = url[:-1], ','
return '{}<a href="{}" target="_blank" rel="noopener">{}</a>{}' \
.format(sp, url, url, aft)
_NakedURLs_re = _re.compile(r"""\
(?P<sp>^|\s|[[:punct:]])
(?P<url>(https?|ftp):
(?P<ucore>[^\[\]\(\)\s]* (\[(?&ucore)\]?)* (\((?&ucore)\)?)*)*
)
""", _re.VERBOSE | _re.M)
# ---
# Smileys.
# ---
_Smileys = {
'>:)': '/images/smileys/twisted.gif',
'>:(': '/images/smileys/evil.gif',
':)': '/images/smileys/smile.gif',
';)': '/images/smileys/wink.gif',
':(': '/images/smileys/sad.gif',
':D': '/images/smileys/grin.gif',
':p': '/images/smileys/hehe.gif',
'8-)': '/images/smileys/cool2.gif',
':@': '/images/smileys/mad.gif',
'0_0': '/images/smileys/eek.gif',
':E': '/images/smileys/mrgreen.gif',
':O': '/images/smileys/shocked.gif',
':s': '/images/smileys/confused2.gif',
'^^': '/images/smileys/eyebrows.gif',
":'(": '/images/smileys/cry.gif',
# ':-°': ('/images/smileys/whistle.gif', 'height: 15px;'),
# Name-based smileys.
':lol:': '/images/smileys/lol.gif',
':oops:': '/images/smileys/confused2.gif',
':grr:': '/images/smileys/evil.gif',
':sry:': '/images/smileys/redface.gif',
':mmm:': '/images/smileys/rolleyes.gif',
':waza:': '/images/smileys/waza.gif',
# ':whistle:': ('/images/smileys/whistle.gif', 'height: 15px;'),
':here:': '/images/smileys/pointer.gif',
':bow:': '/images/smileys/bow.gif',
':cool:': '/images/smileys/cool.gif',
':good:': '/images/smileys/welldone.gif',
':love:': '/images/smileys/love.gif',
':aie:': '/images/smileys/banghead2.gif',
':cry:': '/images/smileys/cry.gif',
':facepalm:': '/images/smileys/facepalm.gif',
':argh:': '/images/smileys/insults.gif',
':?:': '/images/smileys/what.gif',
':!:': '/images/smileys/excl.gif',
':arrow:': '/images/smileys/here.gif',
':grin:': '/images/smileys/grin.gif',
}
def _Smiley_sub(m):
return m.group(1) + '<img src="{}">'.format(_Smileys[m.group(2)]) \
+ m.group(3)
_Smiley_re = _re.compile('(^|\\s)(' + '|'.join(map(_re.escape,
_Smileys.keys())) + ')(\\s|$)')
# ---
# Translator main class.
# ---
class Translator:
""" One-time usage class for translating.
Use it this way: `Translator(my_inp, my_outp).process()`.
@ -75,9 +155,14 @@ class Translator:
text = _htmlescape(self.text_group)
if not self.raw_mode:
# TODO: HTML conversions of naked URLs (not in [url] tags),
# smileys, etc.
pass
# Naked URL conversion.
text = _NakedURLs_re.sub(_NakedURLs_sub, text)
# Smiley conversion. Basically sub and replace.
text = _Smiley_re.sub(_Smiley_sub, text)
text = _Smiley_re.sub(_Smiley_sub, text)
return text
@ -200,21 +285,21 @@ class Translator:
if hasattr(tag, 'begin'):
self.put_code(tag.begin())
if hasattr(tag, 'content'):
self.put_code(tag.content())
if hasattr(tag, 'process'):
self.put_code(tag.process())
else:
self.put_text(content)
else:
# Even when there is no preprocessing, there might be some
# content replacing method, this is where we manage it.
if hasattr(tag, 'content'):
if hasattr(tag, 'process'):
# Tag replaces content without preprocessing, which means
# the content has been ignored and the tag only puts the
# things.
self.cign -= 1
self.put_code(tag.content())
self.put_code(tag.process())
# Don't forget to put the end of the tag, as well.