python automates the process of converting an markdown file into an html file

  • 2020-05-12 02:51:48
  • OfStack

1. The background

The documents written by our project developers are all markdown files. It's not very convenient for other groups to read. Every time I edit the markdown file, I use the software to convert the md file into the html file. At the beginning of the turn, what is not, turn more times, feel can not continue to go on like this. As a developer, let the machine do the little things. Therefore, I wrote two scripts to convert the md file into html file and put it under the web server for others to read.

There are two scripts and one timing task:

The & # 8226; 1 python script, mainly turning md files into html files;

The & # 8226; 1 shell script for managing logic;

The & # 8226; 1 linux timing task, which mainly executes shell script at a fixed time.

2. Convert markdown to html using python

2.1 python library dependent

Using python's markdown library to convert md files to html relies on two libraries:

•pip install markdown

•pip install importlib

2.2 core code

The core code is actually only one sentence long, and markdown.markdown (text) is executed to get the original html generated.


input_file = codecs.open(in_file, mode="r", encoding="utf-8")
text = input_file.read()
html = markdown.markdown(text)

2.3 html encoding and html style

The html text generated directly by markdown.markdown (text) is very rough and just pure html content. Besides, when viewing in the browser, the Chinese characters are scrambled (in chrome), and there is no good-looking css style, which is too ugly.

The solution is also simple, when saving the file, will < meta http-equiv="Content-Type" content="text/html; charset=utf-8" / > And css style added on. It's that simple.

2.4 complete content of python

The & # 8226; Read the md file;

The & # 8226; Convert md files into html text;

The & # 8226; Add the css style and save the html text.

python code content:


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#  Method of use  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">
<!--  Omit here markdown the css Style, because it's too long  -->
</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:])

3. shell logic

3.1 logical description

Create an shell file for logical processing. The main operations are as follows:

The & # 8226; Update the svn file to bring down the latest md file (assume that md file is the test document.md);

The & # 8226; Execute python markdown_convert.py $NAME convert md file into html file (generate test document.html);

The & # 8226; Migrate the improved html to the web path (move to html/ test document.html);

The & # 8226; Start one web service (python's SimpleHTTPServer's web server is used here).

3.2 complete shell logic


#!/bin/bash
NAME=' Test documentation '
##  Update the code 
svn update
##  delete html file 
if [ -f "$NAME.html" ];then
rm "$NAME.html"
fi
##  generate html
if [ -f "$NAME.md" ];then
python markdown_convert.py $NAME
fi
##  generate html directory 
if [ ! -d "html" ];then
mkdir "html"
fi
##  copy html file 
if [ -f "$NAME.html" ];then
mv -f "$NAME.html" "html/"
fi
##  open web The server 
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

4. linux timing task

Enter crontab-e under the shell command to enter the linux timing task editing interface. Inside, set the timing task of the markdown2web. sh script:


##  Update the document 
*/10 * * * * cd /home/xxx/doc; sh markdown2web.sh > /dev/null 2>&1

Related articles: