Created
December 17, 2010 14:23
-
-
Save rummelonp/745005 to your computer and use it in GitHub Desktop.
エクスポートされたEvernoteXMLフォーマット(*.enex)ファイルからはてなブックマークにインポート出来るNetscapeBookmarkFileを生成するRubyスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require 'nokogiri' | |
require 'time' | |
module Evernote | |
def self.load_file(path) | |
doc = Nokogiri.XML open(path) | |
parse_document doc | |
end | |
private | |
def self.parse_document(doc) | |
doc.css('note').map do |note| | |
Note.parse note | |
end | |
end | |
def parse_element(element) | |
element.text if element | |
end | |
def parse_time_element(element) | |
Time.parse element.text if element | |
end | |
class Note | |
extend Evernote | |
attr_accessor :title, :content, :created, :updated, :tags, :attribute | |
def self.parse(doc) | |
note = self.new | |
note.title = parse_element doc.at('title') | |
note.content = parse_element doc.at('content') | |
note.created = parse_time_element doc.at('created') | |
note.updated = parse_time_element doc.at('updated') | |
note.tags = doc.css('tag').map do |tag| | |
tag.text | |
end | |
note.attribute = Attribute.parse doc.at('note-attributes') | |
note | |
end | |
class Attribute | |
extend Evernote | |
attr_accessor :subject_date, :source, :source_url | |
def self.parse(doc) | |
attribute = self.new | |
return attribute unless doc | |
attribute.subject_date = parse_time_element doc.at('subject-date') | |
attribute.source = parse_element doc.at('source') | |
attribute.source_url = parse_element doc.at('source-url') | |
attribute | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
## evernote2bookmark is a command line script for convert to netscape bookmark file from exported evernote file. | |
## | |
## Usage: evernote2bookmark.rb [options] file ... | |
## | |
require 'optparse' | |
def usage | |
File.readlines(__FILE__).grep(/^##.*/).map do |line| | |
line.chomp[3..-1] | |
end.join("\n") | |
end | |
options = { | |
include_tags: [], | |
exclude_tags: [], | |
output_file: nil | |
} | |
# Parse Options. | |
opt = OptionParser.new do |opt| | |
opt.banner = usage | |
include_text = 'Include Tags Separate Comma. (If tag be both matches are exclude.)' | |
opt.on('-i [tag,...]', '--include-tags [tag,...]', include_text) do |v| | |
options[:include_tags] = v.split(',').map(&:strip) | |
end | |
exclude_text = 'Exclude Tags Separate Comma.' | |
opt.on('-e [tag,...]', '--exclude-tags [tag,...]', exclude_text) do |v| | |
options[:exclude_tags] = v.split(',').map(&:strip) | |
end | |
output_text = 'Output File Name.' | |
opt.on('-o [file]', '--output-file [file]', output_text) do |v| | |
options[:output_file] = v | |
end | |
opt.on('-h', '--help', 'Show This Help Message.') do | |
puts opt.help | |
exit | |
end | |
opt.parse! ARGV | |
end | |
if ARGV.empty? | |
puts opt.help | |
exit | |
end | |
file_paths = ARGV | |
# Loading Evernote Module. | |
require File.join File.dirname(File.expand_path(__FILE__)), 'evernote' | |
# All XML File Converting to Object. | |
notes = file_paths.inject([]) do |notes, path| | |
notes.concat Evernote.load_file(path) | |
end | |
# Select Web Clip Notes. | |
notes = notes.select do |note| | |
note.attribute.source_url | |
end | |
# Select Notes if Specified include tags. | |
unless options[:include_tags].empty? | |
notes = notes.select do |note| | |
note.tags.any? do |tag| | |
options[:include_tags].include? tag | |
end | |
end | |
end | |
# Detect Notes if Specified exclude tags. | |
unless options[:exclude_tags].empty? | |
notes = notes.reject do |note| | |
note.tags.any? do |tag| | |
options[:exclude_tags].include? tag | |
end | |
end | |
end | |
# Generate Netsace Bookmark File. | |
items = notes.map do |note| | |
"<DT><A HREF=\"#{note.attribute.source_url}\" ADD_DATE=\"#{note.created.to_i}\" PRIVATE=\"0\" TAGS=\"#{note.tags.join(',')}\">#{note.title}</A>" | |
end | |
xml = <<XML | |
<!DOCTYPE NETSCAPE-Bookmark-file-1> | |
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"> | |
<TITLE>Bookmarks</TITLE> | |
<H1>Bookmarks</H1> | |
<DL><p>#{items.join("\n")} | |
</DL><p> | |
XML | |
# Output to file if Specified File. Otherwise Output to Standard Output. | |
if options[:output_file] | |
open(options[:output_file], 'w') do |file| | |
file.puts xml | |
end | |
else | |
puts xml | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment