|
@@ -3,13 +3,12 @@
|
|
flaskbb.utils.markup
|
|
flaskbb.utils.markup
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
- A module for all markup related stuff like markdown and emojis.
|
|
|
|
|
|
+ A module for all markup related stuff.
|
|
|
|
|
|
:copyright: (c) 2016 by the FlaskBB Team.
|
|
:copyright: (c) 2016 by the FlaskBB Team.
|
|
:license: BSD, see LICENSE for more details.
|
|
:license: BSD, see LICENSE for more details.
|
|
"""
|
|
"""
|
|
import logging
|
|
import logging
|
|
-import os
|
|
|
|
import re
|
|
import re
|
|
|
|
|
|
from flask import url_for
|
|
from flask import url_for
|
|
@@ -24,60 +23,18 @@ from pygments.util import ClassNotFound
|
|
logger = logging.getLogger(__name__)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
-_re_emoji = re.compile(r':([a-z0-9\+\-_]+):', re.I)
|
|
|
|
_re_user = re.compile(r'@(\w+)', re.I)
|
|
_re_user = re.compile(r'@(\w+)', re.I)
|
|
|
|
|
|
-# base directory of flaskbb - used to collect the emojis
|
|
|
|
-_basedir = os.path.join(os.path.abspath(os.path.dirname(
|
|
|
|
- os.path.dirname(__file__))))
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-def collect_emojis():
|
|
|
|
- """Returns a dictionary containing all emojis with their
|
|
|
|
- name and filename. If the folder doesn't exist it returns a empty
|
|
|
|
- dictionary.
|
|
|
|
- """
|
|
|
|
- emojis = dict()
|
|
|
|
- full_path = os.path.join(_basedir, "static", "emoji")
|
|
|
|
- # return an empty dictionary if the path doesn't exist
|
|
|
|
- if not os.path.exists(full_path):
|
|
|
|
- return emojis
|
|
|
|
-
|
|
|
|
- for emoji in os.listdir(full_path):
|
|
|
|
- name, ending = emoji.split(".")
|
|
|
|
- if ending in ["png", "gif", "jpg", "jpeg"]:
|
|
|
|
- emojis[name] = emoji
|
|
|
|
-
|
|
|
|
- return emojis
|
|
|
|
-
|
|
|
|
-EMOJIS = collect_emojis()
|
|
|
|
-
|
|
|
|
|
|
|
|
class FlaskBBRenderer(mistune.Renderer):
|
|
class FlaskBBRenderer(mistune.Renderer):
|
|
- """Markdown with some syntetic sugar such as @user gets linked to the
|
|
|
|
- user's profile and emoji support.
|
|
|
|
|
|
+ """Markdown with some syntactic sugar, such as @user gettting linked
|
|
|
|
+ to the user's profile.
|
|
"""
|
|
"""
|
|
def __init__(self, **kwargs):
|
|
def __init__(self, **kwargs):
|
|
super(FlaskBBRenderer, self).__init__(**kwargs)
|
|
super(FlaskBBRenderer, self).__init__(**kwargs)
|
|
|
|
|
|
def paragraph(self, text):
|
|
def paragraph(self, text):
|
|
- """Rendering paragraph tags. Like ``<p>`` with emoji support."""
|
|
|
|
-
|
|
|
|
- def emojify(match):
|
|
|
|
- value = match.group(1)
|
|
|
|
-
|
|
|
|
- if value in EMOJIS:
|
|
|
|
- filename = url_for(
|
|
|
|
- "static",
|
|
|
|
- filename="emoji/{}".format(EMOJIS[value])
|
|
|
|
- )
|
|
|
|
-
|
|
|
|
- emoji = "<img class='{css}' alt='{alt}' src='{src}' />".format(
|
|
|
|
- css="emoji", alt=value,
|
|
|
|
- src=filename
|
|
|
|
- )
|
|
|
|
- return emoji
|
|
|
|
- return match.group(0)
|
|
|
|
|
|
+ """Render paragraph tags, autolinking user handles."""
|
|
|
|
|
|
def userify(match):
|
|
def userify(match):
|
|
value = match.group(1)
|
|
value = match.group(1)
|
|
@@ -87,7 +44,6 @@ class FlaskBBRenderer(mistune.Renderer):
|
|
)
|
|
)
|
|
return user
|
|
return user
|
|
|
|
|
|
- text = _re_emoji.sub(emojify, text)
|
|
|
|
text = _re_user.sub(userify, text)
|
|
text = _re_user.sub(userify, text)
|
|
|
|
|
|
return '<p>%s</p>\n' % text.strip(' ')
|
|
return '<p>%s</p>\n' % text.strip(' ')
|