-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunmark.py
More file actions
33 lines (26 loc) · 728 Bytes
/
Copy pathunmark.py
File metadata and controls
33 lines (26 loc) · 728 Bytes
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Converts markdown to plain text
Author: Pavel Vorobyov
Source: https://stackoverflow.com/a/54923798/2643154
License: CC BY-SA 4.0
"""
from markdown import Markdown
from io import StringIO
def unmark_element(element, stream=None):
if stream is None:
stream = StringIO()
if element.text:
stream.write(element.text)
for sub in element:
unmark_element(sub, stream)
if element.tail:
stream.write(element.tail)
return stream.getvalue()
# patching Markdown
Markdown.output_formats["plain"] = unmark_element
__md = Markdown(output_format="plain")
__md.stripTopLevelTags = False
def unmark(text):
return __md.convert(text)