ローカルのテキストファイルの読み込み・保存プラグイン

何となく書いてみた。
ローカルのファイルを変換する場面って結構ありそうな気がするけれど、
探した場所がいけなかったのか、需要がないのか、それらしいプラグインが見つからなかった。
フォーマット、コメントのつけ方などこれでいいのか疑問もあったけれど、公開。

  • plugin/Text/load.rb
# load local text file
# 
# - module: Text::load
#   config:
#     filename: /home/foo/filename.txt
#

def load(config, data)
  require 'kconv'

  begin
    text = ''
    File::open(config["filename"]) {|f|
      text = f.read.toutf8
    }
    return text
  rescue 
    puts "Cannot load file; #{config["filename"]}"
    return []
  end
end
  • plugin/Text/save.rb
# save local text file
# 
# - module: Text::save
#   config:
#     filename: /home/foo/filename.txt
#

def save(config, data)
  require 'kconv'

  begin
    File.open(config["filename"], "w") { |f|
      f.write(data.to_s.toutf8)
    }
  rescue 
    puts "Cannot write file; #{config["filename"]}"
  end

  return data
end