パスワードを忘れた? アカウント作成
698207 journal

soramineの日記: 習作 txt2html.rb 1

日記 by soramine
#!/usr/local/bin/ruby

############################################################
# テキスト文書に<BR>タグをつけてhtml化するスクリプト
# txt2html.rb
#
# ■目標
# ・タグをつける
# ・特殊文字変換
# ・ファイル読みこみ(シェルで実行する)
#
############################################################

##### メインスクリプト
print "======================================== \n"
print "script txt2html.rb start \n\n"

### 引数ARGV取得
ARGV.each do |argname|
    print "read argument> " , argname , "\n"

    ### ファイル拡張子のチェック(.txtでなければ次へ)
    unless argname =~ /^.*\.txt$/ then
        print "this file is not .txt file, next process. \n\n"
        next
    end

    ### ファイル存在チェック
    unless FileTest.exist?(argname) then
        print "this file is not found, next process. \n\n"
        next
    end

    ### ファイルオープン、処理
    print "htmlfile open\n"
    wahtmlfile = open(argname.sub(/\.txt$/, "\.html"), "w")
    print "textfile open\n"
    open(argname, "r").readlines.each { |txtline|
        wahtmlfile.print( txtline.gsub(/\&/, "&#38").gsub(/\</, "&#60").gsub(/\>/, "&#62").gsub(/\"/, "&#34").gsub(/(\r\n|\n)/,"<br>\\1") )
    }
    print "textfile close\n"
    print "htmlfile close\n"
    wahtmlfile.close
    print "\n"

end

### 終了
print "script finish. \n"
print "======================================== \n"
__END__

*****
註:&は本当は小文字です(変換されちゃうので大文字にしてごまかしですの)
この議論は賞味期限が切れたので、アーカイブ化されています。 新たにコメントを付けることはできません。
typodupeerror

にわかな奴ほど語りたがる -- あるハッカー

読み込み中...