Python automatically converts a markdown file to an html file,
I. background
All documents written by our project developers are markdown files. It is not convenient for other groups to read. After editing the markdown file, I use software to convert the md file into an html file. At the beginning of the conversion, there was no such thing as it had been transferred too many times, so I felt that I could not continue this way. As a developer, let the machine do these trivial tasks. Therefore, I wrote two scripts to convert the md file into an html file and place it on the web server for other people to read.
There are two scripts and one scheduled task:
• A python script that converts an md file into an html file;
• A shell script is mainly used to manage the logic;
• A linux scheduled task is mainly scheduled to execute shell scripts.
Ii. Convert markdown to html using python
2.1 python dependency Library
Using the markdown library of python to convert md files to html depends on two libraries:
• Pip install markdown
• Pip install importlib
2.2 core code
There is actually only one core code sentence. You can execute markdown. markdown (text) to obtain the original html text 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 styles
The html text directly generated by markdown. markdown (text) is very rough, just plain html content. In addition, Chinese characters are garbled (in chrome) when viewed in the browser, and there is no good-looking css style, which is too ugly.
The solution is also simple. When saving the file, set <meta http-equiv = "Content-Type" content = "text/html; add charset = UTF-8 "/> and css styles. This is a simple solution.
2.4 complete python content
• Read the md file;
• Convert the md file into html text;
• Add css styles and save html text.
Python code content:
#! /Usr/bin/env python #-*-coding: UTF-8-*-# usage: python markdown_convert.py filenameimport sysimport markdownimport codecscss = ''' <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "/> <style type =" text/css "> <! -- The markdown css style is omitted here, because it is too long --> </style> ''' def main (argv): name = argv [0] in_file = '% s. md '% (name) out_file = 'audio 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:])
Iii. shell Logic
3.1 logic description
Create a shell file for logical processing. The main operations are as follows:
• Update the svn file and update the latest md file (Here we assume that the md file is a test document. md );
• Execute python markdown_convert.py runtime );
• Migrate the converted html to the web path (to html/test document .html );
• Start a web Service (the SimpleHTTPServer web server of python is used here ).
3.2 complete shell Logic
#! /Bin/bashNAME = 'test document' # update the code svn update # Delete the html file if [-f "example"]; thenrm "temperature" fi # generate htmlif [-f "$ NAME. md "]; thenpython markdown_convert.py $ NAMEfi # generate the html directory if [! -D "html"]; thenmkdir "html" fi # copy an html file if [-f "else"]; thenmv-f "inherit" "html/" fi # enable the web server PID = 'ps aux | grep' python-m SimpleHTTPServer 100' | grep-v 'grep' | awk' {print $2} ''if [" $ PID "=" "]; thencd htmlnohup python-m SimpleHTTPServer 8080 & echo 'start web server' elseecho 'already start' fi
Iv. linux scheduled tasks
Enter crontab-e under the shell command to go To the linux scheduled task editing page. Set the scheduled task of the markdown2web. sh script in it:
# Update the document */10 *** cd/home/xxx/doc; sh markdown2web. sh>/dev/null 2> & 1
The above section describes how to automatically convert a markdown file to an html file in python. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!