[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

相关推荐
bst@微胖子20 分钟前
Python高级语法之selenium
开发语言·python·selenium
王小义笔记25 分钟前
Postman如何流畅使用DeepSeek
开发语言·测试工具·lua·postman·deepseek
查理零世1 小时前
【蓝桥杯集训·每日一题2025】 AcWing 6118. 蛋糕游戏 python
python·算法·蓝桥杯
魔尔助理顾问2 小时前
一个简洁高效的Flask用户管理示例
后端·python·flask
java1234_小锋2 小时前
一周学会Flask3 Python Web开发-request请求对象与url传参
开发语言·python·flask·flask3
流星白龙5 小时前
【C++】36.C++IO流
开发语言·c++
诚信爱国敬业友善6 小时前
常见排序方法的总结归类
开发语言·python·算法
nbsaas-boot7 小时前
Go 自动升级依赖版本
开发语言·后端·golang
架构默片7 小时前
【JAVA工程师从0开始学AI】,第五步:Python类的“七十二变“——当Java的铠甲遇见Python的液态金属
java·开发语言·python
不只会拍照的程序猿7 小时前
从插入排序到希尔排序
java·开发语言·数据结构·算法·排序算法