*: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文件的更多相关文章

  1. 自制 Python小工具 将markdown文件转换成Html文件

    今天看到了一个Python库,名为markdown.瞬间就给了我一个灵感,那就是制作一个将markdown文件转换成html文件的小工具. 我的实验环境 操作系统: Windows 7 64位 旗舰版 ...

  2. 将 Python3 文件打包成 exe 文件

    我们用 Python 写好的代码,如何给别人在没有配置 Python 环境的情况下直接使用呢?尤其是面向 windows 众. 因为 Python 是一门解释性的语言,离开了 Python 解释器,P ...

  3. <p><span style="font-size:14px">近期须要批量将PNM&#26684;式的文件转换成GIF文件。我尝试了例如以下的图像转换工具:</span></p>

    近期须要批量将PNM格式的文件转换成GIF文件.我尝试了例如以下的图像转换工具: ImageBatch:全然免费,但只支持PNG JPEG BMP GIF四种格式 OfficeConverter:在线 ...

  4. 使用宏批量将多个csv文件转成excel文件

    在一个压缩文件中有100多个csv文件,要求要将此100多个csv文件转成excel文件,名字命名不变,有三种方式: 1. 傻不拉几的复制粘贴法 2. 一个一个打开csv文件,另存为xls文件,工作量 ...

  5. [转载]webarchive文件转换成htm文件

    原文地址:webarchive文件转换成htm文件作者:xhbaxf Mac OS X系统带有文件转换功能,可以把webarchive文件变成html文件.方法是:   Step 1: 建立一个文件夹 ...

  6. C#.NET常见问题(FAQ)-如何将cs文件编译成dll文件 exe文件 如何调用dll文件

    比如我要把TestDLL.cs文件编译成dll文件,则在命令提示符下,输入下面的命令,生成的文件为TestDLL.dll csc /target:library TestDLL.cs 注意前提是你安装 ...

  7. 用gulp把less文件编译成css文件

    第一次使用gulp构建工具,使用gulp将.less文件编译成.css文件并输出.根据视频做了笔记.提供新手和自己以后做参考. HTML文件 <!DOCTYPE html> <htm ...

  8. C#.NET如何将cs文件编译成dll文件 exe文件 如何调用dll文件

    比如我要把TestDLL.cs文件编译成dll文件,则在命令提示符下,输入下面的命令,生成的文件为TestDLL.dll csc /target:library TestDLL.cs 注意前提是你安装 ...

  9. 在JAVA中将class文件编译成jar文件包,运行提示没有主清单属性

    在JAVA中将class文件编译成jar文件包,运行提示没有主清单属性 Maven 项目生成jar运行时提示“没有主清单属性” 新建了一个Maven的项目,mvn compile和mvn packag ...

随机推荐

  1. JQuery 插件之Ajax Autocomplete(ajax自动完成)搜索引擎自动显示下拉框

    平时用百度,谷歌搜索的时候 会有一个下 拉列表进行提示 这是一个非常好的功能 本文要介绍的这个JQuery 插件 名叫Ajax Autocomplete 顾名思义 ajax 也就是用ajax的方式获取 ...

  2. FAST特征点检测features2D

    #include <opencv2/core/core.hpp> #include <opencv2/features2d/features2d.hpp> #include & ...

  3. webform--LinQ的相关操作

    LinQ:LineQ to Sq类:集成化的数据访问类:与ado.net没区别:--------------------------------------------LinQ的创建:右键,添加新建项 ...

  4. Frame URl

    http://www.zi-han.net/theme/hplus/?v=4.1 http://webapplayers.com/inspinia_admin-v2.5/ http://baijuny ...

  5. shell 脚本 exit 1 报错:numeric argument required问题解决

    原因是在window环境编辑会有特殊字符,解决办法:sed -i'' "s/\r//" file_name

  6. DHCP服务器原理

    DHCP服务器   port:67 DHCP 这个服务可以自动的分配 IP 与相关的网络参数给客户端, 来提供客户端自动以服务器提供的参数来设定他们的网络   12.1 DHCP 运作的原理      ...

  7. JavaScript中的枚举

    在JavaScript目前的版本中,没有枚举这个概念(当然,ECMA-262第三版中已经将enum作为关键字保留). 然而,如同JavaScript中没有class一样,但我们仍然可以通过间接的方式- ...

  8. PHP防SQL注入不要再用addslashes和mysql_real_escape_string

    PHP防SQL注入不要再用addslashes和mysql_real_escape_string了,有需要的朋友可以参考下. 博主热衷各种互联网技术,常啰嗦,时常伴有强迫症,常更新,觉得文章对你有帮助 ...

  9. 记一次项目中的css样式复用

    本文同步至微信公众号:http://mp.weixin.qq.com/s?__biz=MzAxMzgwNDU3Mg==&mid=401616238&idx=1&sn=3c6e9 ...

  10. http://www.360doc.com/content/13/0516/22/12094763_285956121.shtml

    http://www.360doc.com/content/13/0516/22/12094763_285956121.shtml