|
|
|
@ -1,13 +1,10 @@
|
|
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
from __future__ import unicode_literals |
|
|
|
|
|
|
|
|
|
import codecs |
|
|
|
|
import translitcodec # provides 'translit/long', used by codecs.encode() |
|
|
|
|
import html |
|
|
|
|
import re |
|
|
|
|
import six |
|
|
|
|
import translitcodec # provides 'translit/long', used by codecs.encode() # noqa |
|
|
|
|
|
|
|
|
|
from django.conf import settings |
|
|
|
|
from django.utils.encoding import force_str, smart_str |
|
|
|
|
from django.utils.encoding import force_str |
|
|
|
|
from django.utils.functional import keep_lazy_text |
|
|
|
|
from django.utils.html import mark_safe |
|
|
|
|
from django.utils.text import slugify as django_slugify |
|
|
|
@ -43,20 +40,8 @@ SLUGIFY_FUNCTION = getattr(settings, 'SLUGIFY_FUNCTION', downgrading_slugify)
|
|
|
|
|
slugify = SLUGIFY_FUNCTION |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if six.PY2: |
|
|
|
|
import bs4 |
|
|
|
|
|
|
|
|
|
def html_entities_to_unicode(html): |
|
|
|
|
# An incoming HTML or XML entity is always converted into the corresponding Unicode character in bs4 |
|
|
|
|
return smart_str(bs4.BeautifulSoup(html), 'lxml') |
|
|
|
|
|
|
|
|
|
else: |
|
|
|
|
import html |
|
|
|
|
|
|
|
|
|
# Works only with Python >= 3.4 |
|
|
|
|
def html_entities_to_unicode(html_str): |
|
|
|
|
return html.unescape(html_str) |
|
|
|
|
# html_entities_to_unicode = allow_lazy(html_entities_to_unicode, six.text_type, SafeText) |
|
|
|
|
def html_entities_to_unicode(html_str): |
|
|
|
|
return html.unescape(html_str) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Translators: Separator between list elements |
|
|
|
@ -67,7 +52,7 @@ LAST_WORD_SEPARATOR = gettext_lazy(" and ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@keep_lazy_text |
|
|
|
|
def get_text_joined(list_, separator=DEFAULT_SEPARATOR, last_word=LAST_WORD_SEPARATOR): |
|
|
|
|
def text_joined(list_, separator=DEFAULT_SEPARATOR, last_word=LAST_WORD_SEPARATOR): |
|
|
|
|
list_ = list(list_) |
|
|
|
|
if len(list_) == 0: |
|
|
|
|
return '' |
|
|
|
@ -78,16 +63,19 @@ def get_text_joined(list_, separator=DEFAULT_SEPARATOR, last_word=LAST_WORD_SEPA
|
|
|
|
|
force_str(last_word), force_str(list_[-1])) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO Don't match escaped stars (like \*) |
|
|
|
|
|
|
|
|
|
b_pattern = re.compile(r"(\*\*)(.*?)\1") |
|
|
|
|
i_pattern = re.compile(r"(\*)(.*?)\1") |
|
|
|
|
u_pattern = re.compile(r"(__)(.*?)\1") |
|
|
|
|
link_pattern = re.compile(r"\[([^\[]+)\]\(([^\)]+)\)") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@keep_lazy_text |
|
|
|
|
def slimdown(text): |
|
|
|
|
""" |
|
|
|
|
Converts simplified markdown (`**`, `*`, `__`) to <b>, <i> und <u> tags. |
|
|
|
|
""" |
|
|
|
|
b_pattern = re.compile(r"(\*\*)(.*?)\1") |
|
|
|
|
i_pattern = re.compile(r"(\*)(.*?)\1") |
|
|
|
|
u_pattern = re.compile(r"(__)(.*?)\1") |
|
|
|
|
link_pattern = re.compile(r"\[([^\[]+)\]\(([^\)]+)\)") |
|
|
|
|
|
|
|
|
|
if text: |
|
|
|
|
text, n = re.subn(b_pattern, "<b>\\2</b>", text) |
|
|
|
|
text, n = re.subn(i_pattern, "<i>\\2</i>", text) |
|
|
|
|