forked from ietf-tools/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.py
More file actions
69 lines (57 loc) · 1.91 KB
/
markdown.py
File metadata and controls
69 lines (57 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Copyright The IETF Trust 2021, All Rights Reserved
# -*- coding: utf-8 -*-
"""Markdown wrapper
Use this instead of importing markdown directly to guarantee consistent extensions / options through
the datatracker.
"""
import markdown as python_markdown
from markdown.extensions import Extension
from markdown.postprocessors import Postprocessor
from django.utils.safestring import mark_safe
from ietf.doc.templatetags.ietf_filters import urlize_ietf_docs
from .html import clean_html, liberal_clean_html
from .text import linkify
class LinkifyExtension(Extension):
"""
Simple Markdown extension inspired by https://github.com/daGrevis/mdx_linkify,
but using our own linker directly. Doing the linkification on the converted
Markdown output introduces artifacts.
"""
def extendMarkdown(self, md):
md.postprocessors.register(LinkifyPostprocessor(md), "linkify", 50)
# disable automatic links via angle brackets for email addresses
md.inlinePatterns.deregister("automail")
# "autolink" for URLs does not seem to cause issues, so leave it on
class LinkifyPostprocessor(Postprocessor):
def run(self, text):
return urlize_ietf_docs(linkify(text))
def markdown(text):
return mark_safe(
clean_html(
python_markdown.markdown(
text,
extensions=[
"extra",
"nl2br",
"sane_lists",
"toc",
LinkifyExtension(),
],
)
)
)
def liberal_markdown(text):
return mark_safe(
liberal_clean_html(
python_markdown.markdown(
text,
extensions=[
"extra",
"nl2br",
"sane_lists",
"toc",
LinkifyExtension(),
],
)
)
)