[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

相关推荐
2501_9032386514 分钟前
Git Bash:Windows下的强大命令行工具
windows·git·bash·个人开发
Joey_friends32 分钟前
【Linux】线程详解
开发语言
紫雾凌寒34 分钟前
计算机视觉 |解锁视频理解三剑客——TimeSformer
python·深度学习·神经网络·计算机视觉·transformer·timesformer
_GR37 分钟前
Qt开发⑧Qt的窗口_下_浮动窗口+对话框
开发语言·css·c++·qt·microsoft
Good Note1 小时前
Golang——常用库context和runtime
开发语言·爬虫·golang
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
程序员杰哥1 小时前
测试用例详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
清河__1 小时前
【Go】十七、grpc 服务的具体功能编写
开发语言·后端·golang
张声录11 小时前
国密算法Sm2工具类--golang实现版
开发语言·后端·golang
吃汤圆的抹香鲸1 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net