2017-04-24 15:51:12 +00:00
|
|
|
# -*- coding:utf-8 -*
|
|
|
|
#
|
|
|
|
# Copyright 2016,2017
|
|
|
|
# - Skia <skia@libskia.so>
|
|
|
|
#
|
|
|
|
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
|
|
|
|
# http://ae.utbm.fr.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU General Public License a published by the Free Software
|
|
|
|
# Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# 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
|
|
|
|
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2016-12-24 00:41:49 +00:00
|
|
|
import re
|
|
|
|
from mistune import Renderer, InlineGrammar, InlineLexer, Markdown
|
|
|
|
from django.core.urlresolvers import reverse_lazy, reverse
|
|
|
|
|
|
|
|
|
|
|
|
class SithRenderer(Renderer):
|
|
|
|
def file_link(self, id, suffix):
|
|
|
|
return reverse('core:file_detail', kwargs={'file_id': id}) + suffix
|
|
|
|
|
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):
|
|
|
|
return """<span class="underline">%s</span>""" % text
|
|
|
|
|
|
|
|
class SithInlineGrammar(InlineGrammar):
|
|
|
|
double_emphasis = re.compile(
|
|
|
|
r'^\*{2}([\s\S]+?)\*{2}(?!\*)' # **word**
|
|
|
|
)
|
|
|
|
emphasis = re.compile(
|
|
|
|
r'^\*((?:\*\*|[^\*])+?)\*(?!\*)' # *word*
|
|
|
|
)
|
|
|
|
underline = re.compile(
|
|
|
|
r'^_{2}([\s\S]+?)_{2}(?!_)' # __word__
|
|
|
|
)
|
2016-12-30 00:17:41 +00:00
|
|
|
exposant = re.compile( # FIXME Does not work for now
|
|
|
|
r'^\^([\s\S]+?)\^' # ^text^
|
|
|
|
# r'|' # FIXME doesn't properly works like this
|
|
|
|
# r'^\^(\S+)' # ^word
|
2016-12-27 18:40:31 +00:00
|
|
|
)
|
2016-12-30 00:17:41 +00:00
|
|
|
indice = re.compile(
|
|
|
|
r'^_([\s\S]+?)_' # _text_ (^` hack, because no other solution were found :/ this sadly prevent code in indices)
|
|
|
|
# r'|' # FIXME doesn't properly works like this
|
|
|
|
# r'^_(\S+)' # _word
|
2016-12-27 18:40:31 +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 = [
|
|
|
|
'escape',
|
|
|
|
'inline_html',
|
|
|
|
'autolink',
|
|
|
|
'url',
|
|
|
|
'footnote',
|
|
|
|
'link',
|
|
|
|
'reflink',
|
|
|
|
'nolink',
|
2016-12-30 00:17:41 +00:00
|
|
|
'exposant',
|
2016-12-27 18:40:31 +00:00
|
|
|
'double_emphasis',
|
|
|
|
'emphasis',
|
|
|
|
'underline',
|
2016-12-30 00:17:41 +00:00
|
|
|
'indice',
|
2016-12-27 18:40:31 +00:00
|
|
|
'code',
|
|
|
|
'linebreak',
|
|
|
|
'strikethrough',
|
|
|
|
'text',
|
|
|
|
]
|
|
|
|
inline_html_rules = [
|
|
|
|
'escape',
|
|
|
|
'autolink',
|
|
|
|
'url',
|
|
|
|
'link',
|
|
|
|
'reflink',
|
|
|
|
'nolink',
|
2016-12-30 00:17:41 +00:00
|
|
|
'exposant',
|
2016-12-27 18:40:31 +00:00
|
|
|
'double_emphasis',
|
|
|
|
'emphasis',
|
|
|
|
'underline',
|
2016-12-30 00:17:41 +00:00
|
|
|
'indice',
|
2016-12-27 18:40:31 +00:00
|
|
|
'code',
|
|
|
|
'linebreak',
|
|
|
|
'strikethrough',
|
|
|
|
'text',
|
|
|
|
]
|
|
|
|
|
|
|
|
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):
|
2016-12-27 18:40:31 +00:00
|
|
|
try: # Add page:// support for links
|
2016-12-25 21:50:47 +00:00
|
|
|
page = re.compile(
|
|
|
|
r'^page://(\S*)' # page://nom_de_ma_page
|
|
|
|
)
|
|
|
|
match = page.search(link)
|
|
|
|
page = match.group(1) or ""
|
|
|
|
link = reverse('core:page', kwargs={'page_name': page})
|
|
|
|
except: pass
|
2016-12-27 18:40:31 +00:00
|
|
|
try: # Add file:// support for links
|
2016-12-24 14:37:19 +00:00
|
|
|
file_link = re.compile(
|
2016-12-25 21:50:47 +00:00
|
|
|
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 ""
|
|
|
|
link = reverse('core:file_detail', kwargs={'file_id': id}) + suffix
|
|
|
|
except: pass
|
|
|
|
return super(SithInlineLexer, self)._process_link(m, link, title)
|
|
|
|
|
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__":
|
|
|
|
print(markdown.inline.default_rules)
|
|
|
|
print(markdown.inline.inline_html_rules)
|
|
|
|
text = """
|
|
|
|
## Basique
|
|
|
|
|
|
|
|
* Mettre le texte en **gras** : `**texte**`
|
|
|
|
|
|
|
|
* Mettre le texte en *italique* : `*texte*`
|
|
|
|
|
|
|
|
* __Souligner__ le texte : `__texte__`
|
|
|
|
|
|
|
|
* ~~Barrer du texte~~ : `~~texte~~`
|
|
|
|
|
2016-12-30 00:17:41 +00:00
|
|
|
* Mettre ^du texte^ en ^exposant^ : `^mot` ou `^texte^`
|
2016-12-27 18:40:31 +00:00
|
|
|
|
2016-12-30 00:17:41 +00:00
|
|
|
* _Mettre du texte_ en _indice_ : `_mot` ou `_texte_`
|
2016-12-27 18:40:31 +00:00
|
|
|
|
|
|
|
* Pied de page [^en pied de page]
|
|
|
|
|
|
|
|
## Blocs de citations
|
|
|
|
|
|
|
|
Un bloc de citation se crée ainsi :
|
|
|
|
```
|
|
|
|
> Ceci est
|
|
|
|
> un bloc de
|
|
|
|
> citation
|
|
|
|
```
|
|
|
|
|
2017-02-06 23:10:00 +00:00
|
|
|
> Ceci est
|
|
|
|
> un bloc de
|
|
|
|
> citation
|
2016-12-27 18:40:31 +00:00
|
|
|
|
|
|
|
Il est possible d'intégrer de la syntaxe Markdown-AE dans un tel bloc.
|
|
|
|
|
2016-12-30 00:17:41 +00:00
|
|
|
Petit *test* _sur_ ^une^ **seule** ^ligne pour voir^
|
|
|
|
|
2016-12-27 18:40:31 +00:00
|
|
|
"""
|
|
|
|
print(markdown(text))
|
2016-12-24 00:41:49 +00:00
|
|
|
|