Merge pull request #780 from ae-utbm/remove-bbcode

remove doku/bbcode to markdown
This commit is contained in:
thomas girod 2024-08-10 16:04:10 +02:00 committed by GitHub
commit 8174bce720
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 2 additions and 195 deletions

View File

@ -190,7 +190,6 @@ or user.is_in_group(pk=settings.SITH_GROUP_PEDAGOGY_ADMIN_ID)
<div>
<h4>{% trans %}Other tools{% endtrans %}</h4>
<ul>
<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>
</div>

View File

@ -38,7 +38,6 @@ register_converter(BooleanStringConverter, "bool")
urlpatterns = [
path("", index, name="index"),
path("to_markdown/", ToMarkdownView.as_view(), name="to_markdown"),
path("notifications/", NotificationList.as_view(), name="notification_list"),
path("notification/<int:notif_id>/", notification, name="notification"),
# Search

View File

@ -13,7 +13,6 @@
#
#
import re
import subprocess
from datetime import date
@ -142,156 +141,6 @@ def exif_auto_rotate(image):
return image
def doku_to_markdown(text: str) -> str:
"""Convert doku text to the corresponding markdown.
Args:
text: the doku text to convert
Returns:
The converted markdown text
"""
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)
text = re.sub(r"<sub>(.*?)<\/sub>", r"_\1_", text) # Subscript (idem)
text = re.sub(r"^======(.*?)======", r"#\1", text, flags=re.MULTILINE) # Titles
text = re.sub(r"^=====(.*?)=====", r"##\1", text, flags=re.MULTILINE)
text = re.sub(r"^====(.*?)====", r"###\1", text, flags=re.MULTILINE)
text = re.sub(r"^===(.*?)===", r"####\1", text, flags=re.MULTILINE)
text = re.sub(r"^==(.*?)==", r"#####\1", text, flags=re.MULTILINE)
text = re.sub(r"^=(.*?)=", r"######\1", text, flags=re.MULTILINE)
text = re.sub(r"<nowiki>", r"<nosyntax>", text)
text = re.sub(r"</nowiki>", r"</nosyntax>", text)
text = re.sub(r"<code>", r"```\n", text)
text = re.sub(r"</code>", r"\n```", text)
text = re.sub(r"article://", r"page://", text)
text = re.sub(r"dfile://", r"file://", text)
i = 1
for fn in re.findall(r"\(\((.*?)\)\)", text): # Footnotes
text = re.sub(r"\(\((.*?)\)\)", r"[^%s]" % i, text, count=1)
text += "\n[^%s]: %s\n" % (i, fn)
i += 1
text = re.sub(r"\\{2,}[\s]", r" \n", text) # Carriage return
text = re.sub(r"\[\[(.*?)\|(.*?)\]\]", r"[\2](\1)", text) # Links
text = re.sub(r"\[\[(.*?)\]\]", r"[\1](\1)", text) # Links 2
text = re.sub(r"{{(.*?)\|(.*?)}}", r'![\2](\1 "\2")', text) # Images
text = re.sub(r"{{(.*?)(\|(.*?))?}}", r'![\1](\1 "\1")', text) # Images 2
text = re.sub(
r"{\[(.*?)(\|(.*?))?\]}", r"[\1](\1)", text
) # Video (transform to classic links, since we can't integrate them)
text = re.sub(r"###(\d*?)###", r"[[[\1]]]", text) # Progress bar
text = re.sub(
r"(\n +[^* -][^\n]*(\n +[^* -][^\n]*)*)", r"```\1\n```", text, flags=re.DOTALL
) # Block code without lists
text = re.sub(r"( +)-(.*)", r"1.\2", text) # Ordered lists
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 re.search(r"\A\s*\^(([^\^]*?)\^)*", line): # Table part
line = line.replace("^", "|")
new_text.append("> " * quote_level + line)
new_text.append(
"> " * quote_level + "|---|"
) # Don't keep the text alignement in tables it's really too complex for what it's worth
elif 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)
def bbcode_to_markdown(text):
"""Convert bbcode text to the corresponding markdown.
Args:
text: the bbcode text to convert
Returns:
The converted markdown text
"""
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)
def get_client_ip(request: HttpRequest) -> str | None:
headers = (
"X_FORWARDED_FOR", # Common header for proixes

View File

@ -17,7 +17,7 @@
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
@ -32,12 +32,11 @@ from django.http import JsonResponse
from django.shortcuts import redirect, render
from django.utils import html
from django.utils.text import slugify
from django.views.generic import ListView, TemplateView
from django.views.generic import ListView
from haystack.query import SearchQuerySet
from club.models import Club
from core.models import Notification, User
from core.utils import bbcode_to_markdown, doku_to_markdown
def index(request, context=None):
@ -128,26 +127,3 @@ def search_json(request):
"clubs": search_club(request.GET.get("query", ""), as_json=True),
}
return JsonResponse(result)
class ToMarkdownView(TemplateView):
template_name = "core/to_markdown.jinja"
def post(self, request, *args, **kwargs):
self.text = request.POST["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().get_context_data(**kwargs)
try:
kwargs["text"] = self.text
kwargs["text_md"] = self.text_md
except:
kwargs["text"] = ""
kwargs["text_md"] = ""
return kwargs

View File

@ -3028,22 +3028,6 @@ msgstr "Utilisateurs"
msgid "Clubs"
msgstr "Clubs"
#: core/templates/core/to_markdown.jinja:4
msgid "To Markdown"
msgstr "Vers Markdown"
#: core/templates/core/to_markdown.jinja:15
msgid "Convert"
msgstr "Convertir"
#: core/templates/core/to_markdown.jinja:18
msgid "Markdown"
msgstr "Markdown"
#: core/templates/core/to_markdown.jinja:22
msgid "Render"
msgstr "Rendu"
#: core/templates/core/user_account.jinja:8
msgid "Year"
msgstr "Année"