Skip to content

Commit 61d8649

Browse files
committed
Add convert command which export .enex to Boostnote
1 parent 1a55105 commit 61d8649

File tree

6 files changed

+53
-10
lines changed

6 files changed

+53
-10
lines changed

lib/ever2boost/cli.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
require 'thor'
22
require 'ever2boost/evernote_authorizer'
3+
require 'ever2boost/enex_converter'
4+
require 'ever2boost/util'
35

46
module Ever2boost
57
class CLI < Thor
68
DEFAULT_OUTPUT_DIR = "#{ENV['HOME']}/evernote_storage"
79

810
desc "import", "import from evernote"
9-
option :directory, aliases: :d, banner: 'DIRCTORY_PATH', desc: "make Boostnote storage in the directory\ndefault: ~/evernote_storage"
11+
option :directory, aliases: :d, banner: 'DIRCTORY_PATH', desc: "make Boostnote storage in the directory default: ~/evernote_storage"
1012
def import
1113
output_dir = options[:directory] || DEFAULT_OUTPUT_DIR
14+
Util.make_output_dir(output_dir)
1215
developer_token = ask('DEVELOPER_TOKEN:')
1316
EvernoteAuthorizer.new(developer_token).import(output_dir)
1417
end
18+
19+
desc "convert", "convert fron .enex"
20+
option :directory, aliases: :d, banner: 'DIRCTORY_PATH', desc: "make Boostnote storage in the directory default: ~/evernote_storage"
21+
def convert(path)
22+
output_dir = options[:directory] || DEFAULT_OUTPUT_DIR
23+
Util.make_output_dir(output_dir)
24+
enex = File.read(path)
25+
filename = File.basename(path, ".enex")
26+
EnexConverter.convert(enex, output_dir, filename)
27+
end
1528
end
1629
end

lib/ever2boost/cson_generator.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ module Ever2boost
22
class CsonGenerator
33
class << self
44
def build(folder_hash, note)
5-
md_note_content = MdConverter.convert(note.content)
65
cson = <<-EOS
76
type: "MARKDOWN_NOTE"
87
folder: "#{folder_hash}"
98
title: "#{note.title}"
109
content: '''
1110
# #{note.title}
12-
#{md_note_content}
11+
#{note.md_content}
1312
'''
1413
tags: []
1514
isStarred: false
@@ -23,7 +22,6 @@ def timestamp
2322
end
2423

2524
def output(folder_hash, note, output_dir)
26-
FileUtils.mkdir_p("#{output_dir}/notes") unless FileTest.exist?("#{output_dir}/notes")
2725
File.open("#{output_dir}/notes/#{note.file_name}.cson", "w") do |f|
2826
f.write(self.build(folder_hash, note))
2927
end

lib/ever2boost/enex_converter.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'rexml/document'
2+
3+
module Ever2boost
4+
class EnexConverter
5+
class << self
6+
def convert(enex, output_dir, filename)
7+
en_notes = []
8+
REXML::Document.new(Ever2boost::MdConverter.convert(enex)).elements['en-export'].each { |el| en_notes.push(el.to_s) }
9+
en_notes.delete("\n")
10+
notes = en_notes.map { |note|
11+
title = REXML::Document.new(note).elements['note/title'].text
12+
Note.new(title: title, content: "<div>#{REXML::Document.new(note).elements['note/content/text()'].value.sub(/.+\n/, '').sub(/.+\n/, '')}</div>")
13+
}
14+
notebook_list = [NoteList.new(title: filename)]
15+
JsonGenerator.output(notebook_list, output_dir)
16+
notes.each do |note|
17+
CsonGenerator.output(notebook_list.first.hash, note, output_dir)
18+
end
19+
end
20+
end
21+
end
22+
end

lib/ever2boost/md_converter.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'rexml/document'
2-
require 'byebug'
32

43
module Ever2boost
54
class MdConverter
@@ -8,11 +7,15 @@ def convert(note_content)
87
en_note = nil
98
# TODO: convert evernote xml to md
109
REXML::Document.new(note_content).elements.each('*') {|el| en_note = el.to_s || en_note + el.to_s }
11-
en_note.gsub(/<en-note>/, '')
12-
.gsub(/<\/en-note>/, '')
13-
.gsub(/<div>/, '')
14-
.gsub(/<\/div>/, '')
15-
.gsub(/<br\/>/, '\n\r')
10+
if en_note.nil?
11+
note_content
12+
else
13+
en_note.gsub(/<en-note>/, '')
14+
.gsub(/<\/en-note>/, '')
15+
.gsub(/<div>/, '')
16+
.gsub(/<\/div>/, '')
17+
.gsub(/<br\/>/, '\n\r')
18+
end
1619
end
1720
end
1821
end

lib/ever2boost/note.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ def initialize(title: nil, content: nil, notebook_guid: nil)
1010
@notebook_guid = notebook_guid
1111
@file_name = SecureRandom.hex(DEFAULT_BYTES_NUMBER)
1212
end
13+
14+
def md_content
15+
MdConverter.convert(self.content)
16+
end
1317
end
1418
end

lib/ever2boost/util.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module Ever2boost
22
class Util
33
class << self
4+
def make_output_dir(output_dir)
5+
FileUtils.mkdir_p("#{output_dir}/notes") unless FileTest.exist?("#{output_dir}/notes")
6+
end
47
end
58
end
69
end

0 commit comments

Comments
 (0)