2017-04-24 15:51:12 +00:00
|
|
|
# -*- coding:utf-8 -*
|
|
|
|
#
|
2023-04-04 17:17:44 +00:00
|
|
|
# Copyright 2023 © AE UTBM
|
|
|
|
# ae@utbm.fr / ae.info@utbm.fr
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 17:17:44 +00:00
|
|
|
# This file is part of the website of the UTBM Student Association (AE UTBM),
|
|
|
|
# https://ae.utbm.fr.
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 17:17:44 +00:00
|
|
|
# You can find the source code of the website at https://github.com/ae-utbm/sith3
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 17:17:44 +00:00
|
|
|
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
|
|
|
|
# SEE : https://raw.githubusercontent.com/ae-utbm/sith3/master/LICENSE
|
|
|
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2017-08-24 14:29:40 +00:00
|
|
|
import os
|
2016-12-24 00:41:49 +00:00
|
|
|
import re
|
2017-05-13 23:03:50 +00:00
|
|
|
from mistune import Renderer, InlineGrammar, InlineLexer, Markdown, escape, escape_link
|
2019-10-06 11:28:56 +00:00
|
|
|
from django.urls import reverse
|
2016-12-24 00:41:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SithRenderer(Renderer):
|
|
|
|
def file_link(self, id, suffix):
|
2018-10-04 19:29:19 +00:00
|
|
|
return reverse("core:file_detail", kwargs={"file_id": id}) + suffix
|
2016-12-24 00:41:49 +00:00
|
|
|
|
2016-12-27 18:40:31 +00:00
|
|
|
def exposant(self, text):
|
2017-02-24 01:04:39 +00:00
|
|
|
return """<sup>%s</sup>""" % text
|
2016-12-27 18:40:31 +00:00
|
|
|
|
|
|
|
def indice(self, text):
|
2017-02-24 01:04:39 +00:00
|
|
|
return """<sub>%s</sub>""" % text
|
2016-12-27 18:40:31 +00:00
|
|
|
|
|
|
|
def underline(self, text):
|
2017-06-10 19:39:56 +00:00
|
|
|
return """<u>%s</u>""" % text
|
2016-12-27 18:40:31 +00:00
|
|
|
|
2017-05-13 23:03:50 +00:00
|
|
|
def image(self, original_src, title, text):
|
|
|
|
"""Rendering a image with title and text.
|
|
|
|
:param src: source link of the image.
|
|
|
|
:param title: title text of the image.
|
|
|
|
:param text: alt text of the image.
|
|
|
|
"""
|
|
|
|
style = None
|
2018-10-04 19:29:19 +00:00
|
|
|
if "?" in original_src:
|
|
|
|
src, params = original_src.rsplit("?", maxsplit=1)
|
|
|
|
m = re.search(r"(\d+%?)(x(\d+%?))?", params)
|
2017-05-13 23:03:50 +00:00
|
|
|
if not m:
|
|
|
|
src = original_src
|
|
|
|
else:
|
|
|
|
width = m.group(1)
|
2018-10-04 19:29:19 +00:00
|
|
|
if not width.endswith("%"):
|
2017-06-12 07:42:03 +00:00
|
|
|
width += "px"
|
2017-05-13 23:03:50 +00:00
|
|
|
style = "width: %s; " % width
|
|
|
|
try:
|
|
|
|
height = m.group(3)
|
2018-10-04 19:29:19 +00:00
|
|
|
if not height.endswith("%"):
|
2017-06-12 07:42:03 +00:00
|
|
|
height += "px"
|
2017-05-13 23:03:50 +00:00
|
|
|
style += "height: %s; " % height
|
2017-06-12 07:42:03 +00:00
|
|
|
except:
|
|
|
|
pass
|
2017-05-13 23:03:50 +00:00
|
|
|
else:
|
|
|
|
params = None
|
|
|
|
src = original_src
|
|
|
|
src = escape_link(src)
|
|
|
|
text = escape(text, quote=True)
|
|
|
|
if title:
|
|
|
|
title = escape(title, quote=True)
|
|
|
|
html = '<img src="%s" alt="%s" title="%s"' % (src, text, title)
|
|
|
|
else:
|
|
|
|
html = '<img src="%s" alt="%s"' % (src, text)
|
|
|
|
if style:
|
|
|
|
html = '%s style="%s"' % (html, style)
|
2018-10-04 19:29:19 +00:00
|
|
|
if self.options.get("use_xhtml"):
|
|
|
|
return "%s />" % html
|
|
|
|
return "%s>" % html
|
2017-05-13 23:03:50 +00:00
|
|
|
|
2017-06-12 07:42:03 +00:00
|
|
|
|
2016-12-27 18:40:31 +00:00
|
|
|
class SithInlineGrammar(InlineGrammar):
|
2018-10-04 19:29:19 +00:00
|
|
|
double_emphasis = re.compile(r"^\*{2}([\s\S]+?)\*{2}(?!\*)") # **word**
|
|
|
|
emphasis = re.compile(r"^\*((?:\*\*|[^\*])+?)\*(?!\*)") # *word*
|
|
|
|
underline = re.compile(r"^_{2}([\s\S]+?)_{2}(?!_)") # __word__
|
|
|
|
exposant = re.compile(r"^<sup>([\s\S]+?)</sup>") # <sup>text</sup>
|
|
|
|
indice = re.compile(r"^<sub>([\s\S]+?)</sub>") # <sub>text</sub>
|
2016-12-27 18:40:31 +00:00
|
|
|
|
2017-06-12 07:42:03 +00:00
|
|
|
|
2016-12-24 00:41:49 +00:00
|
|
|
class SithInlineLexer(InlineLexer):
|
2016-12-27 18:40:31 +00:00
|
|
|
grammar_class = SithInlineGrammar
|
|
|
|
|
|
|
|
default_rules = [
|
2018-10-04 19:29:19 +00:00
|
|
|
"escape",
|
2017-06-10 19:39:56 +00:00
|
|
|
# 'inline_html',
|
2018-10-04 19:29:19 +00:00
|
|
|
"autolink",
|
|
|
|
"url",
|
|
|
|
"footnote",
|
|
|
|
"link",
|
|
|
|
"reflink",
|
|
|
|
"nolink",
|
|
|
|
"exposant",
|
|
|
|
"double_emphasis",
|
|
|
|
"emphasis",
|
|
|
|
"underline",
|
|
|
|
"indice",
|
|
|
|
"code",
|
|
|
|
"linebreak",
|
|
|
|
"strikethrough",
|
|
|
|
"text",
|
2016-12-27 18:40:31 +00:00
|
|
|
]
|
|
|
|
inline_html_rules = [
|
2018-10-04 19:29:19 +00:00
|
|
|
"escape",
|
|
|
|
"autolink",
|
|
|
|
"url",
|
|
|
|
"link",
|
|
|
|
"reflink",
|
|
|
|
"nolink",
|
|
|
|
"exposant",
|
|
|
|
"double_emphasis",
|
|
|
|
"emphasis",
|
|
|
|
"underline",
|
|
|
|
"indice",
|
|
|
|
"code",
|
|
|
|
"linebreak",
|
|
|
|
"strikethrough",
|
|
|
|
"text",
|
2016-12-27 18:40:31 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def output_underline(self, m):
|
|
|
|
text = m.group(1)
|
|
|
|
return self.renderer.underline(text)
|
|
|
|
|
|
|
|
def output_exposant(self, m):
|
2016-12-30 00:17:41 +00:00
|
|
|
text = m.group(1)
|
2016-12-27 18:40:31 +00:00
|
|
|
return self.renderer.exposant(text)
|
|
|
|
|
|
|
|
def output_indice(self, m):
|
2016-12-30 00:17:41 +00:00
|
|
|
text = m.group(1)
|
2016-12-27 18:40:31 +00:00
|
|
|
return self.renderer.indice(text)
|
|
|
|
|
|
|
|
# Double emphasis rule changed
|
|
|
|
def output_double_emphasis(self, m):
|
|
|
|
text = m.group(1)
|
|
|
|
text = self.output(text)
|
|
|
|
return self.renderer.double_emphasis(text)
|
|
|
|
|
|
|
|
# Emphasis rule changed
|
|
|
|
def output_emphasis(self, m):
|
|
|
|
text = m.group(1)
|
|
|
|
text = self.output(text)
|
|
|
|
return self.renderer.emphasis(text)
|
|
|
|
|
2016-12-24 14:37:19 +00:00
|
|
|
def _process_link(self, m, link, title=None):
|
2017-06-12 07:42:03 +00:00
|
|
|
try: # Add page:// support for links
|
2018-10-04 19:29:19 +00:00
|
|
|
page = re.compile(r"^page://(\S*)") # page://nom_de_ma_page
|
2016-12-25 21:50:47 +00:00
|
|
|
match = page.search(link)
|
|
|
|
page = match.group(1) or ""
|
2018-10-04 19:29:19 +00:00
|
|
|
link = reverse("core:page", kwargs={"page_name": page})
|
2017-06-12 07:42:03 +00:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
try: # Add file:// support for links
|
2018-10-04 19:29:19 +00:00
|
|
|
file_link = re.compile(r"^file://(\d*)/?(\S*)?") # file://4000/download
|
2016-12-24 14:37:19 +00:00
|
|
|
match = file_link.search(link)
|
|
|
|
id = match.group(1)
|
|
|
|
suffix = match.group(2) or ""
|
2018-10-04 19:29:19 +00:00
|
|
|
link = reverse("core:file_detail", kwargs={"file_id": id}) + suffix
|
2017-06-12 07:42:03 +00:00
|
|
|
except:
|
|
|
|
pass
|
2016-12-24 14:37:19 +00:00
|
|
|
return super(SithInlineLexer, self)._process_link(m, link, title)
|
|
|
|
|
2017-06-12 07:42:03 +00:00
|
|
|
|
2017-02-24 01:45:37 +00:00
|
|
|
renderer = SithRenderer(escape=True)
|
2016-12-24 00:41:49 +00:00
|
|
|
inline = SithInlineLexer(renderer)
|
|
|
|
|
|
|
|
markdown = Markdown(renderer, inline=inline)
|
|
|
|
|
2016-12-27 18:40:31 +00:00
|
|
|
if __name__ == "__main__":
|
2017-08-24 14:29:40 +00:00
|
|
|
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
2018-10-04 19:29:19 +00:00
|
|
|
with open(os.path.join(root_path) + "/doc/SYNTAX.md", "r") as md:
|
2017-08-24 14:29:40 +00:00
|
|
|
result = markdown(md.read())
|
2018-10-04 19:29:19 +00:00
|
|
|
print(result, end="")
|