[python库] mistune库的基本使用

前言

mistune库是一个解析Markdown的利器,使用起来非常简单。当我们想要解析Markdown格式的文档时,只需两步就能将其转换成html格式。如下:

python 复制代码
import mistune
mistune.html(YOUR_MARKDOWN_TEXT)

安装方式也非常简单,dddd:

python 复制代码
pip install mistune

命令行工具

mistune可以通过命令行将markdown格式的文本转换为HTML,可以使用python3 -m mistune -h查看帮助文档:

python 复制代码
yczx@yczx:~$ python3 -m mistune -h
usage: python -m mistune [-h] [-m MESSAGE] [-f FILE] [-p NAME [NAME ...]]
                         [--escape] [--hardwrap] [-o OUTPUT] [-r RENDERER]
                         [--version]

Mistune, a sane and fast python markdown parser.

Here are some use cases of the command line tool:

    $ python -m mistune -m "Hi **Markdown**"
    <p>Hi <strong>Markdown</strong></p>

    $ python -m mistune -f README.md
    <p>...

    $ cat README.md | python -m mistune
    <p>...

options:
  -h, --help            show this help message and exit
  -m MESSAGE, --message MESSAGE
                        the markdown message to convert
  -f FILE, --file FILE  the markdown file to convert
  -p NAME [NAME ...], --plugin NAME [NAME ...]
                        specifiy a plugin to use
  --escape              turn on escape option
  --hardwrap            turn on hardwrap option
  -o OUTPUT, --output OUTPUT
                        write the rendered result into file
  -r RENDERER, --renderer RENDERER
                        specify the output renderer
  --version             show program's version number and exit

此时我们有一段Markdown文档,用以下内容来测试mistune提供的命令行工具:

markdown 复制代码
# h1 标题
## h2 标题
### h3 标题
#### h4 标题
##### h5 标题
###### h6 标题


## 水平线

___

---

***


## 文本样式

**This is bold text**

__This is bold text__

*This is italic text*

_This is italic text_

~~Strikethrough~~


## 列表

无序

+ Create a list by starting a line with `+`, `-`, or `*`
+ Sub-lists are made by indenting 2 spaces:
  - Marker character change forces new list start:
    * Ac tristique libero volutpat at
    + Facilisis in pretium nisl aliquet
    - Nulla volutpat aliquam velit
+ Very easy!

有序

1. Lorem ipsum dolor sit amet
2. Consectetur adipiscing elit
3. Integer molestie lorem at massa


1. You can use sequential numbers...
1. ...or keep all the numbers as `1.`

Start numbering with offset:

57. foo
1. bar


## 代码

Inline `code`

Indented code

    // Some comments
    line 1 of code
    line 2 of code
    line 3 of code


Block code "fences"

Sample text here...

复制代码
Syntax highlighting

```js
var foo = function (bar) {
  return bar++;
};

console.log(foo(5));

## 将 Markdown 转换为 HTML
默认情况下,misune的命令行工具会将markdown文本转换为HTML文本:
```shell
python -m mistune -f README.md 

输出:

html 复制代码
<h1>h1 标题</h1>
<h2>h2 标题</h2>
<h3>h3 标题</h3>
<h4>h4 标题</h4>
<h5>h5 标题</h5>
<h6>h6 标题</h6>
<h2>水平线</h2>
<hr />
<hr />
<hr />
<h2>文本样式</h2>
<p><strong>This is bold text</strong></p>
<p><strong>This is bold text</strong></p>
<p><em>This is italic text</em></p>
<p><em>This is italic text</em></p>
<p><del>Strikethrough</del></p>
<h2>列表</h2>
<p>无序</p>
<ul>
  <li>Create a list by starting a line with <code>+</code>, <code>-</code>, or <code>*</code></li>
  <li>Sub-lists are made by indenting 2 spaces:<ul>
    <li>Marker character change forces new list start:<ul>
      <li>Ac tristique libero volutpat at</li>
    </ul>
      <ul>
        <li>Facilisis in pretium nisl aliquet</li>
      </ul>
      <ul>
        <li>Nulla volutpat aliquam velit</li>
      </ul>
    </li>
  </ul>
  </li>
  <li>Very easy!</li>
</ul>
<p>有序</p>
<ol>
  <li><p>Lorem ipsum dolor sit amet</p>
  </li>
  <li><p>Consectetur adipiscing elit</p>
  </li>
  <li><p>Integer molestie lorem at massa</p>
  </li>
  <li><p>You can use sequential numbers...</p>
  </li>
  <li><p>...or keep all the numbers as <code>1.</code></p>
  </li>
</ol>
<p>Start numbering with offset:</p>
<ol start="57">
  <li>foo</li>
  <li>bar</li>
</ol>
<h2>代码</h2>
<p>Inline <code>code</code></p>
<p>Indented code</p>
<pre><code>// Some comments
  line 1 of code
  line 2 of code
  line 3 of code</code></pre>
<p>Block code &quot;fences&quot;</p>
<pre><code>Sample text here...
</code></pre>
<p>Syntax highlighting</p>
<pre><code class="language-js">var foo = function (bar) {
  return bar++;
  };

  console.log(foo(5));
</code></pre>

将 Markdown 转换为RestructedText

Mistune 有一个内置的 RestructedText 格式化程序,使用以下命令指定渲染器:-r rst

shell 复制代码
python -m mistune -f README.md -r rst

运行这个我这边是直接报错了。。。

格式化 Markdown

您可以使用 Markdown 渲染器重新格式化 Markdown 文件:

shell 复制代码
python -m mistune -f README.md -r markdown -o README.md 

该命令将重新格式化文本README.md,额也是报错,已提issuehttps://github.com/lepture/mistune/issues/374

Unix管道

命令行工具支持unix PIPE。例如:

shell 复制代码
echo "foo **bar**" | python -m mistune

输出:

html 复制代码
<p>foo <strong>bar</strong></p>

Reference

https://mistune.lepture.com/en/latest/directives.html

相关推荐
Json_1817901448026 分钟前
亚马逊Amazon商品详情API接口概述,json数据示例返回(测试)
开发语言·python
bingbingyihao37 分钟前
通过代码获取接口文档工具
开发语言·前端·javascript
cyz14100142 分钟前
树莓派4B配置wifi热点,可访问http协议
linux·网络·windows·后端·网络协议·http·树莓派
DevangLic1 小时前
下【STL 之速通pair vector list stack queue set map 】
开发语言·c++·list
PfCoder1 小时前
WinForm真入门(13)——ListBox控件详解
windows·c#·visual studio·winform
你又食言了哦1 小时前
qt上设置 WebAssembly显示中文
开发语言·qt·wasm
测试杂货铺1 小时前
软件测试之功能测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
神州永泰1 小时前
Java logback框架日志输出中文乱码的解决方案(windows)
java·windows·logback
前端大菜鸟_1 小时前
河北工程大学e2e平台,python
python·e2e·河北工程大学
顾琬清1 小时前
Linux系统Docker部署开源在线协作笔记Trilium Notes与远程访问详细教程
开发语言·后端·golang