[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

相关推荐
uppp»13 分钟前
深入理解 Java 反射机制:获取类信息与动态操作
java·开发语言
Yan-英杰15 分钟前
百度搜索和文心智能体接入DeepSeek满血版——AI搜索的新纪元
图像处理·人工智能·python·深度学习·deepseek
weixin_307779131 小时前
Azure上基于OpenAI GPT-4模型验证行政区域数据的设计方案
数据仓库·python·云计算·aws
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
多想和从前一样5 小时前
Django 创建表时 “__str__ ”方法的使用
后端·python·django
ll7788115 小时前
LeetCode每日精进:20.有效的括号
c语言·开发语言·算法·leetcode·职场和发展
小喵要摸鱼6 小时前
【Pytorch 库】自定义数据集相关的类
pytorch·python
bdawn6 小时前
深度集成DeepSeek大模型:WebSocket流式聊天实现
python·websocket·openai·api·实时聊天·deepseek大模型·流式输出
Jackson@ML6 小时前
Python数据可视化简介
开发语言·python·数据可视化
mosquito_lover16 小时前
怎么把pyqt界面做的像web一样漂亮
前端·python·pyqt