I. Background
The documents written by our project developers are all markdown files. It is not very convenient for other groups of students to read. Every time I finish editing the markdown file, I use the software to convert the MD file into an HTML file. At the beginning of the turn, there is nothing, turn the number of times, I feel can not continue to do so. As a developer, let the machine do the trivial things. Therefore, two scripts were written to convert the MD file into an HTML file and put it under the Web server for easy reading by other people.
There are two main scripts and one scheduled task:
• A Python script that mainly converts the MD file into an HTML file;
• A shell script that is used primarily for management logic;
• A Linux timed task, mostly timed to execute shell scripts.
Second, use Python to turn markdown into HTML
2.1 Python Dependent libraries
Use Python's markdown library to convert MD files to HTML-dependent two libraries:
pip Install Markdown
pip Install importlib
2.2 Core Code
The core code actually only has one sentence, executes Markdown.markdown (text) can obtain the generated HTML the original text.
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
Direct Markdown.markdown (text) generated HTML text, very sketchy, is simply HTML content. And in the browser when viewing the Chinese garbled (in chrome), there is no good-looking CSS style, too ugly.
The solution is also very simple, when saving the file, will <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/> and CSS Styles added on. It's so easy to solve.
2.4 Full Python content
• Read MD files;
• Convert MD files to HTML text;
• Add CSS styles and save HTML text.
Python Code content:
#!/usr/bin/env python#-*-coding:utf-8-*-# How to use Python markdown_convert.py filenameimport sysimport markdownimport Code Cscss = "<meta http-equiv=" Content-type "content=" text/html; Charset=utf-8 "/><style type=" text/css "><!--omit the markdown CSS style here, 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", enc oding= "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 with the following main actions:
• 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 MD files into HTML files (generate test documents. html);
• Migrate the forwarded HTML to the Web path (move to the html/test document. html);
• Start a Web service (using Python's simplehttpserver Web server here).
3.2 Full Shell logic
#!/bin/bashname= ' Test document ' # # update code SVN update## Delete HTML file if [-F ' $NAME. html "];THENRM" $NAME. html "fi## generate Htmlif [-F" $NAME. MD " ];thenpython markdown_convert.py $NAMEfi # # generates an HTML directory if [!-d ' HTML '];thenmkdir ' HTML ' fi## copy HTML file if [-F ' $NAME. html "]; Thenmv-f "$NAME. html" "html/" fi## open Web server pid= ' ps aux | grep ' python-m simplehttpserver 8080 ' | Grep-v ' grep ' | awk ' {print $} ' if ["$PID" = ""];thencd htmlnohup python-m simplehttpserver 8080 &echo ' start Web server ' Elseecho ' already start ' fi
Iv. Linux timed Tasks
Enter CRONTAB-E into the Linux timed task editing interface under Shell commands. Set the timer 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 you to the Python automation of the markdown file into an HTML file method, I hope we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for your support for topic.alibabacloud.com!