[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 ...
随机推荐
- nslookup命令
nslookup命令可以从本地DNS服务器中查看所有的IP地址和域名信息(它就像一本互联网电话簿).例如,想要找到www.baidu.com的IP地址就可以使用nslookup命令. nslookup ...
- BAT 快速删除CVS文件和拷贝最近修改文件的目录结构
相信大家在操作大量文件的的时候,经常会遇到一些手动很难操作的情况 比如有CVS版本控制下每个文件夹下都有一个CVS文件夹,一个个手工删除肯定很费劲,我们都是懒人,还是用工具解决吧.不用重新写程序,直接 ...
- scrum.4
1.准备看板. 形式参考图4. 2.任务认领,并把认领人标注在看板上的任务标签上. 先由个人主动领任务,PM根据具体情况进行任务的平衡. 然后每个人都着手实现自己的任务. 3.为了团队合作愉快进展顺利 ...
- ( 转)UVM验证方法学之一验证平台
在现代IC设计流程中,当设计人员根据设计规格说明书完成RTL代码之后,验证人员开始验证这些代码(通常称其为DUT,Design Under Test).验证工作主要保证从设计规格说明书到RTL转变的正 ...
- Apache Server 添加虚拟主机(Virtual Host )
当前许多虚拟服务器如阿里云的ECS服务器,都提供各式各样的虚拟机,常见的有Linux.Windows等,如果我们使用了Apache Server作为虚拟机的Web服务器,并且我们希望多个web应用程序 ...
- JS里面利用random()实现随机颜色更换
首先你需要一个div <div id="box"></div> 然后给这个div加入CSS属性 #box{width:500px;height:500px; ...
- php设计模式之单例模式使用示例
单例模式也就是只能实例化一次,也就代表在实例化时不可能使用new关键字,下面是使用示例,大家参考使用吧 <?php class EasyFramework_Easy_Mysql{ p ...
- 使用hex6x 进行十六进制转换
接触DSP两年多,虽然烧写Flash的操作都没问题,但是要是问起来为什么这么做的,就有点自惭形秽了.所以花些时间,查阅一下资料,整理一下. 1.先看看BurnFlash都需要什么东西. XXX.out ...
- transform初学习
1.什么是transform? transform主要用于形变,位移和旋转,可用于动画. p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; text-align: jus ...
- copy(python中的引用,浅拷贝,深拷贝)
#直接赋值 list = [1,2,['a','b'],'python'] #现将a等于list a = list print a [1,2,['a','b'],'python'] list.appe ...