Skip to content

Instantly share code, notes, and snippets.

@kazuhito-m
Last active December 22, 2015 17:49
Show Gist options
  • Save kazuhito-m/6509007 to your computer and use it in GitHub Desktop.
Save kazuhito-m/6509007 to your computer and use it in GitHub Desktop.
* 今は亡きcatch.comのエクスポートファイルをsimplenote.comに移行するテキストを吐くツール。 * 独自形式のファイルを読み、変換し、xmlへと吐きだすサンプルでもある。
#! ruby -Ku
# 今は亡きcatch.comのエクスポートファイルをsimplenote.comに移行するテキストを吐くツール。
require "kconv"
require "rexml/document"
# 内部で使用する構造体風VOクラス。
class Note
@id = ""
@tags
@auther = ""
@date = ""
@content = ""
attr_accessor :id, :tags, :auther, :date, :content
def initialize()
@tags = Array.new
@content = ""
end
end
# catch.comの忘れ形見であるエクスポートファイルからオブジェクトをつくりだす。
def load_noteobjects_by_catchfile(target_file_path)
notes = Array.new
f = open(target_file_path)
i = 0
is_content_in = false
is_blank_line = false
now_note = Note.new()
f.each {|line|
part = line.chomp()
if part == '==================================================' then
# 本文に入ったかフラグをoff
is_content_in = false
# オブジェクト作成。
now_note = Note.new()
notes << now_note
i = i + 1
# debug
p "note : #{i}"
elsif part.include?("Note ID: ") then
now_note.id = part.partition("Note ID: ")[2]
elsif part.include?("Space\(s\): ") then
now_note.tags << "catch"
now_note.tags << part.partition("Space\(s\): ")[2]
elsif part.include?("By: ") then
now_note.auther = part.partition("By: ")[2]
elsif part.include?("Date: ") then
date_str = part.partition("Date: ")[2]
date_str = date_str.gsub(/\- /,"-0").gsub(/: /,":0").gsub(/ /,"T") + ":00"
now_note.date = date_str
is_blank_line = true
else
if is_blank_line then
is_blank_line = false
else
now_note.content << part + "\n"
end
end
# debug
# print(Kconv.tosjis(line))
}
f.close
return notes
end
def write_xml_by_notes()
out_file_name = "./sample7_out.xml"
out_doc = REXML::Document.new()
out_doc.add(REXML::XMLDecl.new(version="1.0", encoding="UTF-8"))
root_element = REXML::Element.new("notes")
out_doc.add_element(root_element)
note_element = REXML::Element.new("note")
root_element.add_element(note_element)
key_element = REXML::Element.new("key")
key_element.add_text("123410938531")
note_element.add_element(key_element)
created_element = REXML::Element.new("created")
created_element.add_text("2013-09-10T12:44:00")
note_element.add_element(created_element)
modified_element = REXML::Element.new("modified")
modified_element.add_text("2013-09-10T12:44:00")
note_element.add_element(modified_element)
tags_element = REXML::Element.new("tags")
note_element.add_element(tags_element)
tag_element = REXML::Element.new("tag")
tag_element.add_text("a")
tags_element.add_element(tag_element)
tag_element = REXML::Element.new("tag")
tag_element.add_text("b")
tags_element.add_element(tag_element)
content_element = REXML::Element.new("content")
content_element.add_text("naiyou ha naiyo")
note_element.add_element(content_element)
File.open(out_file_name,"w") do |outfile|
out_doc.write(outfile, -1 , true)
end
end
# 本処理。
notes = load_noteobjects_by_catchfile(ARGV[0])
# debug
p notes[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment