Linux下批量將md檔案批量轉換為html檔案

來源:互聯網
上載者:User

要將markdown檔案轉換成html檔案,可以用discount或python-markdown軟體包提供的markdown:

以下主要介紹在Ubuntu系統下的轉化:

# Debian/Ubuntu

sudo apt-get install discount

或:

# Debian/Ubuntu

sudo apt-get install python-markdown

轉換工作很簡單:

# 用discount提供的markdown工具

markdown -o Release-Notes.html Release-Notes.md

 

# 用python-markdown提供的markdown_py工具

markdown_py -o html4 Release-Notest.md > Release-Notes.html

如果要產生PDF,也很簡單,可以用python-pisa提供的xhtml2pdf:

# Debian/Ubuntu

sudo apt-get install python-pisa

 

# 將html轉換成PDF

xhtml2pdf --html Release-Notes.html Release-Notes.pdf

所以,你可以在文檔目錄下放置這樣一個Makefile來自動這個過程:

# Makefile

 

MD = markdown

MDFLAGS = -T

H2P = xhtml2pdf

H2PFLAGS = --html

SOURCES := $(wildcard *.md)

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

OBJECTS_PDF := $(patsubst %.md, %.pdf, $(wildcard *.md))

 

all: build

 

build: html pdf

 

pdf: $(OBJECTS_PDF)

 

html: $(OBJECTS)

 

$(OBJECTS_PDF): %.pdf: %.html

    $(H2P) $(H2PFLAGS) $< > $@

 

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

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

clean:

    rm -f $(OBJECTS)

這樣你就可以通過簡單的一個命令產生目前的目錄下所有md檔案的pdf或html輸出了:

# html 輸出

make html

 

# pdf輸出

make pdf

注意:這裡有個問題是如果markdown的內容是中文,那麼轉換出來的html在瀏覽器中開啟就無法自動識別編碼,pdf更慘,直接是一堆亂碼。這時我們可以藉助markdown對html標記的支援來在markdown檔案中加入編碼資訊。例如我們要將markdown轉換為html檔案,可以在檔案的開頭加上meta標記,指明編碼格式:

sed -i '1i\<meta http-equiv="content-type" content="text/html; charset=UTF-8">' *.md

這樣就可以了。

 

參考連結:http://www.ituring.com.cn/article/10044

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.