From 7a0fa9f1a0e4c20f554a6b4e5b3af182913aad12 Mon Sep 17 00:00:00 2001 From: thomas girod Date: Sat, 10 Aug 2024 14:23:01 +0200 Subject: [PATCH] remove doku/bbcode to markdown --- core/templates/core/user_tools.jinja | 1 - core/urls.py | 1 - core/utils.py | 151 --------------------------- core/views/site.py | 28 +---- locale/fr/LC_MESSAGES/django.po | 16 --- 5 files changed, 2 insertions(+), 195 deletions(-) diff --git a/core/templates/core/user_tools.jinja b/core/templates/core/user_tools.jinja index fed40176..340b619b 100644 --- a/core/templates/core/user_tools.jinja +++ b/core/templates/core/user_tools.jinja @@ -190,7 +190,6 @@ or user.is_in_group(pk=settings.SITH_GROUP_PEDAGOGY_ADMIN_ID)

{% trans %}Other tools{% endtrans %}

diff --git a/core/urls.py b/core/urls.py index a776cb94..5a62ed65 100644 --- a/core/urls.py +++ b/core/urls.py @@ -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//", notification, name="notification"), # Search diff --git a/core/utils.py b/core/utils.py index b50578b0..5ed6cb1b 100644 --- a/core/utils.py +++ b/core/utils.py @@ -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>", r"~~\1~~", text, flags=re.DOTALL - ) # Strike (may be multiline) - text = re.sub( - r"(.*?)<\/sup>", r"^\1^", text - ) # Superscript (multiline not supported, because almost never used) - text = re.sub(r"(.*?)<\/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"", r"", text) - text = re.sub(r"", r"", text) - text = re.sub(r"", r"```\n", text) - text = re.sub(r"", 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 diff --git a/core/views/site.py b/core/views/site.py index 67c628d8..82d7f0cf 100644 --- a/core/views/site.py +++ b/core/views/site.py @@ -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 diff --git a/locale/fr/LC_MESSAGES/django.po b/locale/fr/LC_MESSAGES/django.po index 7747bdc6..dab55769 100644 --- a/locale/fr/LC_MESSAGES/django.po +++ b/locale/fr/LC_MESSAGES/django.po @@ -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"