Skip to content

Instantly share code, notes, and snippets.

@labocho
Last active November 2, 2017 05:48
Show Gist options
  • Select an option

  • Save labocho/0825e51293584376e0b8a6bd06be2224 to your computer and use it in GitHub Desktop.

Select an option

Save labocho/0825e51293584376e0b8a6bd06be2224 to your computer and use it in GitHub Desktop.
Export HTML from Kobito.db with tags and dates (like Evernote)
#!/usr/bin/env ruby
# Usage: ./kobito2html.rb Kobito.db ./export
gem "activerecord", "~> 5.1.4"
gem "nokogiri", "~> 1.8.1"
gem "sqlite3", "~> 1.3.13"
require "active_record"
require "nokogiri"
require "sqlite3"
db, dest = ARGV
ActiveRecord::Base.establish_connection(
adapter: "sqlite3",
database: db
)
TIMESTAMP_OFFSET = Time.parse("2001-01-01T00:00:00Z").to_i
class Item < ActiveRecord::Base
self.table_name = "ZITEM"
self.primary_key = "Z_PK"
has_many :taggings, foreign_key: :Z_2ITEMS
has_many :tags, through: :taggings
def title
self.ZTITLE
end
def body
self.ZBODY
end
def body_with_metadata
doc = Nokogiri.parse(body)
head = doc.css("head")[0]
head << doc.create_element("meta", name: "keywords", content: tags.map(&:name).join(", "))
head << doc.create_element("meta", name: "created", content: created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z"))
head << doc.create_element("meta", name: "updated", content: updated_at.utc.strftime("%Y-%m-%d %H:%M:%S %z"))
doc.css("title")[0].content = title
doc.to_html
end
def created_at
Time.at(TIMESTAMP_OFFSET + self.ZCREATED_AT)
end
def updated_at
Time.at(TIMESTAMP_OFFSET + self.ZUPDATED_AT)
end
end
class Tag < ActiveRecord::Base
self.table_name = "ZTAG"
self.primary_key = "Z_PK"
def name
self.ZNAME
end
end
class Tagging < ActiveRecord::Base
self.table_name = "Z_2TAGS"
belongs_to :item, foreign_key: :Z_2ITEMS
belongs_to :tag, foreign_key: :Z_3TAGS
end
Item.all.each do |item|
filename = File.join(dest, "#{item.id}.html")
$stderr.puts "Export to #{filename}"
File.write(filename, item.body_with_metadata)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment