Python automatically converts a markdown file to an html file

Source: Internet
Author: User
This article describes how to convert a markdown file to an html file automatically by using python. This article is very detailed and has reference value. For more information, see 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 very simple. when saving the file 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-*-# use python markdown_convert.py filenameimport sysimport markdownimport codecscss = '''
 '''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. I would like to thank you for your support for PHP chinnet!

For more articles related to python's automated conversion of markdown files into html files, please follow the PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.