2010-07-30 142 views
2

有沒有什麼辦法可以在Mac上從命令行漂亮地打印HTML或XML字符串?試圖在Ruby中做到這一點。有任何想法嗎?我曾經想過XSLT,還有關於編寫我自己的解析器的問題,但這兩者都很相關。尋找已經存在的東西。在Ruby(或Shell)中漂亮的打印HTML

回答

5

XML文件

xmllint --format file.xml

HTML文件

xmllint --format --html file.html

http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/xmllint.1.html

+1

如何從stdin中讀取它? – 2011-05-05 05:46:24

+0

@ landon9720(抱歉格式不對) 'output = IO.popen(「xmllint --format --html - 」,「w +」)do | pipe | pipe.puts html pipe.close_write pipe.read end' – Stoffe 2011-09-15 15:28:50

+0

要從stdin中讀取,請使用 - 作爲文件名:xmllint --format - – 2013-04-04 23:54:21

0

在炮彈,另一種選擇是使用pup不帶參數:

pup 

XmlStarlet還支持使用HTML解析器。 foformat的簡稱。請參閱xml fo -h尋求幫助。

xml fo --html 

tidy主要實現不支持HTML5,但tidy-html5一樣。 brew install tidy-html5在OS X中安裝tidy-html5/usr/local/bin/tidy