Python Markdown的使用

Python Markdown的使用

安装

pip intstall markdown

使用

foo.py

import markdown
from markdown.extensions.wikilinks import WikiLinkExtension

#基本用法
a = markdown.markdown("*boo!*")
b = markdown.markdown("**boom!**")
print(a)
print(b)

#扩展用法
input_file = open("foo.md", mode="r", encoding="utf-8")
text = input_file.read()
html = markdown.markdown(text, output_format='html5', \
    extensions=['markdown.extensions.toc',\
    WikiLinkExtension(base_url='https://en.wikipedia.org/wiki/',\
        end_url='#Hyperlinks_in_wikis'),\
    'markdown.extensions.sane_lists',\
    'markdown.extensions.codehilite',\
    'markdown.extensions.abbr',\
    'markdown.extensions.attr_list',\
    'markdown.extensions.def_list',\
    'markdown.extensions.fenced_code',\
    'markdown.extensions.footnotes',\
    'markdown.extensions.smart_strong',\
    'markdown.extensions.meta',\
    'markdown.extensions.nl2br',\
    'markdown.extensions.tables'])


output_file = open("foo.html", "w", 
                          encoding="utf-8", 
                          errors="xmlcharrefreplace"
)
output_file.write(html)

foo.md

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="https://guides.github.com/components/gridism/gridism.css">
<link href="markdown.css" rel="stylesheet">   
<link rel="stylesheet" href="https://guides.github.com/components/primer/octicons.css">
<link href="https://guides.github.com/stylesheets/main.css" rel="stylesheet">
<link href="https://guides.github.com/stylesheets/pygments.css" rel="stylesheet">
</head>
<body>
<div class="markdown-body">

[TOC]

# Markdown 介绍

> Markdown 代码块
> 前面空一个Tab的距离

    #!/usr/bin/python


    import markdown
    from markdown.extensions import Extension
    from markdown.extensions.wikilinks import WikiLinkExtension

    #from markdown2 import Markdown

    #markdown.extensions.tables as the name of the extension
    a = markdown.markdown("*boo!*")
    b = markdown.markdown("**boom!**")
    print(a)
    print(b)

    input_file = open("foo.md", mode="r", encoding="utf-8")
    text = input_file.read()
    html = markdown.markdown(text, output_format='html5', \
        extensions=['markdown.extensions.toc',\
        WikiLinkExtension(base_url='https://en.wikipedia.org/wiki/',\
            end_url='#Hyperlinks_in_wikis'),\
        'markdown.extensions.sane_lists',\
        'markdown.extensions.codehilite',\
        'markdown.extensions.abbr',\
        'markdown.extensions.attr_list',\
        'markdown.extensions.def_list',\
        'markdown.extensions.fenced_code',\
        'markdown.extensions.footnotes',\
        'markdown.extensions.smart_strong',\
        'markdown.extensions.meta',\
        'markdown.extensions.nl2br',\
        'markdown.extensions.tables'])


    output_file = open("foo.html", "w", 
                              encoding="utf-8", 
                              errors="xmlcharrefreplace"
    )
    output_file.write(html)

    #E:/x1/XbnMarkdown/
    #import os
    #os.system('python -m markdown -x markdown.extensions.footnotes -x markdown.extensions.tables foo.md > foo.html')


    #import os
    #os.system('markdown2 foo.md > foo.html')

# Markdown 的使用

## 命令行

**python -m markdown E:\x1\XbnMarkdown\foo.md > foo.html**

![GitHub Logo](logo.png)

[GitHub](http://github.com)


First Header | Second Header
------------ | -------------
Content from cell 1 | Content from cell 2
Content in the first column | Content in the second column


Markdown | Markdown2
------------ | ------------
功能基本齐全 | 功能不全
命令行不同 | 命令行不同

1. Ordered item 1
2. Ordered item 2


* Unordered item 1
* Unordered item 2


Apple
:   Pomaceous fruit of plants of the genus Malus in 
    the family Rosaceae.

Orange
:   The fruit of an evergreen tree of the genus Citrus.


The HTML specification 
is maintained by the W3C.

*[HTML]: Hyper Text Markup Language
*[W3C]:  World Wide Web Consortium

function(){

}

[[Hyperlink]]

</div>
</body>
</html>

执行python foo.py的结果
foo.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="https://guides.github.com/components/gridism/gridism.css">
<link href="markdown.css" rel="stylesheet">   
<link rel="stylesheet" href="https://guides.github.com/components/primer/octicons.css">
<link href="https://guides.github.com/stylesheets/main.css" rel="stylesheet">
<link href="https://guides.github.com/stylesheets/pygments.css" rel="stylesheet">
</head>
<body>
<div class="markdown-body">

<div class="toc">
<ul>
<li><a href="#markdown2">Markdown2的使用</a></li>
<li><a href="#markdown">Markdown 的使用</a><ul>
<li><a href="#_1">命令行</a></li>
</ul>
</li>
</ul>
</div>
<h1 id="markdown2">Markdown2的使用</h1>
<blockquote>
<p>Markdown2 代码块<br>
前面空一个Tab的距离</p>
</blockquote>
<pre class="codehilite"><code>from markdown2 import Markdown

markdowner = Markdown()


a = markdowner.convert(&quot;*boo!*&quot;)
b = markdowner.convert(&quot;**boom!**&quot;)

print(a)
print(b)

import os
os.system('markdown2 foo.md &gt; foo.html')</code></pre>


<h1 id="markdown">Markdown 的使用</h1>
<h2 id="_1">命令行</h2>
<p><strong>python -m markdown E:\x1\XbnMarkdown\foo.md &gt; foo.html</strong></p>
<p><img alt="GitHub Logo" src="logo.png"></p>
<p><a href="http://github.com">GitHub</a></p>
<table>
<thead>
<tr>
<th>First Header</th>
<th>Second Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Content from cell 1</td>
<td>Content from cell 2</td>
</tr>
<tr>
<td>Content in the first column</td>
<td>Content in the second column</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>Markdown</th>
<th>Markdown2</th>
</tr>
</thead>
<tbody>
<tr>
<td>功能基本齐全</td>
<td>功能不全</td>
</tr>
<tr>
<td>命令行不同</td>
<td>命令行不同</td>
</tr>
</tbody>
</table>
<ol>
<li>Ordered item 1</li>
<li>Ordered item 2</li>
</ol>
<ul>
<li>Unordered item 1</li>
<li>Unordered item 2</li>
</ul>
<dl>
<dt>Apple</dt>
<dd>Pomaceous fruit of plants of the genus Malus in <br>
the family Rosaceae.</dd>
<dt>Orange</dt>
<dd>The fruit of an evergreen tree of the genus Citrus.</dd>
</dl>
<p>The <abbr title="Hyper Text Markup Language">HTML</abbr> specification <br>
is maintained by the <abbr title="World Wide Web Consortium">W3C</abbr>.</p>
<pre class="codehilite"><code class="language-javascript">function(){

}</code></pre>


<p><a class="wikilink" href="https://en.wikipedia.org/wiki/Hyperlink#Hyperlinks_in_wikis">Hyperlink</a></p>
<p></div><br>
</body><br>
</html></p>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值