Add doku_to_markdown function

This commit is contained in:
Skia 2017-05-14 03:14:38 +02:00
parent 8c151fa498
commit 862a4619b3
6 changed files with 139 additions and 2 deletions

View File

@ -0,0 +1,27 @@
{% extends "core/base.jinja" %}
{% block title %}
{% trans %}Doku to Markdown{% endtrans %}
{% endblock %}
{% block content %}
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<textarea name="text" id="text" rows="30" cols="80">
{{- text -}}
</textarea>
<p><input type="submit" value="{% trans %}Convert{% endtrans %}" /></p>
</form>
<hr>
<h6>{% trans %}Markdown{% endtrans %}</h6>
<div style="border: 1px solid black; color: white; background: black; padding: 10px; margin: 5px;"><pre>{{- text_md -}}</pre>
</div>
<hr>
<h6>{% trans %}Render{% endtrans %}</h6>
<div style="border: 1px solid black; padding: 10px; margin: 5px;" class="page_content">
{{ text_md|markdown }}
</div>
{% endblock %}

View File

@ -108,6 +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('trombi:user_tools') }}">{% trans %}Trombi tools{% endtrans %}</a></li>
</ul>

View File

@ -28,6 +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'^notifications$', NotificationList.as_view(), name='notification_list'),
url(r'^notification/(?P<notif_id>[0-9]+)$', notification, name='notification'),

View File

@ -22,6 +22,8 @@
#
#
import re
# Image utils
from io import BytesIO
@ -62,3 +64,69 @@ def exif_auto_rotate(image):
image=image.rotate(90, expand=True)
return image
def doku_to_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'[\3](\1)', text) # Links
text = re.sub(r'{{(.*?)(\|(.*?))?}}', r'![\3](\1 "\3")', text) # Images
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))
line = line.replace(quote.group(0), '')
except:
new_text.append("> " * quote_level)
final_quote_level = quote_level # Store quote_level to use at the end, since it will be modified during quit iteration
for quote in quit: # Quit quotes (support multiple at a time)
line = line.replace(quote.group(0), '')
quote_level -= 1
new_text.append("> " * final_quote_level + line) # Finally append the line
else:
new_text.append(line)
return "\n".join(new_text)

View File

@ -28,7 +28,7 @@ from django.http import JsonResponse
from django.core import serializers
from django.db.models import Q
from django.contrib.auth.decorators import login_required
from django.views.generic import ListView
from django.views.generic import ListView, TemplateView
import os
import json
@ -37,6 +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 club.models import Club
def index(request, context=None):
@ -97,3 +98,22 @@ def search_json(request):
}
return JsonResponse(result)
class DokuToMarkdownView(TemplateView):
template_name = "core/doku_to_markdown.jinja"
def post(self, request, *args, **kwargs):
self.text = request.POST['text']
self.text_md = doku_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)
try:
kwargs['text'] = self.text
kwargs['text_md'] = self.text_md
except:
kwargs['text'] = ""
kwargs['text_md'] = ""
return kwargs

View File

@ -6,7 +6,7 @@
msgid ""
msgstr ""
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-05-13 18:31+0200\n"
"POT-Creation-Date: 2017-05-14 03:16+0200\n"
"PO-Revision-Date: 2016-07-18\n"
"Last-Translator: Skia <skia@libskia.so>\n"
"Language-Team: AE info <ae.info@utbm.fr>\n"
@ -1973,6 +1973,22 @@ msgstr "Confirmation"
msgid "Cancel"
msgstr "Annuler"
#: core/templates/core/doku_to_markdown.jinja:4
msgid "Doku to Markdown"
msgstr "Doku vers Markdown"
#: core/templates/core/doku_to_markdown.jinja:13
msgid "Convert"
msgstr "Convertir"
#: core/templates/core/doku_to_markdown.jinja:16
msgid "Markdown"
msgstr "Markdown"
#: core/templates/core/doku_to_markdown.jinja:20
msgid "Render"
msgstr "Rendu"
#: core/templates/core/edit.jinja:5 core/templates/core/edit.jinja.py:13
#: core/templates/core/file_edit.jinja:4
#: counter/templates/counter/cash_register_summary.jinja:4
@ -2678,6 +2694,10 @@ msgid "Other tools"
msgstr "Autres outils"
#: core/templates/core/user_tools.jinja:111
msgid "Convert dokuwiki syntax to Markdown"
msgstr "Convertir de la syntaxe dokuwiki vers Markdown"
#: core/templates/core/user_tools.jinja:112
msgid "Trombi tools"
msgstr "Outils Trombi"