Total Complexity | 2 |
Total Lines | 53 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
1 | import bleach |
||
11 | class MarkdownRenderer(BaseRenderer): |
||
12 | """ Handles rendering markdown into html """ |
||
13 | |||
14 | DEFAULT_EXTENSIONS = [ |
||
15 | 'pymdownx.github', |
||
16 | ] |
||
17 | |||
18 | # Attributes and tags used for rendering markdown taken from: |
||
19 | # https://github.com/yourcelf/bleach-whitelist |
||
20 | # |
||
21 | # Slightly modified |
||
22 | |||
23 | MARKDOWN_ATTRS = { |
||
24 | 'a': ['alt', 'href', 'title'], |
||
25 | 'img': ['alt', 'src', 'title'], |
||
26 | } |
||
27 | |||
28 | MARKDOWN_TAGS = { |
||
29 | 'a', |
||
30 | 'b', 'blockquote', 'br', |
||
31 | 'code', |
||
32 | 'dd', 'div', 'dt', |
||
33 | 'em', |
||
34 | 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', |
||
35 | 'i', 'img', |
||
36 | 'li', |
||
37 | 'ol', |
||
38 | 'p', |
||
39 | 'span', 'strong', |
||
40 | 'tt', |
||
41 | 'ul', |
||
42 | } |
||
43 | |||
44 | @staticmethod |
||
45 | def get_extensions(): |
||
46 | """ Get a list of extensions to use """ |
||
47 | return get_setting( |
||
48 | 'markdown_extensions', |
||
49 | MarkdownRenderer.DEFAULT_EXTENSIONS) |
||
50 | |||
51 | def render(self, text): |
||
52 | """ Convert the given text into html """ |
||
53 | converted = markdown( |
||
54 | text, |
||
55 | extensions=MarkdownRenderer.get_extensions(), |
||
56 | output_format='html5') |
||
57 | |||
58 | cleaned = bleach.clean( |
||
59 | converted, |
||
60 | attributes=self.MARKDOWN_ATTRS, |
||
61 | tags=self.MARKDOWN_TAGS) |
||
62 | |||
63 | return mark_safe(cleaned) |
||
64 |