[python库] mistune库的基本使用

2023-12-15 00:55:28

前言

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

import mistune
mistune.html(YOUR_MARKDOWN_TEXT)

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

pip install mistune

命令行工具

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

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提供的命令行工具:

# 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 

输出:

<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

python -m mistune -f README.md -r rst

运行这个我这边是直接报错了。。。
在这里插入图片描述

格式化 Markdown

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

python -m mistune -f README.md -r markdown -o README.md 

该命令将重新格式化文本README.md,额也是报错,已提issuehttps://github.com/lepture/mistune/issues/374
在这里插入图片描述

Unix管道

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

echo "foo **bar**" | python -m mistune

输出:

<p>foo <strong>bar</strong></p>

Reference

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

文章来源:https://blog.csdn.net/xiaokai1999/article/details/134862068
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。