人気ブログランキング | 話題のタグを見る

txt2html.rb

テキストファイルをHTML形式に変換するフィルター txt2html.rb を書いた。久しぶりの Ruby スクリプトだ。フィルターとして使えるので次のような使い方ができる。

$ cat hello.txt | txt2html.rb
$ txt2html.rb hello.txt > hello.html

フィルタープログラムのいいところは、デスティネーションファイルに出力する前に標準入力で動作確認できることだ。txt2html.rb のスクリプトは次のようになる。

#!/usr/bin/ruby
line = gets
while line != nil
.. if line == "\n"
.... print "\n<p>"
.. else
.... puts line.gsub(/&/, '&amp;').gsub(/</, '&lt;').gsub(/>/, '&gt;').gsub(/"/, '&quot;')
.. end
..  line = gets
end



by tnomura9 | 2020-02-14 22:06 | HTML | Comments(0)
<< HTML 構成用シェルスクリプト 構造化タグの用例 >>