[python]自动化将markdown文件转成html文件
*:first-child {
margin-top: 0 !important;
}
body>*:last-child {
margin-bottom: 0 !important;
}
/* BLOCKS
=============================================================================*/
p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}
/* HEADERS
=============================================================================*/
h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}
h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}
h1 {
font-size: 28px;
color: #000;
}
h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
color: #777;
font-size: 14px;
}
body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}
a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}
h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}
/* LINKS
=============================================================================*/
a {
color: #4183C4;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* LISTS
=============================================================================*/
ul, ol {
padding-left: 30px;
}
ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}
ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}
dl {
padding: 0;
}
dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}
dl dt:first-child {
padding: 0;
}
dl dt>:first-child {
margin-top: 0px;
}
dl dt>:last-child {
margin-bottom: 0px;
}
dl dd {
margin: 0 0 15px;
padding: 0 15px;
}
dl dd>:first-child {
margin-top: 0px;
}
dl dd>:last-child {
margin-bottom: 0px;
}
/* CODE
=============================================================================*/
pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}
code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}
pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}
pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}
pre code, pre tt {
background-color: transparent;
border: none;
}
kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}
/* QUOTES
=============================================================================*/
blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}
blockquote>:first-child {
margin-top: 0px;
}
blockquote>:last-child {
margin-bottom: 0px;
}
/* HORIZONTAL RULES
=============================================================================*/
hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}
/* TABLES
=============================================================================*/
table th {
font-weight: bold;
}
table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}
table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}
table tr:nth-child(2n) {
background-color: #f8f8f8;
}
/* IMAGES
=============================================================================*/
img {
max-width: 100%
}
-->
一、背景
我们项目开发人员写的文档都是markdown文件。对于其它组的同学要进行阅读不是很方便。每次编辑完markdown文件,我都是用软件将md文件转成html文件。刚开始转的时候,还没啥,转得次数多了,就觉得不能继续这样下去了。作为一名开发人员,还是让机器去做这些琐碎的事情吧。故写了两个脚本将md文件转成html文件,并将其放置在web服务器下,方便其他人员阅读。
主要有两个脚本和一个定时任务:
- 一个python脚本,主要将
md文件转成html文件; - 一个shell脚本,主要用于管理逻辑;
- 一个linux定时任务,主要是定时执行shell脚本。
二、用python将markdown转成html
2.1 python依赖库
使用python的markdown库来转换md文件到html依赖两个库:
- pip install markdown
- pip install importlib
2.2 核心代码
核心代码其实只有一句,执行 markdown.markdown(text)就可以获得生成的html的原文。
input_file = codecs.open(in_file, mode="r", encoding="utf-8")
text = input_file.read()
html = markdown.markdown(text)
2.3 html编码和html样式
直接markdown.markdown(text)生成的html文本,非常粗略,只是单纯的html内容。而且在浏览器内查看的时候中文乱码(在chrome中),没有好看的css样式,太丑了。

解决办法也很简单,在保存文件的时候,将<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />和css样式添加上。就这么简单解决了。

2.4 完整python内容
- 读取md文件;
- 将md文件转成html文本;
- 添加css样式和保存html文本。
python代码内容:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 使用方法 python markdown_convert.py filename import sys
import markdown
import codecs css = '''
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!-- 此处省略掉markdown的css样式,因为太长了 -->
</style>
''' def main(argv):
name = argv[0]
in_file = '%s.md' % (name)
out_file = '%s.html' % (name) input_file = codecs.open(in_file, mode="r", encoding="utf-8")
text = input_file.read()
html = markdown.markdown(text) output_file = codecs.open(out_file, "w",encoding="utf-8",errors="xmlcharrefreplace")
output_file.write(css+html) if __name__ == "__main__":
main(sys.argv[1:])
三、shell逻辑
3.1 逻辑说明
建立一个shell文件,用于进行逻辑处理,主要操作如下:
- 更新svn文件,将最新的md文件更新下来(此处假设md文件是
测试文档.md); - 执行
python markdown_convert.py $NAME将md文件转成html文件(生成测试文档.html); - 将转好的html迁移到web路径下(移动到
html/测试文档.html); - 启动一个web服务(此处用的是python的
SimpleHTTPServer的web服务器).
3.2 完整shell逻辑
#!/bin/bash NAME='测试文档' ## 更新代码
svn update ## 删除html文件
if [ -f "$NAME.html" ];then
rm "$NAME.html"
fi ## 生成html
if [ -f "$NAME.md" ];then
python markdown_convert.py $NAME
fi ## 生成html目录
if [ ! -d "html" ];then
mkdir "html"
fi ## 拷贝html文件
if [ -f "$NAME.html" ];then
mv -f "$NAME.html" "html/"
fi ## 开启web服务器
PID=`ps aux | grep 'python -m SimpleHTTPServer 8080' | grep -v 'grep' | awk '{print $2}'` if [ "$PID" = "" ];then
cd html
nohup python -m SimpleHTTPServer 8080 &
echo 'start web server'
else
echo 'already start'
fi
四、linux定时任务
在shell命令下输入crontab -e进入linux定时任务编辑界面。在里面设置markdown2web.sh脚本的定时任务:
## 更新文档
*/10 * * * * cd /home/xxx/doc; sh markdown2web.sh > /dev/null 2>&1
设置每10分钟执行一次markdown2web.sh脚本,当然也可以根据需求修改频率。
[python]自动化将markdown文件转成html文件的更多相关文章
- 自制 Python小工具 将markdown文件转换成Html文件
今天看到了一个Python库,名为markdown.瞬间就给了我一个灵感,那就是制作一个将markdown文件转换成html文件的小工具. 我的实验环境 操作系统: Windows 7 64位 旗舰版 ...
- 将 Python3 文件打包成 exe 文件
我们用 Python 写好的代码,如何给别人在没有配置 Python 环境的情况下直接使用呢?尤其是面向 windows 众. 因为 Python 是一门解释性的语言,离开了 Python 解释器,P ...
- <p><span style="font-size:14px">近期须要批量将PNM格式的文件转换成GIF文件。我尝试了例如以下的图像转换工具:</span></p>
近期须要批量将PNM格式的文件转换成GIF文件.我尝试了例如以下的图像转换工具: ImageBatch:全然免费,但只支持PNG JPEG BMP GIF四种格式 OfficeConverter:在线 ...
- 使用宏批量将多个csv文件转成excel文件
在一个压缩文件中有100多个csv文件,要求要将此100多个csv文件转成excel文件,名字命名不变,有三种方式: 1. 傻不拉几的复制粘贴法 2. 一个一个打开csv文件,另存为xls文件,工作量 ...
- [转载]webarchive文件转换成htm文件
原文地址:webarchive文件转换成htm文件作者:xhbaxf Mac OS X系统带有文件转换功能,可以把webarchive文件变成html文件.方法是: Step 1: 建立一个文件夹 ...
- C#.NET常见问题(FAQ)-如何将cs文件编译成dll文件 exe文件 如何调用dll文件
比如我要把TestDLL.cs文件编译成dll文件,则在命令提示符下,输入下面的命令,生成的文件为TestDLL.dll csc /target:library TestDLL.cs 注意前提是你安装 ...
- 用gulp把less文件编译成css文件
第一次使用gulp构建工具,使用gulp将.less文件编译成.css文件并输出.根据视频做了笔记.提供新手和自己以后做参考. HTML文件 <!DOCTYPE html> <htm ...
- C#.NET如何将cs文件编译成dll文件 exe文件 如何调用dll文件
比如我要把TestDLL.cs文件编译成dll文件,则在命令提示符下,输入下面的命令,生成的文件为TestDLL.dll csc /target:library TestDLL.cs 注意前提是你安装 ...
- 在JAVA中将class文件编译成jar文件包,运行提示没有主清单属性
在JAVA中将class文件编译成jar文件包,运行提示没有主清单属性 Maven 项目生成jar运行时提示“没有主清单属性” 新建了一个Maven的项目,mvn compile和mvn packag ...
随机推荐
- vc 获取网络时间
方式1 : #include <WinSock2.h> #include <Windows.h> #pragma comment(lib, "WS2_32" ...
- 块级格式化上下文(block formatting context)
在CSS2.1中,有三种定位方案--普通流.浮动和绝对定位: 普通流:元素按照先后位置自上而下布局,inline元素水平排列,直到行被占满后换行,block元素则被渲染为完整的一行,除非指定,所有元素 ...
- nginx修改配置后不生效的问题
nginx增加了新的server name配置,发现nginx -s reload之后总是不生效. http和https均可以打开页面,但是页面是别的server页面,使用的证书也是别的server的 ...
- c#判断闰年
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Python语法二
1.raw_input 输入 2.如果想查看某个关键字的用法,可以在命令行输入pydoc raw_input. 如果是windows,那么试一下 python -m pydoc raw_input 3 ...
- linux开机随笔
(1),linux开机流程: 固件是在软件与硬件之间的那部分,他们既不叫做硬件也不叫做软件, 开机自检 ,就是 在你按下开机键时,电脑就会自动检查你的硬盘 内存 cpu等器件, 那个CMOS是固 ...
- 利用 cookie 模拟网站登录
import urllib import urllib2 import cookielib filename = 'cookie.txt' \#声明一个MozillaCookieJar对象实例来保存c ...
- [转]保护眼睛的Windows和IE、Firefox、谷歌等浏览器颜色设置
保护眼睛的Windows和IE.Firefox.谷歌等浏览器颜色设置 长时间在电脑前工作,窗口和网页上的白色十分刺眼,眼睛很容易疲劳,也容易引起头痛,其实我们可以通过设置Windows窗口和软件的颜 ...
- C# DateTime转Json汇总
DateTime转换成json的时候容易出现不想要的格式,在网上搜索了相关的解决方法copy如下: 参考http://www.newtonsoft.com/json/help/html/DatesIn ...
- jQuery插件制作方法
html页面:<h1>Hello My name is Alex,i from china.</h1> 1.无参数实现文字阴影效果 测试代码: $("h1" ...