mirror of
https://github.com/ae-utbm/sith.git
synced 2024-10-31 19:38:04 +00:00
First dfile:// parsing, not working into links, need to investigate
This commit is contained in:
parent
15e52d1ae8
commit
323b946e1f
37
core/markdown.py
Normal file
37
core/markdown.py
Normal file
@ -0,0 +1,37 @@
|
||||
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
|
||||
|
||||
class SithInlineLexer(InlineLexer):
|
||||
def enable_file_link(self):
|
||||
# add file_link rules
|
||||
self.rules.file_link = re.compile(
|
||||
r'dfile://(\d*)/?(\S*)?' # dfile://4000/download
|
||||
)
|
||||
# Add file_link parser to default rules
|
||||
# you can insert it some place you like
|
||||
# but place matters, maybe 2 is not good
|
||||
self.default_rules.insert(0, 'file_link')
|
||||
|
||||
def output_file_link(self, m):
|
||||
id = m.group(1)
|
||||
suffix = m.group(2) or ""
|
||||
# you can create an custom render
|
||||
# you can also return the html if you like
|
||||
# return directly html like this:
|
||||
# return reverse('core:file_detail', kwargs={'file_id': id}) + suffix
|
||||
return self.renderer.file_link(id, suffix)
|
||||
|
||||
renderer = SithRenderer()
|
||||
inline = SithInlineLexer(renderer)
|
||||
|
||||
# enable the features
|
||||
inline.enable_file_link()
|
||||
markdown = Markdown(renderer, inline=inline)
|
||||
|
||||
|
@ -1,16 +1,15 @@
|
||||
import mistune
|
||||
from django import template
|
||||
from django.template.defaultfilters import stringfilter
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.html import escape
|
||||
|
||||
from core.markdown import markdown as md
|
||||
|
||||
register = template.Library()
|
||||
|
||||
@register.filter(is_safe=False)
|
||||
@stringfilter
|
||||
def markdown(text):
|
||||
md = mistune.Markdown()
|
||||
return mark_safe(md(escape(text)))
|
||||
|
||||
@register.filter()
|
||||
|
Loading…
Reference in New Issue
Block a user