When developing web apps with Django or Bottle, you may want to render Markdown in Python for the text you type. So, this time, I will try to convert it to HTML on the Python side using the OSS package called mistune.
If you follow the sample described in the mistune README, it will be as follows. As a point, in the case of normal text, each paragraph is surrounded by a <p>
tag, in which highlights and italics are expanded. In the case of list comprehension, it feels like it is surrounded by the corresponding tags.
In [1]: import mistune
In [2]: mistune.markdown('I am using **mistune markdown parser**')
Out[2]: '<p>I am using <strong>mistune markdown parser</strong></p>\n'
In [3]: txt = """Paragraph 1
...:
...:Paragraph 2"""
In [4]: mistune.markdown(txt)
Out[4]: '<p>Paragraph 1</p>\n<p>Paragraph 2</p>\n'
In [5]: txt = """-Listing 1
...: -Listing 2
...: -Listing 3"""
In [6]: mistune.markdown(txt)
Out[6]: '<ul>\n<li>Listing 1</li>\n<li>Listing 2</li>\n<li>Listing 3</li>\n</ul>\n'
There are also options such as ʻescape(True by default) whether to escape the entered HTML tag and
hard_wrap to include the
`tag at line breaks without blank lines. (False by default).
By the way, in mistune, conversion according to the general markdown specifications is provided by default, but in addition, there is also a function to specify rules for unique notations and symbols and convert to HTML.
Although mistune provides http / https linking function as standard, it does not support other protocols such as ftp and smb. Here, as an example, let's add a function to automatically add an a tag to a link.
The method is to write your own rules in Lexer and specify the conversion method corresponding to Renderer. Here, Lexer extracts the URL of the ftp or smb protocol as a regular expression, and Renderer adds <a>
tags before and after.
import re
from mistune import Renderer, InlineLexer
class DocumentLinkRenderer(Renderer):
def document_link(self, link):
return '<a href="{l}">{l}</a>'.format(l=link)
class DocumentLinkInlineLexer(InlineLexer):
def enable_document_link(self):
self.rules.document_link = re.compile(r'''^((https?|smb|ftp|file):\/\/[^\s<]+[^<.,:;"')\]\s])''')
self.default_rules.insert(3, 'document_link')
def output_document_link(self, m):
text = m.group(1)
return self.renderer.document_link(text)
To use it, specify a custom Renderer and InlineLexer in mistune.Markdown ()
.
renderer = DocumentLinkRenderer()
inline = DocumentLinkInlineLexer(renderer)
inline.enable_document_link()
markdown = mistune.Markdown(renderer, inline=inline)
markdown("markdown text")
When you actually use it, you can see that the link is automatically created for the ftp URL as shown below.
In [20]: markdown("ftp://path/to/file")
Out[20]: '<p><a href="ftp://path/to/file">ftp://path/to/file</a></p>\n'
With this function, you can attach a link to a specific word like Hatena Keyword, or link an article ID and URL like a ticket for Redmine or Backlog.
Recommended Posts