Commit 136d0f3f authored by Skia's avatar Skia

Add basic BBcode translator

Signed-off-by: Skia's avatarSkia <skia@libskia.so>
parent 32ac6640
{% extends "core/base.jinja" %}
{% block title %}
{% trans %}Doku to Markdown{% endtrans %}
{% trans %}To Markdown{% endtrans %}
{% endblock %}
{% block content %}
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="radio" name="syntax" value="doku" {% if request.POST['syntax'] != "bbcode" %}checked{% endif %} >Doku</input>
<input type="radio" name="syntax" value="bbcode" {% if request.POST['syntax'] == "bbcode" %}checked{% endif %} >BBCode</input>
<textarea name="text" id="text" rows="30" cols="80">
{{- text -}}
</textarea>
......
......@@ -108,7 +108,7 @@
</ul>
<h4>{% trans %}Other tools{% endtrans %}</h4>
<ul>
<li><a href="{{ url('core:doku_to_markdown') }}">{% trans %}Convert dokuwiki syntax to Markdown{% endtrans %}</a></li>
<li><a href="{{ url('core:to_markdown') }}">{% trans %}Convert dokuwiki/BBcode syntax to Markdown{% endtrans %}</a></li>
<li><a href="{{ url('trombi:user_tools') }}">{% trans %}Trombi tools{% endtrans %}</a></li>
</ul>
......
......@@ -28,7 +28,7 @@ from core.views import *
urlpatterns = [
url(r'^$', index, name='index'),
url(r'^doku_to_markdown$', DokuToMarkdownView.as_view(), name='doku_to_markdown'),
url(r'^to_markdown$', ToMarkdownView.as_view(), name='to_markdown'),
url(r'^notifications$', NotificationList.as_view(), name='notification_list'),
url(r'^notification/(?P<notif_id>[0-9]+)$', notification, name='notification'),
......
......@@ -66,6 +66,7 @@ def exif_auto_rotate(image):
return image
def doku_to_markdown(text):
"""This is a quite correct doku translator"""
text = re.sub(r'([^:]|^)\/\/(.*?)\/\/', r'*\2*', text) # Italic (prevents protocol:// conflict)
text = re.sub(r'<del>(.*?)<\/del>', r'~~\1~~', text, flags=re.DOTALL) # Strike (may be multiline)
text = re.sub(r'<sup>(.*?)<\/sup>', r'^\1^', text) # Superscript (multiline not supported, because almost never used)
......@@ -133,3 +134,44 @@ def doku_to_markdown(text):
return "\n".join(new_text)
def bbcode_to_markdown(text):
"""This is a very basic BBcode translator"""
text = re.sub(r'\[b\](.*?)\[\/b\]', r'**\1**', text, flags=re.DOTALL) # Bold
text = re.sub(r'\[i\](.*?)\[\/i\]', r'*\1*', text, flags=re.DOTALL) # Italic
text = re.sub(r'\[u\](.*?)\[\/u\]', r'__\1__', text, flags=re.DOTALL) # Underline
text = re.sub(r'\[s\](.*?)\[\/s\]', r'~~\1~~', text, flags=re.DOTALL) # Strike (may be multiline)
text = re.sub(r'\[strike\](.*?)\[\/strike\]', r'~~\1~~', text, flags=re.DOTALL) # Strike 2
text = re.sub(r'article://', r'page://', text)
text = re.sub(r'dfile://', r'file://', text)
text = re.sub(r'\[url=(.*?)\](.*)\[\/url\]', r'[\2](\1)', text) # Links
text = re.sub(r'\[url\](.*)\[\/url\]', r'\1', text) # Links 2
text = re.sub(r'\[img\](.*)\[\/img\]', r'![\1](\1 "\1")', text) # Images
new_text = []
quote_level = 0
for line in text.splitlines(): # Tables and quotes
enter = re.finditer(r'\[quote(=(.+?))?\]', line)
quit = re.finditer(r'\[/quote\]', line)
if enter or quit: # Quote part
for quote in enter: # Enter quotes (support multiple at a time)
quote_level += 1
try:
new_text.append("> " * quote_level + "##### " + quote.group(2))
except:
new_text.append("> " * quote_level)
line = line.replace(quote.group(0), '')
final_quote_level = quote_level # Store quote_level to use at the end, since it will be modified during quit iteration
final_newline = False
for quote in quit: # Quit quotes (support multiple at a time)
line = line.replace(quote.group(0), '')
quote_level -= 1
final_newline = True
new_text.append("> " * final_quote_level + line) # Finally append the line
if final_newline: new_text.append("\n") # Add a new line to ensure the separation between the quote and the following text
else:
new_text.append(line)
return "\n".join(new_text)
......@@ -37,7 +37,7 @@ from itertools import chain
from haystack.query import SearchQuerySet
from core.models import User, Notification
from core.utils import doku_to_markdown
from core.utils import doku_to_markdown, bbcode_to_markdown
from club.models import Club
def index(request, context=None):
......@@ -98,17 +98,20 @@ def search_json(request):
}
return JsonResponse(result)
class DokuToMarkdownView(TemplateView):
template_name = "core/doku_to_markdown.jinja"
class ToMarkdownView(TemplateView):
template_name = "core/to_markdown.jinja"
def post(self, request, *args, **kwargs):
self.text = request.POST['text']
self.text_md = doku_to_markdown(self.text)
if request.POST['syntax'] == "doku":
self.text_md = doku_to_markdown(self.text)
else:
self.text_md = bbcode_to_markdown(self.text)
context = self.get_context_data(**kwargs)
return self.render_to_response(context)
def get_context_data(self, **kwargs):
kwargs = super(DokuToMarkdownView, self).get_context_data(**kwargs)
kwargs = super(ToMarkdownView, self).get_context_data(**kwargs)
try:
kwargs['text'] = self.text
kwargs['text_md'] = self.text_md
......
This diff is collapsed.
......@@ -45,7 +45,7 @@ from django.core.files import File
from core.models import User, SithFile
from core.utils import doku_to_markdown
from core.utils import doku_to_markdown, bbcode_to_markdown
from club.models import Club, Membership
from counter.models import Customer, Counter, Selling, Refilling, Product, ProductType, Permanency, Eticket
from subscription.models import Subscription
......@@ -1279,7 +1279,7 @@ def migrate_forum():
if r['syntaxengine_message'] == "doku":
msg.message = doku_to_markdown(to_unicode(r['contenu_message']))
else:
msg.message = to_unicode(r['contenu_message'])
msg.message = bbcode_to_markdown(to_unicode(r['contenu_message']))
msg.save()
except Exception as e:
print(" FAIL to migrate message: %s" % (repr(e)))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment