Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajout traitement balises #70

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
16 changes: 16 additions & 0 deletions lalf/bb.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,22 @@ def _dump_(self, sqlfile):
sqlfile.set_config("newest_user_id", newest_user.newid)
sqlfile.set_config("newest_username", newest_user.name)
sqlfile.set_config("newest_user_colour", newest_user.colour)

#Add user columns
#shouldn't they be located to another table ?
sqlfile.update_config("users", "user_sex", "varchar(1) default NULL")
sqlfile.update_config("users", "user_date_of_birth", "int(11) default NULL")

sqlfile.update_config("users", "user_twitter", "varchar(255)")
sqlfile.update_config("users", "user_facebook", "varchar(255)")
sqlfile.update_config("users", "user_skype", "varchar(255)")

#Own columns (specific to my own forum)
sqlfile.update_config("users", "user_nom_bat", "varchar(255)")
sqlfile.update_config("users", "user_modele_bat", "varchar(255)")
sqlfile.update_config("users", "user_port_bat", "varchar(255)")
sqlfile.update_config("users", "user_mmsi_bat", "varchar(255)")


# Add bbcodes tags
for bbcode in phpbb.BBCODES:
Expand Down
10 changes: 8 additions & 2 deletions lalf/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ def _export_(self):

for element in document.find("a"):
match = pattern.fullmatch(clean_url(element.get("href", "")))
if match:
user = self.users[int(match.group(1))]
if match:
try:
user = self.users[int(match.group(1))]
except KeyError:
self.logger.debug('Erreur de numéro de groupe %d', match.group(1))
print('Erreur de numéro de groupe')
print(match.group)

if self.group not in user.groups:
user.groups.append(self.group)

Expand Down
70 changes: 68 additions & 2 deletions lalf/htmltobbcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Module handling the conversion of html to bbcode (as stored in the phpbb database)
"""

import urllib
import logging
import re
from html.parser import HTMLParser
Expand Down Expand Up @@ -90,7 +91,16 @@ def process_link(bb, url):
if newurl:
url = newurl
else:
logger.warning("Le lien suivant n'a pas pu être réécrit : %s", url)
# Extracting externals image link using /viewimage/ such as viewimage.forum?u=
match = re.search(r"viewimage.forum\?u=(.*)", url)
if match:
url = urllib.parse.unquote(match.group(1))
#logger.warning("Le lien suivant réécrit : %s", url)
else:
# ignore admin to delete/ip/post/editpost post links
match = re.search(r"mode=", url)
if not match:
logger.warning("Le lien suivant n'a pas pu être réécrit : %s", url)

return url

Expand Down Expand Up @@ -348,6 +358,38 @@ def get_bbcode(self, fileobj, bb, uid=""):
else:
fileobj.write("[/{}{}]".format(self.tag.rstrip("="), uid))

#
# intial credit to Titou74
#  https://github.com/Roromis/Lalf-Forumactif/issues/56
# for Iframe
#
class IframeTagNode(Node):
"""
A node representing an inline element
"""
def __init__(self, tag, attrs="", closing_tag=None, content=""):
Node.__init__(self, tag)
self.closing_tag = closing_tag
self.attrs = attrs

if content:
self.add_text(content)

def get_bbcode(self, fileobj, bb, uid=""):
if self.tag not in TAGS:
logger = logging.getLogger('lalf.htmltobbcode')
logger.warning("La balise bbcode [%s] n'est pas supportée.", self.tag)

Node.get_bbcode(self, fileobj, bb, uid)
else:
fileobj.write("[{}{}{}]".format(self.tag, self.attrs, uid))
Node.get_bbcode(self, fileobj, bb, uid)
if self.closing_tag:
fileobj.write("[/{}{}]".format(self.closing_tag, uid))
else:
fileobj.write("[/{}{}]".format(self.tag.rstrip("="), uid))


class BlockTagNode(InlineTagNode):
"""
A node representing an block element
Expand Down Expand Up @@ -486,7 +528,10 @@ def get_bbcode(self, fileobj, bb, uid=""):
fileobj.write('<!-- m --><a class="postlink" href="{}">{}</a><!-- m -->'
.format(url, ellipsized_url))

@Parser.handler("i", "u", "strike", "sub", "sup", "hr", "tr")
#
# initial credit to Titou74
# https://github.com/Roromis/Lalf-Forumactif/issues/56
@Parser.handler("i", "u", "strike", "sub", "sup", "hr", "tr", "h2", "h3", "h4")
def _inline_handler(tag, attrs):
return InlineTagNode(tag)

Expand All @@ -500,6 +545,26 @@ def _td_handler(tag, attrs):
else:
return InlineTagNode("td=", "{},{}".format(colspan, rowspan))


#
# intial credit to Titou74
#  https://github.com/Roromis/Lalf-Forumactif/issues/56
# for Iframe
@Parser.handler("iframe")
def _iframe_handler(tag, attrs):
logger = logging.getLogger('lalf.htmltobbcode')
try:
if attrs["src"][:30] == "https://www.youtube.com/embed/":
logger.warning("Youtube %s", attrs["src"][30:len(attrs["src"])])
return IframeTagNode("youtube", content=attrs["src"][30:len(attrs["src"])])
if attrs["src"][:39] == "http://www.dailymotion.com/embed/video/":
logger.warning("DailyMotion %s", attrs["src"][39:len(attrs["src"])])
return IframeTagNode("dailymotion", content=attrs["src"][39:len(attrs["src"])])
except KeyError:
logger.warning("Unknown Iframe %s", ''.join(attrs))

return InlineTagNode(tag)

@Parser.handler("strong")
def _strong_handler(tag, attrs):
return InlineTagNode("b")
Expand Down Expand Up @@ -596,3 +661,4 @@ def _marquee_handler(tag, attrs):
return InlineTagNode("updown")
else:
return InlineTagNode("scroll")

22 changes: 14 additions & 8 deletions lalf/ocrusers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
optical character recognition to export them anyway.
"""

import sys
import os
import re
import time
Expand Down Expand Up @@ -173,14 +174,19 @@ def _export_(self):
# yet, the email is available
self.trust = 3

lastvisit = e("td").eq(4).text()
if lastvisit != "":
lastvisit = lastvisit.split(" ")
self.lastvisit = int(time.mktime(time.struct_time(
(int(lastvisit[2]), month(lastvisit[1]), int(lastvisit[0]), 0, 0, 0, 0, 0,
0))))
else:
self.lastvisit = 0
try:
lastvisit = e("td").eq(4).text()
if lastvisit != "":
lastvisit = lastvisit.split(" ")
self.lastvisit = int(time.mktime(time.struct_time(
(int(lastvisit[2]), month(lastvisit[1]), int(lastvisit[0]), 0, 0, 0, 0, 0,
0))))
else:
self.lastvisit = 0
except IndexError:
selft.logger.error("Configuration du profil de l'administrateur choisi à revoir cf https://roromis.github.io/Lalf-Forumactif/annexes/profil")
sys.exit("Erreur de configuration du forum")


def confirm_email(self, retries=2):
"""
Expand Down
62 changes: 62 additions & 0 deletions lalf/phpbb.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,68 @@
"first_pass_replace" : '\'[spoiler:$uid]\'.str_replace(array(\"\\r\\n\", \'\\\"\', \'\\\'\', \'(\', \')\'), array(\"\\n\", \'\"\', \'&#39;\', \'&#40;\', \'&#41;\'), trim(\'${1}\')).\'[/spoiler:$uid]\'',
"second_pass_match" : '!\\[spoiler:$uid\\](.*?)\\[/spoiler:$uid\\]!s',
"second_pass_replace" : '<dl class=\"codebox\"><dt>Spoiler: <a href=\"#\" onclick=\"var content = this.parentNode.parentNode.getElementsByTagName(\'dd\')[0]; if (content.style.display != \'\') { content.style.display = \'\'; this.innerText = \'Cacher\'; this.value = \'Hide\'; } else { content.style.display = \'none\'; this.innerText = \'Afficher\'; }; return false;\">Afficher</a></dt><dd style=\"display: none;\">${1}</dd></dl>'},


#
# initial credit : Titou74 11/9/2016
# https://github.com/Roromis/Lalf-Forumactif/issues/56#issuecomment-246206547
# h2, h3, h4, youtube, dailymotion
#
{"bbcode_id" : 29,
"bbcode_tag" : 'h2',
"bbcode_helpline" : 'H2',
"display_on_posting" : 0,
"bbcode_match" : '[h2]{TEXT}[/h2]',
"bbcode_tpl" : '<h2 class="post-content">{TEXT}</h2>',
"first_pass_match" : '!\\[h2\\](.*?)\\[/h2\\]!ies',
"first_pass_replace" : '\'[h2:$uid]\'.str_replace(array(\"\\r\\n\", \'\\\"\', \'\\\'\', \'(\', \')\'), array(\"\\n\", \'\"\', \'&#39;\', \'&#40;\', \'&#41;\'), trim(\'${1}\')).\'[/h2:$uid]\'',
"second_pass_match" : '!\\[h2:$uid\\](.*?)\\[/h2:$uid\\]!s',
"second_pass_replace" : '<h2 class=\"post-content\">${1}</h2>'},

{"bbcode_id" : 30,
"bbcode_tag" : 'h3',
"bbcode_helpline" : 'H3',
"display_on_posting" : 0,
"bbcode_match" : '[h3]{TEXT}[/h3]',
"bbcode_tpl" : '<h3 class="post-content">{TEXT}</h3>',
"first_pass_match" : '!\\[h3\\](.*?)\\[/h3\\]!ies',
"first_pass_replace" : '\'[h3:$uid]\'.str_replace(array(\"\\r\\n\", \'\\\"\', \'\\\'\', \'(\', \')\'), array(\"\\n\", \'\"\', \'&#39;\', \'&#40;\', \'&#41;\'), trim(\'${1}\')).\'[/h3:$uid]\'',
"second_pass_match" : '!\\[h3:$uid\\](.*?)\\[/h3:$uid\\]!s',
"second_pass_replace" : '<h3 class=\"post-content\">${1}</h3>'},

{"bbcode_id" : 31,
"bbcode_tag" : 'h4',
"bbcode_helpline" : 'H4',
"display_on_posting" : 0,
"bbcode_match" : '[h4]{TEXT}[/h4]',

"bbcode_tpl" : '<h4 class="post-content">{TEXT}</h4>',
"first_pass_match" : '!\\[h4\\](.*?)\\[/h4\\]!ies',
"first_pass_replace" : '\'[h4:$uid]\'.str_replace(array(\"\\r\\n\", \'\\\"\', \'\\\'\', \'(\', \')\'), array(\"\\n\", \'\"\', \'&#39;\', \'&#40;\', \'&#41;\'), trim(\'${1}\')).\'[/h4:$uid]\'',
"second_pass_match" : '!\\[h4:$uid\\](.*?)\\[/h4:$uid\\]!s',
"second_pass_replace" : '<h4 class=\"post-content\">${1}</h4>'},

{"bbcode_id" : 32,
"bbcode_tag" : 'youtube',
"bbcode_helpline" : 'Youtube',
"display_on_posting" : 0,
"bbcode_match" : '[youtube]{TEXT}[/youtube]',
"bbcode_tpl" : '<iframe src="https://www.youtube.com/embed/{TEXT}\" allowfullscreen frameborder="0" height="315" width="560"></iframe>',
"first_pass_match" : '!\\[youtube\\](.*?)\\[/youtube\\]!ies',
"first_pass_replace" : '\'[youtube:$uid]${1}[/youtube:$uid]\'',
"second_pass_match" : '!\\[youtube:$uid\\](.*?)\\[/youtube:$uid\\]!s',
"second_pass_replace" : '<iframe src=\"https://www.youtube.com/embed/${1}\" allowfullscreen=\"\" frameborder=\"0\" height=\"315\" width=\"560\"></iframe>'},

{"bbcode_id" : 33,
"bbcode_tag" : 'dailymotion',
"bbcode_helpline" : 'Dailymotion',
"display_on_posting" : 0,
"bbcode_match" : '[dailymotion]{TEXT}[/dailymotion]',
"bbcode_tpl" : '<iframe src="http://www.dailymotion.com/video/embed/{TEXT}\" allowfullscreen="" frameborder="0" height="270" width="480"></iframe>',
"first_pass_match" : '!\\[dailymotion\\](.*?)\\[/dailymotion\\]!ies',
"first_pass_replace" : '\'[dailymotion:$uid]\'.str_replace(array(\"\\r\\n\", \'\\\"\', \'\\\'\', \'(\', \')\'), array(\"\\n\", \'\"\', \'&#39;\', \'&#40;\', \'&#41;\'), trim(\'${1}\')).\'[/dailymotion:$uid]\'',
"second_pass_match" : '!\\[dailymotion:$uid\\](.*?)\\[/dailymotion:$uid\\]!s',
"second_pass_replace" : '<iframe src=\"http://www.dailymotion.com/embed/video/embed/${1}\" allowfullscreen=\"\" frameborder=\"0\" height=\"270\" width=\"480\"></iframe>'},
]

BOTS = [
Expand Down
4 changes: 2 additions & 2 deletions lalf/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def connect(self):
"""
Connect to the forum and initialize session, sid and tid.
"""
self.logger.debug('Connection au forum')
self.logger.debug('Connexion au forum')

# Reset session
self.session.close()
Expand All @@ -101,7 +101,7 @@ def connect(self):
self.sid = value

if self.sid is None:
self.logger.critical('Échec de la connection.')
self.logger.critical('Échec de la connexion.')
raise UnableToConnect()

if self.tid is None:
Expand Down
11 changes: 11 additions & 0 deletions lalf/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,14 @@ def set_config(self, name, value):
prefix=self.prefix,
value=escape(str(value)),
name=escape(name)))

def update_config(self, table, name, definition):
"""
Update a value in the phpbb_config table
"""
self.fileobj.write(
"ALTER TABLE {prefix}{table} ADD {name} {definition} ;\n".format(
prefix=self.prefix,
table=table,
name=escape(name),
definition=escape(definition)))
2 changes: 1 addition & 1 deletion lalf/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(self, topic_id, topic_type, title, locked, views):
self.views = views

def _export_(self):
self.logger.info('Récupération du sujet %d', self.topic_id)
self.logger.info('Récupération du sujet %d -> %s', self.topic_id, self.title.ljust(15)[:15].strip())

self.root.current_topics += 1
self.ui.update()
Expand Down
Loading