aboutsummaryrefslogtreecommitdiffhomepage
path: root/apioforum/mdrender.py
blob: d9b8ea10ea4f746a654e5a3bb83d22d674ff7461 (plain)
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
import bleach
from .csscolors import csscolors

allowed_tags = [
    'p',
    'h1',
    'h2',
    'h3',
    'h4',
    'h5',
    'h6',
    'pre',
    'del',
    'ins',
    'mark',
    'img',
    'marquee',
    'pulsate',
    'sup','sub',
    'table','thead','tbody','tr','th','td',
    'details','summary',
    'hr',
    'br',
    


]

allowed_tags += csscolors
allowed_tags += ("mark" + c for c in csscolors)

allowed_attributes = bleach.sanitizer.ALLOWED_ATTRIBUTES.copy()
allowed_attributes.update(
    img=['src','alt','title'],
    ol=['start'],
    details=['open'],
)

allowed_tags.extend(bleach.sanitizer.ALLOWED_TAGS)

cleaner = bleach.sanitizer.Cleaner(tags=allowed_tags,attributes=allowed_attributes)

import markdown
md = markdown.Markdown(extensions=[
    'pymdownx.tilde',
    'pymdownx.caret',
    'fenced_code',
    'tables',
    'pymdownx.details',
])

def render(text):
    text = md.reset().convert(text)
    text = cleaner.clean(text)
    return text