kobo_notes_binder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 418adf1f681a78420171c3ac2d84b42841a56f6ea9fb4f7a8909cfa738c0bd68
4
+ data.tar.gz: f751e9667b16923094e2732dc58dcca853f823c6c767fff41680d534cbe0e245
5
+ SHA512:
6
+ metadata.gz: 66e3b11c4a96d6a1f2d35b762007bf7d1c048134f1856cbed12acdb89cf39d92b4fe28275e752e5b2e9aa89b57e785acdf7d631d92662c3abb1f520275470d7f
7
+ data.tar.gz: 6ea74f68860a7c9abedab063ffe47011c4386bb6448d22f12778d51a95b4f4eaa100f3c7c9f1513a8951cd7b330d40a64357c77e41c37e0380374bec3458ea2f
@@ -0,0 +1,16 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.6.5
14
+ bundler-cache: true
15
+ - name: Run the default task
16
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
3
+ This project adheres to [Semantic Versioning](http://semver.org/).
4
+
5
+ ## [Unreleased]
6
+
7
+ ## [0.1.0] - 2021-07-22
8
+
9
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at antulik@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in kobo_notes_binder.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "pry-byebug"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Anton Katunin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # KoboNotesBinder
2
+
3
+ KoboNotesBinder exports your Kobo book highlights bound inside the book.
4
+ In other words, you get a copy of the book with your markings in it.
5
+
6
+ This is an early version and most likely there cases that it doesn't support. If you found issues or have ideas please raise them here.
7
+
8
+ The tool never changes files on the device, so it is safe to use it.
9
+
10
+ ## Installation
11
+
12
+ $ gem install kobo_notes_binder
13
+
14
+ ## Usage
15
+
16
+ 1. Plug-in Kobo device
17
+ 2. Run command `kobo_notes_binder` in terminal and follow instructions
18
+ 3. Find book on your Desktop
19
+
20
+ ## Advance Usage
21
+
22
+ `kobo_notes_binder -h`
23
+
24
+ ## Features
25
+
26
+ - Export embedded book highlights
27
+
28
+ Not tested
29
+ - Ruby version <2.6
30
+ - Non-DRM books purchased in Kobo store
31
+
32
+ Not supported
33
+ - Export of written annotations
34
+ - Export of page bookmarks
35
+ - Color customisation
36
+ - DRM books
37
+
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/antulik/kobo_notes_binder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/antulik/kobo_notes_binder/blob/master/CODE_OF_CONDUCT.md).
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
52
+
53
+ ## Code of Conduct
54
+
55
+ Everyone interacting in the KoboNotesBinder project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/antulik/kobo_notes_binder/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "kobo_notes_binder"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "kobo_notes_binder/cli"
4
+
5
+ # Memo for Anton
6
+ # be kobo_notes_binder --kobo="/Users/anton/projects/kobo_notes_binder/tmp/fake_kobo_device" --open --application="Adobe Digital Editions 4.5.app"
7
+
8
+ KoboNotesBinder::Cli.new.start(ARGV)
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/kobo_notes_binder/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "kobo_notes_binder"
7
+ spec.version = KoboNotesBinder::VERSION
8
+ spec.authors = ["Anton Katunin"]
9
+ spec.email = ["antulik@gmail.com"]
10
+
11
+ spec.summary = "Command line to export Kobo device notes bound in the book"
12
+ spec.description = "Command line to export Kobo device notes bound in the book"
13
+ spec.homepage = "https://github.com/antulik/kobo_notes_binder"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'https://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/antulik/kobo_notes_binder"
21
+ spec.metadata["changelog_uri"] = "https://github.com/antulik/kobo_notes_binder/blob/master/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ spec.add_dependency "sqlite3"
34
+ spec.add_dependency "activerecord"
35
+ spec.add_dependency "tty-prompt"
36
+ spec.add_dependency "nokogiri"
37
+
38
+ # For more information and examples about making a new gem, checkout our
39
+ # guide at: https://bundler.io/guides/creating_gem.html
40
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "kobo_notes_binder/version"
4
+
5
+ module KoboNotesBinder
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,69 @@
1
+ require_relative 'runner'
2
+ require 'optparse'
3
+
4
+ class KoboNotesBinder::Cli
5
+ def start(argv)
6
+ argv = argv.dup
7
+ defaults = {
8
+ kobo_device_path: '/Volumes/KOBOeReader',
9
+ output_folder: '~/Desktop',
10
+
11
+ # Dev data
12
+ # tmp_dir: '/Users/anton/projects/kobo_notes_binder/tmp/tmp_dir',
13
+ # local_kobo_db_path: '/Users/anton/projects/ank/tmp/tmp_dir/KoboReader.sqlite',
14
+ # local_original_epub_path: '/Users/anton/projects/ank/tmp/tmp_dir/Nine Lies About Work_ A Freethinking Leader’s Guide to the Real World.kepub.epub',
15
+ # volume_url: "file:///mnt/onboard/kepub/Nine Lies About Work_ A Freethinking Leader’s Guide to the Real World.kepub.epub"
16
+ }
17
+ options = parse(argv, defaults)
18
+ puts options
19
+
20
+ runner = KoboNotesBinder::Runner.new(options, kobo_device_path: options[:kobo_device_path])
21
+ tmp_epub_path = runner.execute
22
+
23
+ puts 'Moving book to desktop'
24
+
25
+ new_epub_path = File.expand_path(options[:output_folder]) + '/' + File.basename(tmp_epub_path)
26
+ FileUtils.mv tmp_epub_path, new_epub_path
27
+
28
+ if options[:open]
29
+ cmd_args = ['open']
30
+
31
+ if options[:application_name]
32
+ cmd_args += ['-a', options[:application_name]]
33
+ end
34
+
35
+ puts 'Opening file'
36
+ system *cmd_args, new_epub_path
37
+ end
38
+ end
39
+
40
+ def parse(argv, defaults)
41
+ options = {}.merge defaults
42
+ OptionParser.new do |opts|
43
+ opts.banner = "Usage: kobo_notes_binder [options]"
44
+
45
+ opts.on("-kPATH", "--kobo=PATH",
46
+ "Path to kobo device. Default: #{options[:kobo_device_path]}") do |v|
47
+ options[:kobo_device_path] = v
48
+ end
49
+ opts.on("-oPATH", "--output=PATH", "Output folder. Default: #{options[:output_folder]}") do |v|
50
+ options[:output_folder] = v
51
+ end
52
+ opts.on("-p", "--open", "Open file at the end") do |v|
53
+ options[:open] = v
54
+ end
55
+ opts.on("-aNAME", "--application=NAME", "Application name to open epub") do |v|
56
+ options[:application_name] = v
57
+ end
58
+ opts.on("-d", "--debug", "Enable debug mode") do |v|
59
+ options[:debug_mode] = v
60
+ end
61
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
62
+ options[:verbose] = v
63
+ end
64
+ end.parse!(argv)
65
+
66
+ options
67
+ end
68
+
69
+ end
@@ -0,0 +1,282 @@
1
+ require "active_record"
2
+
3
+ class KoboNotesBinder::Runner
4
+ class Bookmark < ActiveRecord::Base
5
+ self.table_name = :Bookmark
6
+ end
7
+
8
+ class Content < ActiveRecord::Base
9
+ self.table_name = :content
10
+
11
+ has_many :bookmarks, primary_key: :ContentID, foreign_key: :VolumeID
12
+ end
13
+
14
+ attr_accessor :kobo_device_path
15
+ attr_accessor :tmp_dir
16
+ attr_accessor :local_kobo_db_path
17
+ attr_accessor :local_original_epub_path
18
+ attr_accessor :volume_url
19
+ attr_accessor :debug_mode
20
+
21
+ def initialize(opts = {}, kobo_device_path:)
22
+ @kobo_device_path = kobo_device_path
23
+ @local_kobo_db_path = opts[:local_kobo_db_path]
24
+ @local_original_epub_path = opts[:local_original_epub_path]
25
+ @volume_url = opts[:volume_url]
26
+ @tmp_dir = opts[:tmp_dir]
27
+ @debug_mode = opts[:debug_mode]
28
+
29
+ require "sqlite3"
30
+ require "tty-prompt"
31
+ require 'nokogiri'
32
+ require 'fileutils'
33
+
34
+ begin
35
+ require 'pry-byebug'
36
+ rescue LoadError
37
+ end
38
+
39
+ if tmp_dir.nil?
40
+ puts 'Creating temporary folder'
41
+ @tmp_dir = Dir.mktmpdir
42
+ puts ' ' + tmp_dir
43
+ at_exit do
44
+ puts 'Cleaning up'
45
+ FileUtils.remove_entry(tmp_dir)
46
+ end
47
+ end
48
+ end
49
+
50
+ def execute
51
+ puts 'Device path ' + kobo_device_path
52
+
53
+ if local_kobo_db_path.nil?
54
+ puts 'Copying kobo database'
55
+ local_kobo_db_path = copy_db kobo_device_path, dir: tmp_dir
56
+ end
57
+
58
+ puts 'Connecting to database'
59
+ ActiveRecord::Base.establish_connection(
60
+ adapter: 'sqlite3',
61
+ database: local_kobo_db_path,
62
+ )
63
+
64
+ if local_original_epub_path.nil?
65
+ book = pick_book
66
+ puts 'Copying selected book'
67
+ local_original_epub_path = copy_book(book, dir: tmp_dir, kobo_device_path: kobo_device_path)
68
+ end
69
+
70
+ puts 'Extracting book content'
71
+ raw_epub_folder = extract_epub(local_original_epub_path, dir: tmp_dir)
72
+
73
+ volume_url ||= book.ContentID
74
+ puts "Notes volume id: #{volume_url}"
75
+
76
+ puts 'Searching for book notes'
77
+ notes = Bookmark.where(VolumeID: volume_url)
78
+ puts " Found #{notes.size} notes"
79
+
80
+ if debug_mode
81
+ puts 'Cloning raw folder'
82
+ clone_raw_epub_folder = File.dirname(raw_epub_folder) + '/raw_original'
83
+ FileUtils.rm_rf clone_raw_epub_folder
84
+ FileUtils.cp_r raw_epub_folder, clone_raw_epub_folder
85
+ end
86
+
87
+ puts 'Embedding notes'
88
+ notes.each do |note|
89
+ process_note note, raw_epub_folder: raw_epub_folder
90
+ end
91
+
92
+ puts 'Binding book'
93
+ new_epub_path = compile(volume_url: volume_url, tmp_dir: tmp_dir, raw_epub_folder: raw_epub_folder)
94
+
95
+ puts 'Book is bound'
96
+ new_epub_path
97
+ end
98
+
99
+ def file_path(note)
100
+ volume_url = note.VolumeID
101
+
102
+ # /mnt/onboard/kepub/Title.kepub.epub!OEBPS!17_Chapter.xhtml
103
+ volume_path = volume_url.sub('file://', '')
104
+
105
+ content_id = note.ContentID
106
+
107
+ path = content_id.sub volume_path, ''
108
+ path.gsub('!', '/').split('#', 2)[0]
109
+ end
110
+
111
+ def pick_book
112
+ book_list = Content.joins(:bookmarks).group(:Title).order('Max(Bookmark.DateCreated) DESC')
113
+
114
+ prompt = TTY::Prompt.new
115
+ prompt.on(:keyescape) { exit }
116
+ book = prompt.select(
117
+ "What book would you like to export?", per_page: 7, filter: true
118
+ ) do |menu|
119
+ book_list.each do |book|
120
+ menu.choice book.Title, book
121
+ end
122
+ end
123
+
124
+ book
125
+ end
126
+
127
+ def copy_book(book, dir:, kobo_device_path:)
128
+ volume_url = book.ContentID
129
+ file_path = dir + "/" + File.basename(volume_url)
130
+ FileUtils.cp(kobo_device_path + volume_url.sub('file:///mnt/onboard', ''), file_path)
131
+ file_path
132
+ end
133
+
134
+ def extract_epub(path, dir:)
135
+ raw_epub_folder = dir + '/raw'
136
+ FileUtils.rm_rf raw_epub_folder
137
+ system 'unzip', path, '-d', raw_epub_folder
138
+ raw_epub_folder
139
+ end
140
+
141
+ def highlight_node(node)
142
+ # wrap only when has content
143
+ if node.content[/\S/]
144
+ node.wrap("<span style='background-color: #FDE383;' class='kobo-notes-binder-highlight'></span>")
145
+ node.parent
146
+ else
147
+ node
148
+ end
149
+ end
150
+
151
+ def process_note(note, raw_epub_folder:)
152
+ debug do
153
+ puts
154
+ puts 'processing note: '
155
+ pp note
156
+ nil
157
+ end
158
+
159
+ # Looks like `note.StartContainerChildIndex != -99` is for page bookmarks
160
+ # We can skip them for now
161
+ if note.StartContainerChildIndex != -99 || note.EndContainerChildIndex != -99
162
+ debug { ' Skipping note' }
163
+ return
164
+ end
165
+
166
+ xml_path = raw_epub_folder + file_path(note)
167
+ xml_string = File.read(xml_path)
168
+
169
+ doc = Nokogiri::XML(xml_string)
170
+ start_node = doc.at_css note.StartContainerPath
171
+ end_node = doc.at_css note.EndContainerPath
172
+
173
+ # Find parent that includes both start and end
174
+ parent = start_node
175
+ loop do
176
+ match = parent.to_enum(:traverse).find do |node|
177
+ node == end_node
178
+ end
179
+ break if match
180
+ parent = parent.parent
181
+ end
182
+
183
+ nodes = parent.to_enum(:traverse).to_a
184
+ selected_nodes = nodes[nodes.index(start_node)...nodes.index(end_node)]
185
+
186
+ # Append start text node, as it will be skipped
187
+ selected_nodes = start_node.to_enum(:traverse).to_a + selected_nodes
188
+
189
+ selected_nodes.select(&:text?).each do |node|
190
+ # special case when the note is duplicate and was already changed
191
+ next if node.previous_sibling && node.previous_sibling['class'] == 'kobo-notes-binder-highlight'
192
+
193
+ at_start = node.parent == start_node
194
+ at_end = node.parent == end_node
195
+
196
+ start_offset = note.StartOffset
197
+ end_offset = note.EndOffset
198
+
199
+ content = node.content
200
+
201
+ if at_start && at_end
202
+ text_before = content[0...start_offset]
203
+ text_highlight = content[start_offset...end_offset]
204
+ text_after = content[end_offset..-1]
205
+ elsif at_start
206
+ text_before = content[0...start_offset]
207
+ text_highlight = content[start_offset..-1]
208
+ text_after = ''
209
+ elsif at_end
210
+ text_before = ''
211
+ text_highlight = content[0...end_offset]
212
+ text_after = content[end_offset..-1]
213
+ else
214
+ text_before = ''
215
+ text_highlight = content
216
+ text_after = ''
217
+ end
218
+
219
+ if note.BookmarkID == 'ee31725b-d579-4a69-972c-f0bdb740bcb1'
220
+ # binding.pry
221
+ end
222
+
223
+ node.content = text_highlight
224
+
225
+ if text_highlight.size > 0
226
+ # need to be wrapped first, so sibling Text nodes won't be merged
227
+ node = highlight_node(node)
228
+ end
229
+
230
+ if text_before.size > 0
231
+ node.add_previous_sibling doc.create_text_node(text_before)
232
+ end
233
+
234
+ if text_after.size > 0
235
+ node.add_next_sibling doc.create_text_node(text_after)
236
+ end
237
+ end
238
+
239
+ File.write xml_path, doc.to_xhtml
240
+ end
241
+
242
+ def copy_db(kobo_device_path, dir:)
243
+ kobo_db_path = kobo_device_path + '/.kobo/KoboReader.sqlite'
244
+ db_path = dir + '/KoboReader.sqlite'
245
+ FileUtils.rm_rf db_path
246
+ puts " Coping from " + kobo_db_path
247
+ puts " Coping to " + db_path
248
+ FileUtils.cp(kobo_db_path, db_path)
249
+ db_path
250
+ end
251
+
252
+ def compile(volume_url:, tmp_dir:, raw_epub_folder:)
253
+ epub_file = tmp_dir + '/' + File.basename(volume_url).sub('.', '.highlights.')
254
+
255
+ files = *Dir[raw_epub_folder + '/*']
256
+
257
+ # move mimetype to be first
258
+ mime_path = files.find { |path| File.basename(path) == 'mimetype' }
259
+ files.delete mime_path
260
+ files.unshift mime_path
261
+
262
+ files = files.map { |path| Pathname.new(path).relative_path_from(raw_epub_folder).to_s }
263
+
264
+ FileUtils.cd raw_epub_folder do
265
+ system 'zip', '-rX', epub_file, *files
266
+ end
267
+
268
+ epub_file
269
+ end
270
+
271
+ def debug
272
+ if debug_mode
273
+ result = yield
274
+ if result === String
275
+ puts result
276
+ end
277
+ end
278
+ end
279
+ end
280
+
281
+
282
+
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KoboNotesBinder
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kobo_notes_binder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anton Katunin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sqlite3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tty-prompt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Command line to export Kobo device notes bound in the book
70
+ email:
71
+ - antulik@gmail.com
72
+ executables:
73
+ - kobo_notes_binder
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".github/workflows/main.yml"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - CHANGELOG.md
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - exe/kobo_notes_binder
89
+ - kobo_notes_binder.gemspec
90
+ - lib/kobo_notes_binder.rb
91
+ - lib/kobo_notes_binder/cli.rb
92
+ - lib/kobo_notes_binder/runner.rb
93
+ - lib/kobo_notes_binder/version.rb
94
+ homepage: https://github.com/antulik/kobo_notes_binder
95
+ licenses:
96
+ - MIT
97
+ metadata:
98
+ homepage_uri: https://github.com/antulik/kobo_notes_binder
99
+ source_code_uri: https://github.com/antulik/kobo_notes_binder
100
+ changelog_uri: https://github.com/antulik/kobo_notes_binder/blob/master/CHANGELOG.md
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 2.4.0
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.0.3
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Command line to export Kobo device notes bound in the book
120
+ test_files: []