Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Markdown 是一种具有纯文本格式语法的标记语言,由于其可读性和易写性,常用于文档和 Readme 文件。它最初仅用于转换为 HTML。Aspose.HTML for Python via .NET 库提供了从 HTML 到 Markdown 的反向转换。您可以在任何设备上使用任何文本编辑器打开、创建和编辑 Markdown 文件。
在本文中,您将学习如何使用转换器类的 convert_html() 方法将 HTML 转换为 Markdown,以及如何应用 MarkdownSaveOptions。
要继续学习本教程,请在您的 Python 项目中 安装并配置 Aspose.HTML for Python via .NET。我们的代码示例将帮助您使用 Python 库将 HTML 转换为 Markdown 并生成 Markdown 文件。
您可以使用 Aspose.HTML for Python 通过 .NET API 实时将 HTML 转换为 Markdown。首先,从本地硬盘或 URL 中加载 HTML 文件并运行示例。在代码示例中,默认设置了保存选项。您将立即收到 HTML 转换为 Markdown 的结果,即一个单独的 Markdown 文件。
Converter 类的方法主要用于将 HTML 代码转换成各种格式的最简单方法。要将 HTML 转换为 Markdown,应遵循以下几个步骤:
要将 HTML 转换为 Markdown,可以定义自己的规则集或使用预定义模板。例如,可以使用基于
GitLab Flavored Markdown 语法的模板。下面的代码片段展示了如何使用 MarkdownSaveOptions 的 git 属性将 HTML 转换为 Markdown:
1# Convert HTML to Markdown using git property in Python
2
3import aspose.html.converters as conv
4import aspose.html.saving as sav
5
6# Prepare HTML code and save it to a file
7code = "<h1>Header 1</h1>" \
8 "<h2>Header 2</h2>" \
9 "<p>Hello, World!!</p>"
10with open("document.html", "w", encoding="utf-8") as f:
11 f.write(code)
12 f.close()
13 # Call the convert_html() method to convert HTML to Markdown
14 conv.Converter.convert_html("document.html", sav.MarkdownSaveOptions.git, "output.md")您可以根据自己的需要,通过保存选项来增强 Markdown 创建功能。MarkdownSaveOptions 有许多属性,可以让你控制转换过程:
| Property | Description |
|---|---|
| default | This property returns a set of options that are compatible with default Markdown documentation. |
| features | This property is a flag set that controls which HTML elements are converted to Markdown. For example, you can choose to convert only links and paragraphs or include a broader range of elements such as headers, lists, and tables. |
| formatter | This property gets or sets the Markdown formatting style. Options available: GIT and DEFAULT. |
| git | This property returns a set of options that are compatible with GitLab Flavored Markdown, which is a popular extension of Markdown used by GitLab. |
| resource_handling_options | This property provides access to a ResourceHandlingOptions object which is used to configure how resources are handled during the conversion process. |
features 属性让我们来看看 MarkdownSaveOptions 的一个重要属性– features。该选项允许您启用/禁用特定元素的转换。下面的示例显示了如何只处理链接和段落,其他 HTML 元素保持原样:
1# Convert HTML to Markdown using features property in Python
2
3import os
4import aspose.html.converters as conv
5import aspose.html.saving as sav
6
7# Prepare directories and paths
8output_dir = "output/"
9if not os.path.exists(output_dir):
10 os.makedirs(output_dir)
11
12save_path = os.path.join(output_dir, "options-output.md")
13
14# Prepare HTML code and save it to a file
15code = "<h1>Header 1</h1>" \
16 "<h2>Header 2</h2>" \
17 "<p>Hello, World!!</p>" \
18 "<a href='https://docs.aspose.com/'>aspose</a>"
19with open(os.path.join(output_dir, "options.html"), "w") as file:
20 file.write(code)
21
22# Create an instance of SaveOptions and set up the rule:
23# – only <a> and <p> elements will be converted to Markdown
24options = sav.MarkdownSaveOptions()
25options.features = sav.MarkdownFeatures.LINK | sav.MarkdownFeatures.AUTOMATIC_PARAGRAPH
26
27# Call the convert_html() method to convert HTML to Markdown
28conv.Converter.convert_html(os.path.join(output_dir, "options.html"), options, save_path)Markdown 支持很多功能,但并非所有功能都能同时使用。例如,表格元素中的列表元素就无法转换。下表显示了哪些功能可以嵌套。每个特性都是 MarkdownFeatures 枚举的成员。
| Parent feature | Features which can be processed inside |
|---|---|
| Header | Link, Emphasis, Strong, InlineCode, Image, Strikethrough, Video |
| Blockquote | Any |
| List | AutomaticParagraph, Link, Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough, Video, TaskList, List |
| Link | Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough |
| AutomaticParagraph | Link, Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough |
| Strikethrough | Link, Emphasis, Strong, InlineCode, Image, LineBreak |
| Table | Video, Strikethrough, Image, InlineCode, Emphasis, Strong, Link |
| Emphasis | Link, InlineCode, Image, LineBreak, Strikethrough, Video |
| Strong | Link, InlineCode, Image, LineBreak, Strikethrough, Video |
Markdown 是一种轻量级且易于使用的语法。不过,并非所有 HTML 元素都能转换为 Markdown,因为 Markdown 语法中没有对应的元素。在转换过程中, STYLE、 SCRIPT、 LINK、 EMBED 等元素将被丢弃。
Markdown 允许您指定纯 HTML 代码,并按原样渲染。允许这种行为的功能称为 “内联 HTML”。要使用该功能,您应在新行开头放置该功能支持的特定元素之一。或者,您也可以将其中一个元素标记为 Inline HTML,方法是在该元素上添加值为 inline 的属性 markdown。下面是一个演示如何使用该属性的小例子:
1# Convert HTML to Markdown using git property in Python
2
3import os
4import aspose.html.converters as conv
5import aspose.html.saving as sav
6
7# Prepare directories and paths
8output_dir = "output/"
9if not os.path.exists(output_dir):
10 os.makedirs(output_dir)
11
12save_path = os.path.join(output_dir, "inline-html.md")
13
14# Prepare HTML code and save it to a file
15code = "text<div markdown='inline'><code>text</code></div>"
16with open(os.path.join(output_dir, "inline.html"), "w") as file:
17 file.write(code)
18
19# Call the convert_html() method for HTML to Markdown conversion
20conv.Converter.convert_html(os.path.join(output_dir, "inline.html"), sav.MarkdownSaveOptions(), save_path)
21
22# Output file will contain: text\r\n<div markdown="inline"><code>text</code></div>如您所见,<div> 元素的内容不会转换为 Markdown 格式,而是由 Markdown 处理器按原样处理。每个 Markdown 处理器支持此功能的元素列表都不同。
最初的 Markdown 规范支持这些标记:blockquote、h1、h2、h3、h4、h5、h6、p、pre、ol、ul、dl、div、ins、del、iframe、fieldset、noscript、form、math。
GitLab Flavored Markdown 用以下标记扩展了该列表:article、footer、nav、ide aside、header、address、hr、dd、figure、figcaption、 abbr、video、audio、output、canvas、section、details、hgroup、summary。
Markdown 语法–基础教程 一文介绍了 Markdown 标记语言的主要元素和 Markdown 语法细节。
下载 Aspose.HTML for Python via .NET库,即可成功、快速、轻松地将 HTML、MHTML、EPUB、SVG 和 Markdown 文档转换为最流行的格式。
您可以从 GitHub 下载完整的示例和数据文件。
Aspose.HTML 提供一款免费的在线 HTML 到 MD 转换器,可将 HTML 高质量、方便快捷地转换为 Markdown。只需上传、转换您的文件,几秒钟就能得到结果!
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.