Python Markdown的使用

  • 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




"utf-8">
"viewport" content="width=device-width,initial-scale=1">
"stylesheet" href="https://guides.github.com/components/gridism/gridism.css">
"markdown.css" rel="stylesheet">   
"stylesheet" href="https://guides.github.com/components/primer/octicons.css">
"https://guides.github.com/stylesheets/main.css" rel="stylesheet">
"https://guides.github.com/stylesheets/pygments.css" rel="stylesheet">


"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]]

执行python foo.py的结果
foo.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("*boo!*")
b = markdowner.convert("**boom!**")

print(a)
print(b)

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


<h1 id="markdown">Markdown 的使用h1>
<h2 id="_1">命令行h2>
<p><strong>python -m markdown E:\x1\XbnMarkdown\foo.md > foo.htmlstrong>p>
<p><img alt="GitHub Logo" src="logo.png">p>
<p><a href="http://github.com">GitHuba>p>
<table>
<thead>
<tr>
<th>First Headerth>
<th>Second Headerth>
tr>
thead>
<tbody>
<tr>
<td>Content from cell 1td>
<td>Content from cell 2td>
tr>
<tr>
<td>Content in the first columntd>
<td>Content in the second columntd>
tr>
tbody>
table>
<table>
<thead>
<tr>
<th>Markdownth>
<th>Markdown2th>
tr>
thead>
<tbody>
<tr>
<td>功能基本齐全td>
<td>功能不全td>
tr>
<tr>
<td>命令行不同td>
<td>命令行不同td>
tr>
tbody>
table>
<ol>
<li>Ordered item 1li>
<li>Ordered item 2li>
ol>
<ul>
<li>Unordered item 1li>
<li>Unordered item 2li>
ul>
<dl>
<dt>Appledt>
<dd>Pomaceous fruit of plants of the genus Malus in <br>
the family Rosaceae.dd>
<dt>Orangedt>
<dd>The fruit of an evergreen tree of the genus Citrus.dd>
dl>
<p>The <abbr title="Hyper Text Markup Language">HTMLabbr> specification <br>
is maintained by the <abbr title="World Wide Web Consortium">W3Cabbr>.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">Hyperlinka>p>
<p>div><br>
body><br>
html>p>

你可能感兴趣的:(使用总结)