Python : How to convert markdown formatted text to text

后端 未结 4 1364
迷失自我
迷失自我 2020-12-23 09:23

I need to convert markdown text to plain text format to display summary in my website. I want the code in python.

4条回答
  •  感动是毒
    2020-12-23 10:08

    Despite the fact that this is a very old question, I'd like to suggest a solution I came up with recently. This one neither uses BeautifulSoup nor has an overhead of converting to html and back.

    The markdown module core class Markdown has a property output_formats which is not configurable but otherwise patchable like almost anything in python is. This property is a dict mapping output format name to a rendering function. By default it has two output formats, 'html' and 'xhtml' correspondingly. With a little help it may have a plaintext rendering function which is easy to write:

    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)
    

    unmark function takes markdown text as an input and returns all the markdown characters stripped out.

提交回复
热议问题