-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code to strip markdown before readability
- Loading branch information
1 parent
3fba138
commit c85d1d0
Showing
3 changed files
with
31 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""Module to convert markdown to plain text. Code based on https://stackoverflow.com/a/54923798""" | ||
from io import StringIO | ||
from markdown import Markdown | ||
|
||
|
||
def unmark_element(element, stream=None): | ||
"""Custom plain output format for markdown.""" | ||
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() | ||
|
||
|
||
Markdown.output_formats["plain"] = unmark_element | ||
|
||
|
||
def unmark(text): | ||
"""Convert markdown-formatted text to plain text.""" | ||
md = Markdown(output_format="plain") | ||
md.stripTopLevelTags = False | ||
return md.convert(text) |