Batch convert MD files to HTML files in Linux

Source: Internet
Author: User

To convert a markdown file to an HTML file, you can use the markdown provided by the discount or Python-markdown software package:

Conversion in Ubuntu is as follows:

# Debian/Ubuntu

Sudo apt-Get install discount

Or:

# Debian/Ubuntu

Sudo apt-Get install Python-markdown

The conversion is simple:

# Use the markdown tool provided by discount

Markdown-O Release-Notes.html Release-Notes.md

 

# Use the markdown_py tool provided by Python-markdown

Markdown_py-O html4 Release-Notest.md> Release-Notes.html

To generate a PDF file, you can use the xhtml2pdf file provided by Python-Pisa:

# Debian/Ubuntu

Sudo apt-Get install Python-Pisa

 

# Converting HTML to PDF

Xhtml2pdf -- HTML Release-Notes.html Release-Notes.pdf

Therefore, you can place such a makefile in the document directory to automate this process:

# Makefile

 

MD = markdown

Mdflags =-T

H2p = xhtml2pdf

H2pflags = -- html

Sources: = $ (wildcard *. md)

Objects: = $ (patsubst %. md, example .html, $ (wildcard *. md ))

Objects_pdf: = $ (patsubst %. md, commandid, $ (wildcard *. md ))

 

ALL: Build

 

Build: HTML PDF

 

PDF: $ (objects_pdf)

 

HTML: $ (objects)

 

$ (Objects_pdf): objects.html

$ (H2p) $ (h2pflags) $ <>$ @

 

$ (Objects): Objects .html: %. md

$ (MD) $ (mdflags)-o $ @ $ <

Clean:

Rm-F $ (objects)

In this way, you can use a simple command to generate PDF or HTML output of all MD files in the current directory:

# HTML output

Make html

 

# PDF output

Make PDF

Note: The problem here is that if the markdown content is Chinese, the converted HTML cannot be automatically recognized when opened in the browser. The PDF is worse, but it is a bunch of garbled characters. In this case, we can use markdown's support for HTML tags to add encoding information to the markdown file. For example, to convert markdown to an HTML file, you can add a meta tag at the beginning of the file to specify the encoding format:

Sed-I '1i \ <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"> '*. md

In this way, you can.

 

Reference: http://www.ituring.com.cn/article/10044

 

Related Article

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.