I. BACKGROUND
The documents written by our project developers are all markdown files. It is not convenient for other groups of students to read. Every time I edit the markdown file, I use the software to turn the MD file into an HTML file. At the beginning of the turn, there is nothing, turn more times, I feel that can not continue to go on like this. As a developer, let the machine do the trivial things. So two scripts were written to turn the MD file into an HTML file and place it under a Web server for other people to read.
There are two main scripts and one timed task:
• A Python script that mainly converts the MD file into an HTML file;
• A shell script, mainly for management logic;
• A Linux timed task that is mainly timed to execute shell scripts.
Second, using Python to convert markdown into HTML
2.1 Python Dependent libraries
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 in fact only one sentence, the execution Markdown.markdown (text) can get the original text of the generated HTML.
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
Direct Markdown.markdown (text) generated HTML text, very rough, just plain HTML content. And in the browser to see when the Chinese garbled (in chrome), there is no good CSS style, too ugly.
The solution is also very simple, when saving the document, will <meta http-equiv= "Content-type" content= "text/html;" Charset=utf-8 "/> and CSS style additions. It was so easy to solve.
2.4 Full Python content
• Read MD files;
• Convert the MD file into HTML text;
• Add CSS styles and save HTML text.
Python Code content:
#!/usr/bin/env python
#-*-coding:utf-8-*-
# using the method python markdown_convert.py filename
import sys
Import markdown
import codecs
css = "
<meta http-equiv=" Content-type "content=" text/html Utf-8 "/>
<style type=" Text/css ">
<!--omit the markdown 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:])
Third, Shell logic
3.1 Logical Description
Create a shell file for logical processing, the main operations are as follows:
• Update the SVN file to update the latest MD file (this assumes that the MD file is a test document. md);
• Execute Python markdown_convert.py $NAME convert the MD file into an HTML file (generate test document. html);
• Migrate the good HTML to the Web path (move to html/test document. html);
• Start a Web service (here is a Python simplehttpserver Web server).
3.2 Full Shell logic
#!/bin/bash
name= ' Test document
# # Update code
SVN update
# # Delete an HTML file
if [-f] $NAME. html "];then
RM" $ name.html "
fi
#" generates HTML
if [F "$NAME. MD"];then
python markdown_convert.py $NAME
fi
# Generate HTML directories
if [!-d "HTML"];then
mkdir "html"
fi
# Copy HTML file
if [-F "$NAME. html"];then
MV -F "$NAME. html" "html/"
fi
# Open Web server
pid= ' ps aux | grep ' python-m simplehttpserver 8080 ' | grep-v ' GRE P ' | awk ' {print $} '
if [' $PID ' = '];then
cd HTML
nohup python-m simplehttpserver 8080 &
Echo ' St Art Web server '
else
Echo ' already start '
fi
Four, Linux timing tasks
Enter the CRONTAB-E into the Linux Timing task editing interface under the shell command. Set the timing task for the markdown2web.sh script inside:
# # UPDATE Document */10 * * * *
cd/home/xxx/doc; sh markdown2web.sh >/dev/null 2>&1
The above is a small series to introduce the Python automation will markdown file into HTML file method, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!