Class: KoboNotesBinder::Runner
- Inherits:
-
Object
- Object
- KoboNotesBinder::Runner
show all
- Defined in:
- lib/kobo_notes_binder/runner.rb
Defined Under Namespace
Classes: Bookmark, Content
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(opts = {}, kobo_device_path:) ⇒ Runner
Returns a new instance of Runner.
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/kobo_notes_binder/runner.rb', line 21
def initialize(opts = {}, kobo_device_path:)
@kobo_device_path = kobo_device_path
@local_kobo_db_path = opts[:local_kobo_db_path]
@local_original_epub_path = opts[:local_original_epub_path]
@volume_url = opts[:volume_url]
@tmp_dir = opts[:tmp_dir]
@debug_mode = opts[:debug_mode]
require "sqlite3"
require "tty-prompt"
require 'nokogiri'
require 'fileutils'
begin
require 'pry-byebug'
rescue LoadError
end
if tmp_dir.nil?
puts 'Creating temporary folder'
@tmp_dir = Dir.mktmpdir
puts ' ' + tmp_dir
at_exit do
puts 'Cleaning up'
FileUtils.remove_entry(tmp_dir)
end
end
end
|
Instance Attribute Details
#debug_mode ⇒ Object
Returns the value of attribute debug_mode.
19
20
21
|
# File 'lib/kobo_notes_binder/runner.rb', line 19
def debug_mode
@debug_mode
end
|
#kobo_device_path ⇒ Object
Returns the value of attribute kobo_device_path.
14
15
16
|
# File 'lib/kobo_notes_binder/runner.rb', line 14
def kobo_device_path
@kobo_device_path
end
|
#local_kobo_db_path ⇒ Object
Returns the value of attribute local_kobo_db_path.
16
17
18
|
# File 'lib/kobo_notes_binder/runner.rb', line 16
def local_kobo_db_path
@local_kobo_db_path
end
|
#local_original_epub_path ⇒ Object
Returns the value of attribute local_original_epub_path.
17
18
19
|
# File 'lib/kobo_notes_binder/runner.rb', line 17
def local_original_epub_path
@local_original_epub_path
end
|
#tmp_dir ⇒ Object
Returns the value of attribute tmp_dir.
15
16
17
|
# File 'lib/kobo_notes_binder/runner.rb', line 15
def tmp_dir
@tmp_dir
end
|
#volume_url ⇒ Object
Returns the value of attribute volume_url.
18
19
20
|
# File 'lib/kobo_notes_binder/runner.rb', line 18
def volume_url
@volume_url
end
|
Instance Method Details
#compile(volume_url:, tmp_dir:, raw_epub_folder:) ⇒ Object
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/kobo_notes_binder/runner.rb', line 252
def compile(volume_url:, tmp_dir:, raw_epub_folder:)
epub_file = tmp_dir + '/' + File.basename(volume_url).sub('.', '.highlights.')
files = *Dir[raw_epub_folder + '/*']
mime_path = files.find { |path| File.basename(path) == 'mimetype' }
files.delete mime_path
files.unshift mime_path
files = files.map { |path| Pathname.new(path).relative_path_from(raw_epub_folder).to_s }
FileUtils.cd raw_epub_folder do
system 'zip', '-rX', epub_file, *files
end
epub_file
end
|
#copy_book(book, dir:, kobo_device_path:) ⇒ Object
127
128
129
130
131
132
|
# File 'lib/kobo_notes_binder/runner.rb', line 127
def copy_book(book, dir:, kobo_device_path:)
volume_url = book.ContentID
file_path = dir + "/" + File.basename(volume_url)
FileUtils.cp(kobo_device_path + volume_url.sub('file:///mnt/onboard', ''), file_path)
file_path
end
|
#copy_db(kobo_device_path, dir:) ⇒ Object
242
243
244
245
246
247
248
249
250
|
# File 'lib/kobo_notes_binder/runner.rb', line 242
def copy_db(kobo_device_path, dir:)
kobo_db_path = kobo_device_path + '/.kobo/KoboReader.sqlite'
db_path = dir + '/KoboReader.sqlite'
FileUtils.rm_rf db_path
puts " Coping from " + kobo_db_path
puts " Coping to " + db_path
FileUtils.cp(kobo_db_path, db_path)
db_path
end
|
#debug ⇒ Object
271
272
273
274
275
276
277
278
|
# File 'lib/kobo_notes_binder/runner.rb', line 271
def debug
if debug_mode
result = yield
if result === String
puts result
end
end
end
|
#execute ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/kobo_notes_binder/runner.rb', line 50
def execute
puts 'Device path ' + kobo_device_path
if local_kobo_db_path.nil?
puts 'Copying kobo database'
local_kobo_db_path = copy_db kobo_device_path, dir: tmp_dir
end
puts 'Connecting to database'
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: local_kobo_db_path,
)
if local_original_epub_path.nil?
book = pick_book
puts 'Copying selected book'
local_original_epub_path = copy_book(book, dir: tmp_dir, kobo_device_path: kobo_device_path)
end
puts 'Extracting book content'
raw_epub_folder = (local_original_epub_path, dir: tmp_dir)
volume_url ||= book.ContentID
puts "Notes volume id: #{volume_url}"
puts 'Searching for book notes'
notes = Bookmark.where(VolumeID: volume_url)
puts " Found #{notes.size} notes"
if debug_mode
puts 'Cloning raw folder'
clone_raw_epub_folder = File.dirname(raw_epub_folder) + '/raw_original'
FileUtils.rm_rf clone_raw_epub_folder
FileUtils.cp_r raw_epub_folder, clone_raw_epub_folder
end
puts 'Embedding notes'
notes.each do |note|
process_note note, raw_epub_folder: raw_epub_folder
end
puts 'Binding book'
new_epub_path = compile(volume_url: volume_url, tmp_dir: tmp_dir, raw_epub_folder: raw_epub_folder)
puts 'Book is bound'
new_epub_path
end
|
134
135
136
137
138
139
|
# File 'lib/kobo_notes_binder/runner.rb', line 134
def (path, dir:)
raw_epub_folder = dir + '/raw'
FileUtils.rm_rf raw_epub_folder
system 'unzip', path, '-d', raw_epub_folder
raw_epub_folder
end
|
#file_path(note) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/kobo_notes_binder/runner.rb', line 99
def file_path(note)
volume_url = note.VolumeID
volume_path = volume_url.sub('file://', '')
content_id = note.ContentID
path = content_id.sub volume_path, ''
path.gsub('!', '/').split('#', 2)[0]
end
|
#highlight_node(node) ⇒ Object
141
142
143
144
145
146
147
148
149
|
# File 'lib/kobo_notes_binder/runner.rb', line 141
def highlight_node(node)
if node.content[/\S/]
node.wrap("<span style='background-color: #FDE383;' class='kobo-notes-binder-highlight'></span>")
node.parent
else
node
end
end
|
#pick_book ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/kobo_notes_binder/runner.rb', line 111
def pick_book
book_list = Content.joins(:bookmarks).group(:Title).order('Max(Bookmark.DateCreated) DESC')
prompt = TTY::Prompt.new
prompt.on(:keyescape) { exit }
book = prompt.select(
"What book would you like to export?", per_page: 7, filter: true
) do ||
book_list.each do |book|
.choice book.Title, book
end
end
book
end
|
#process_note(note, raw_epub_folder:) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/kobo_notes_binder/runner.rb', line 151
def process_note(note, raw_epub_folder:)
debug do
puts
puts 'processing note: '
pp note
nil
end
if note.StartContainerChildIndex != -99 || note.EndContainerChildIndex != -99
debug { ' Skipping note' }
return
end
xml_path = raw_epub_folder + file_path(note)
xml_string = File.read(xml_path)
doc = Nokogiri::XML(xml_string)
start_node = doc.at_css note.StartContainerPath
end_node = doc.at_css note.EndContainerPath
parent = start_node
loop do
match = parent.to_enum(:traverse).find do |node|
node == end_node
end
break if match
parent = parent.parent
end
nodes = parent.to_enum(:traverse).to_a
selected_nodes = nodes[nodes.index(start_node)...nodes.index(end_node)]
selected_nodes = start_node.to_enum(:traverse).to_a + selected_nodes
selected_nodes.select(&:text?).each do |node|
next if node.previous_sibling && node.previous_sibling['class'] == 'kobo-notes-binder-highlight'
at_start = node.parent == start_node
at_end = node.parent == end_node
start_offset = note.StartOffset
end_offset = note.EndOffset
content = node.content
if at_start && at_end
text_before = content[0...start_offset]
text_highlight = content[start_offset...end_offset]
text_after = content[end_offset..-1]
elsif at_start
text_before = content[0...start_offset]
text_highlight = content[start_offset..-1]
text_after = ''
elsif at_end
text_before = ''
text_highlight = content[0...end_offset]
text_after = content[end_offset..-1]
else
text_before = ''
text_highlight = content
text_after = ''
end
if note.BookmarkID == 'ee31725b-d579-4a69-972c-f0bdb740bcb1'
end
node.content = text_highlight
if text_highlight.size > 0
node = highlight_node(node)
end
if text_before.size > 0
node.add_previous_sibling doc.create_text_node(text_before)
end
if text_after.size > 0
node.add_next_sibling doc.create_text_node(text_after)
end
end
File.write xml_path, doc.to_xhtml
end
|