anki_record 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rdoc_options +27 -0
- data/.rspec +3 -0
- data/.rubocop.yml +29 -0
- data/CHANGELOG.md +9 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +20 -0
- data/Gemfile.lock +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +53 -0
- data/Rakefile +12 -0
- data/anki_record.gemspec +41 -0
- data/lib/anki_record/anki_package.rb +183 -0
- data/lib/anki_record/card_template.rb +91 -0
- data/lib/anki_record/collection.rb +65 -0
- data/lib/anki_record/db/anki_schema_definition.rb +77 -0
- data/lib/anki_record/db/clean_collection21_record.rb +10 -0
- data/lib/anki_record/db/clean_collection2_record.rb +10 -0
- data/lib/anki_record/deck.rb +88 -0
- data/lib/anki_record/deck_options_group.rb +94 -0
- data/lib/anki_record/helpers/shared_constants_helper.rb +11 -0
- data/lib/anki_record/helpers/time_helper.rb +23 -0
- data/lib/anki_record/note_field.rb +63 -0
- data/lib/anki_record/note_type.rb +147 -0
- data/lib/anki_record/version.rb +5 -0
- data/lib/anki_record.rb +25 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6b6bcf30a94ecdc25ac6632007ec16125c92ee8a35a13d93dfbe61b721a5564c
|
4
|
+
data.tar.gz: d3ad70a4966a2fb260447e44d6da5f33fb799a4559a5147d8deb554ea6fbe5c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8ff665346db80061992e832cada84a373b22bd480bfbbbf3e1deab9ca2e3915d4a668442bccee98de746ed1a636ab6a978bc5ef0c61e640908a4df5fe0c57bf3
|
7
|
+
data.tar.gz: 563d4c9984ab0c98af1565748904263902c9fa374af61e3882b8c618dcb471856f2d36137e8590489354148b13c1492f94e9744970addf5e8acad07f40efce17
|
data/.rdoc_options
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
---
|
2
|
+
encoding: UTF-8
|
3
|
+
static_path: []
|
4
|
+
rdoc_include: []
|
5
|
+
page_dir:
|
6
|
+
charset: UTF-8
|
7
|
+
exclude:
|
8
|
+
- "~\\z"
|
9
|
+
- "\\.orig\\z"
|
10
|
+
- "\\.rej\\z"
|
11
|
+
- "\\.bak\\z"
|
12
|
+
- "\\.gemspec\\z"
|
13
|
+
hyperlink_all: false
|
14
|
+
line_numbers: false
|
15
|
+
locale:
|
16
|
+
locale_dir: locale
|
17
|
+
locale_name:
|
18
|
+
main_page:
|
19
|
+
markup: rdoc
|
20
|
+
output_decoration: true
|
21
|
+
show_hash: false
|
22
|
+
skip_tests: true
|
23
|
+
tab_width: 8
|
24
|
+
template_stylesheets: []
|
25
|
+
title:
|
26
|
+
visibility: :protected
|
27
|
+
webcvs:
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 3.2.1
|
3
|
+
NewCops: enable
|
4
|
+
|
5
|
+
Style/StringLiterals:
|
6
|
+
Enabled: true
|
7
|
+
EnforcedStyle: double_quotes
|
8
|
+
|
9
|
+
Style/StringLiteralsInInterpolation:
|
10
|
+
Enabled: true
|
11
|
+
EnforcedStyle: double_quotes
|
12
|
+
|
13
|
+
Layout/LineLength:
|
14
|
+
Max: 120
|
15
|
+
Exclude:
|
16
|
+
- "spec/anki_record/*"
|
17
|
+
|
18
|
+
Metrics/BlockLength:
|
19
|
+
Exclude:
|
20
|
+
- "spec/anki_record/*"
|
21
|
+
|
22
|
+
Layout/IndentationConsistency:
|
23
|
+
EnforcedStyle: indented_internal_methods
|
24
|
+
|
25
|
+
Metrics/ClassLength:
|
26
|
+
Max: 120
|
27
|
+
|
28
|
+
Style/HashSyntax:
|
29
|
+
EnforcedShorthandSyntax: either
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 regoky@outlook.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,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
ruby "3.2.1"
|
6
|
+
|
7
|
+
# Specify your gem's dependencies in anki_record.gemspec
|
8
|
+
gemspec
|
9
|
+
|
10
|
+
gem "rake", "~> 13.0"
|
11
|
+
|
12
|
+
gem "rspec", "~> 3.0"
|
13
|
+
|
14
|
+
gem "simplecov"
|
15
|
+
|
16
|
+
gem "rubocop", "~> 1.21"
|
17
|
+
|
18
|
+
gem "pry"
|
19
|
+
|
20
|
+
gem "rdoc"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
anki_record (0.1.1)
|
5
|
+
rubyzip (>= 2.3)
|
6
|
+
sqlite3
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
ast (2.4.2)
|
12
|
+
coderay (1.1.3)
|
13
|
+
diff-lcs (1.5.0)
|
14
|
+
docile (1.4.0)
|
15
|
+
json (2.6.3)
|
16
|
+
method_source (1.0.0)
|
17
|
+
parallel (1.22.1)
|
18
|
+
parser (3.2.0.0)
|
19
|
+
ast (~> 2.4.1)
|
20
|
+
pry (0.14.2)
|
21
|
+
coderay (~> 1.1)
|
22
|
+
method_source (~> 1.0)
|
23
|
+
psych (5.1.0)
|
24
|
+
stringio
|
25
|
+
rainbow (3.1.1)
|
26
|
+
rake (13.0.6)
|
27
|
+
rdoc (6.5.0)
|
28
|
+
psych (>= 4.0.0)
|
29
|
+
regexp_parser (2.6.2)
|
30
|
+
rexml (3.2.5)
|
31
|
+
rspec (3.12.0)
|
32
|
+
rspec-core (~> 3.12.0)
|
33
|
+
rspec-expectations (~> 3.12.0)
|
34
|
+
rspec-mocks (~> 3.12.0)
|
35
|
+
rspec-core (3.12.1)
|
36
|
+
rspec-support (~> 3.12.0)
|
37
|
+
rspec-expectations (3.12.2)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.12.0)
|
40
|
+
rspec-mocks (3.12.3)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.12.0)
|
43
|
+
rspec-support (3.12.0)
|
44
|
+
rubocop (1.44.1)
|
45
|
+
json (~> 2.3)
|
46
|
+
parallel (~> 1.10)
|
47
|
+
parser (>= 3.2.0.0)
|
48
|
+
rainbow (>= 2.2.2, < 4.0)
|
49
|
+
regexp_parser (>= 1.8, < 3.0)
|
50
|
+
rexml (>= 3.2.5, < 4.0)
|
51
|
+
rubocop-ast (>= 1.24.1, < 2.0)
|
52
|
+
ruby-progressbar (~> 1.7)
|
53
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
54
|
+
rubocop-ast (1.24.1)
|
55
|
+
parser (>= 3.1.1.0)
|
56
|
+
ruby-progressbar (1.11.0)
|
57
|
+
rubyzip (2.3.2)
|
58
|
+
simplecov (0.22.0)
|
59
|
+
docile (~> 1.1)
|
60
|
+
simplecov-html (~> 0.11)
|
61
|
+
simplecov_json_formatter (~> 0.1)
|
62
|
+
simplecov-html (0.12.3)
|
63
|
+
simplecov_json_formatter (0.1.4)
|
64
|
+
sqlite3 (1.6.0-x86_64-linux)
|
65
|
+
stringio (3.0.5)
|
66
|
+
unicode-display_width (2.4.2)
|
67
|
+
|
68
|
+
PLATFORMS
|
69
|
+
x86_64-linux
|
70
|
+
|
71
|
+
DEPENDENCIES
|
72
|
+
anki_record!
|
73
|
+
pry
|
74
|
+
rake (~> 13.0)
|
75
|
+
rdoc
|
76
|
+
rspec (~> 3.0)
|
77
|
+
rubocop (~> 1.21)
|
78
|
+
simplecov
|
79
|
+
|
80
|
+
RUBY VERSION
|
81
|
+
ruby 3.2.1p31
|
82
|
+
|
83
|
+
BUNDLED WITH
|
84
|
+
2.3.25
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Kyle Rego
|
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,53 @@
|
|
1
|
+
# AnkiRecord
|
2
|
+
|
3
|
+
AnkiRecord provides an interface to Anki SQLite databases through the Ruby programming language.
|
4
|
+
|
5
|
+
Currently it can be used to create an empty Anki database file, execute raw SQL statements against it, and then zip the database into an *.apkg file which can be imported into Anki.
|
6
|
+
|
7
|
+
[Documentation](https://kylerego.github.io/anki_record_docs)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add anki_record
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install anki_record
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require "anki_record"
|
23
|
+
|
24
|
+
db = AnkiRecord::AnkiPackage.new name: "test1"
|
25
|
+
db.execute "any valid SQL statement"
|
26
|
+
db.zip_and_close # creates test.apkg file in the current working directory
|
27
|
+
|
28
|
+
```
|
29
|
+
|
30
|
+
The RSpec tests are written BDD-style as executable documentation; reading them might help to understand the gem (e.g. [anki_package_spec.rb](https://github.com/KyleRego/anki_record/blob/main/spec/anki_record/anki_package_spec.rb)).
|
31
|
+
|
32
|
+
## Development
|
33
|
+
|
34
|
+
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.
|
35
|
+
|
36
|
+
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).
|
37
|
+
|
38
|
+
### Release checklist
|
39
|
+
- Bump version
|
40
|
+
- Update changelog
|
41
|
+
- Regenerate documentation
|
42
|
+
|
43
|
+
<!-- ## Contributing
|
44
|
+
|
45
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/KyleRego/anki_record. 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/KyleRego/anki_record/blob/master/CODE_OF_CONDUCT.md). -->
|
46
|
+
|
47
|
+
## License
|
48
|
+
|
49
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
50
|
+
|
51
|
+
## Code of Conduct
|
52
|
+
|
53
|
+
Everyone interacting in the AnkiRecord project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/KyleRegoanki_record/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/anki_record.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/anki_record/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "anki_record"
|
7
|
+
spec.version = AnkiRecord::VERSION
|
8
|
+
spec.authors = ["Kyle Rego"]
|
9
|
+
spec.email = ["regoky@outlook.com"]
|
10
|
+
|
11
|
+
spec.summary = "Automate Anki flashcard editing with the Ruby programming language."
|
12
|
+
spec.description = <<-DESC
|
13
|
+
This Ruby library, which is currently in development, will provide an interface to inspect, update, and create Anki SQLite3 databases (*.apkg files).
|
14
|
+
DESC
|
15
|
+
spec.homepage = "https://github.com/KyleRego/anki_record"
|
16
|
+
spec.license = "MIT"
|
17
|
+
spec.required_ruby_version = ">= 3.2.1"
|
18
|
+
|
19
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
20
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
21
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
22
|
+
spec.metadata["source_code_uri"] = "https://github.com/KyleRego/anki_record"
|
23
|
+
spec.metadata["changelog_uri"] = "https://github.com/KyleRego/anki_record/blob/main/CHANGELOG.md"
|
24
|
+
|
25
|
+
# Specify which files should be added to the gem when it is released.
|
26
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
27
|
+
spec.files = Dir.chdir(__dir__) do
|
28
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
29
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
30
|
+
end
|
31
|
+
end
|
32
|
+
spec.bindir = "exe"
|
33
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
34
|
+
spec.require_paths = ["lib"]
|
35
|
+
|
36
|
+
spec.add_dependency "rubyzip", ">= 2.3"
|
37
|
+
spec.add_dependency "sqlite3"
|
38
|
+
|
39
|
+
# For more information and examples about making a new gem, check out our
|
40
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
41
|
+
end
|
@@ -0,0 +1,183 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "pry"
|
4
|
+
require "pathname"
|
5
|
+
|
6
|
+
require_relative "db/anki_schema_definition"
|
7
|
+
require_relative "db/clean_collection2_record"
|
8
|
+
require_relative "db/clean_collection21_record"
|
9
|
+
require_relative "collection"
|
10
|
+
|
11
|
+
module AnkiRecord
|
12
|
+
##
|
13
|
+
# Represents an Anki SQLite3 package/database
|
14
|
+
#
|
15
|
+
# Use ::new to create a new object or ::open to create an object from an existing one
|
16
|
+
class AnkiPackage
|
17
|
+
NAME_ERROR_MESSAGE = "The name argument must be a string without spaces."
|
18
|
+
PATH_ERROR_MESSAGE = "*No .apkg file was found at the given path."
|
19
|
+
STANDARD_ERROR_MESSAGE = <<-MSG
|
20
|
+
An error occurred.
|
21
|
+
The temporary *.anki21 database has been deleted.
|
22
|
+
No *.apkg zip file has been saved.
|
23
|
+
MSG
|
24
|
+
|
25
|
+
private_constant :NAME_ERROR_MESSAGE, :PATH_ERROR_MESSAGE, :STANDARD_ERROR_MESSAGE
|
26
|
+
|
27
|
+
##
|
28
|
+
# Creates a new object which represents an Anki SQLite3 database
|
29
|
+
#
|
30
|
+
# This method takes an optional block argument.
|
31
|
+
#
|
32
|
+
# When a block argument is used, execution is yielded to the block.
|
33
|
+
# After the block executes, the temporary files are zipped into the +name+.apkg file
|
34
|
+
# which is saved in +directory+. +directory+ is the current working directory by default.
|
35
|
+
# If the block throws a runtime error, the temporary files are deleted but the zip file is not created.
|
36
|
+
#
|
37
|
+
# When no block argument is used, #zip_and_close must be called explicitly at the end of your script.
|
38
|
+
def initialize(name:, directory: Dir.pwd)
|
39
|
+
setup_package_instance_variables(name: name, directory: directory)
|
40
|
+
|
41
|
+
return unless block_given?
|
42
|
+
|
43
|
+
begin
|
44
|
+
yield self
|
45
|
+
rescue StandardError => e
|
46
|
+
close
|
47
|
+
puts_error_and_standard_message(error: e)
|
48
|
+
else
|
49
|
+
zip_and_close
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
##
|
54
|
+
# Executes a raw SQL statement against the *.anki21 database
|
55
|
+
#
|
56
|
+
# Do not use this to execute data definition language SQL statements
|
57
|
+
# (i.e. do not create, alter, or drop tables or indexes)
|
58
|
+
# unless you have a good reason to change the database schema.
|
59
|
+
def execute(raw_sql_string)
|
60
|
+
@anki21_database.execute raw_sql_string
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def setup_package_instance_variables(name:, directory:)
|
66
|
+
@name = check_name_is_valid(name: name)
|
67
|
+
@directory = directory # TODO: check directory is valid
|
68
|
+
@tmpdir = Dir.mktmpdir
|
69
|
+
@tmp_files = []
|
70
|
+
@anki21_database = setup_anki21_database_object
|
71
|
+
@anki2_database = setup_anki2_database_object
|
72
|
+
@media_file = setup_media
|
73
|
+
@collection = Collection.new(anki_package: self)
|
74
|
+
end
|
75
|
+
|
76
|
+
def check_name_is_valid(name:)
|
77
|
+
raise ArgumentError, NAME_ERROR_MESSAGE unless name.instance_of?(String) && !name.empty? && !name.include?(" ")
|
78
|
+
|
79
|
+
name.end_with?(".apkg") ? name[0, name.length - 5] : name
|
80
|
+
end
|
81
|
+
|
82
|
+
def setup_anki21_database_object
|
83
|
+
anki21_file_name = "collection.anki21"
|
84
|
+
db = SQLite3::Database.new "#{@tmpdir}/#{anki21_file_name}", options: {}
|
85
|
+
@tmp_files << anki21_file_name
|
86
|
+
db.execute_batch ANKI_SCHEMA_DEFINITION
|
87
|
+
db.execute CLEAN_COLLECTION_21_RECORD
|
88
|
+
db.results_as_hash = true
|
89
|
+
db
|
90
|
+
end
|
91
|
+
|
92
|
+
def setup_anki2_database_object
|
93
|
+
anki2_file_name = "collection.anki2"
|
94
|
+
db = SQLite3::Database.new "#{@tmpdir}/#{anki2_file_name}", options: {}
|
95
|
+
@tmp_files << anki2_file_name
|
96
|
+
db.execute_batch ANKI_SCHEMA_DEFINITION
|
97
|
+
db.execute CLEAN_COLLECTION_2_RECORD
|
98
|
+
db.close
|
99
|
+
db
|
100
|
+
end
|
101
|
+
|
102
|
+
def setup_media
|
103
|
+
media_file_path = FileUtils.touch("#{@tmpdir}/media")[0]
|
104
|
+
media_file = File.open(media_file_path, mode: "w")
|
105
|
+
media_file.write("{}")
|
106
|
+
media_file.close
|
107
|
+
@tmp_files << "media"
|
108
|
+
media_file
|
109
|
+
end
|
110
|
+
|
111
|
+
def puts_error_and_standard_message(error:)
|
112
|
+
puts "#{error}\n#{STANDARD_ERROR_MESSAGE}"
|
113
|
+
end
|
114
|
+
|
115
|
+
public
|
116
|
+
|
117
|
+
##
|
118
|
+
# Creates a new object which represents the Anki SQLite3 database file at +path+
|
119
|
+
#
|
120
|
+
# Development has focused on ::new so this method is not recommended at this time
|
121
|
+
def self.open(path:, create_backup: true)
|
122
|
+
pathname = check_file_at_path_is_valid(path: path)
|
123
|
+
copy_apkg_file(pathname: pathname) if create_backup
|
124
|
+
@anki_package = new(name: pathname.basename.to_s, directory: pathname.dirname)
|
125
|
+
end
|
126
|
+
|
127
|
+
class << self
|
128
|
+
private
|
129
|
+
|
130
|
+
def check_file_at_path_is_valid(path:)
|
131
|
+
pathname = Pathname.new(path)
|
132
|
+
raise PATH_ERROR_MESSAGE unless pathname.file? && pathname.extname == ".apkg"
|
133
|
+
|
134
|
+
pathname
|
135
|
+
end
|
136
|
+
|
137
|
+
def copy_apkg_file(pathname:)
|
138
|
+
path = pathname.to_s
|
139
|
+
FileUtils.cp path, "#{path}.copy-#{Time.now.to_i}"
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
##
|
144
|
+
# Zips the temporary files into the *.apkg package and deletes the temporary files.
|
145
|
+
def zip_and_close
|
146
|
+
zip && close
|
147
|
+
end
|
148
|
+
|
149
|
+
private
|
150
|
+
|
151
|
+
def zip
|
152
|
+
Zip::File.open(target_zip_file, create: true) do |zip_file|
|
153
|
+
@tmp_files.each do |file_name|
|
154
|
+
zip_file.add(file_name, File.join(@tmpdir, file_name))
|
155
|
+
end
|
156
|
+
end
|
157
|
+
true
|
158
|
+
end
|
159
|
+
|
160
|
+
def target_zip_file
|
161
|
+
"#{@directory}/#{@name}.apkg"
|
162
|
+
end
|
163
|
+
|
164
|
+
def close
|
165
|
+
@anki21_database.close
|
166
|
+
FileUtils.rm_rf(@tmpdir)
|
167
|
+
end
|
168
|
+
|
169
|
+
public
|
170
|
+
|
171
|
+
##
|
172
|
+
# Returns true if the database is open
|
173
|
+
def open?
|
174
|
+
!closed?
|
175
|
+
end
|
176
|
+
|
177
|
+
##
|
178
|
+
# Returns true if the database is closed
|
179
|
+
def closed?
|
180
|
+
@anki21_database.closed?
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "pry"
|
4
|
+
|
5
|
+
# TODO: All instance variables should at least be readable
|
6
|
+
|
7
|
+
module AnkiRecord
|
8
|
+
##
|
9
|
+
# CardTemplate represents a card template of an Anki note type
|
10
|
+
class CardTemplate
|
11
|
+
##
|
12
|
+
# The name of this card template
|
13
|
+
attr_accessor :name
|
14
|
+
|
15
|
+
##
|
16
|
+
# The question format
|
17
|
+
attr_accessor :question_format
|
18
|
+
|
19
|
+
##
|
20
|
+
# The answer format
|
21
|
+
attr_accessor :answer_format
|
22
|
+
|
23
|
+
##
|
24
|
+
# The font style shown for this card template in the browser
|
25
|
+
attr_accessor :browser_font_style
|
26
|
+
|
27
|
+
##
|
28
|
+
# The font size used for this card template in the browser
|
29
|
+
attr_accessor :browser_font_size
|
30
|
+
|
31
|
+
##
|
32
|
+
# The note type that this card template belongs to
|
33
|
+
attr_reader :note_type
|
34
|
+
|
35
|
+
##
|
36
|
+
# 0 for the first card template of the note type, 1 for the second, etc.
|
37
|
+
attr_reader :ordinal_number
|
38
|
+
|
39
|
+
##
|
40
|
+
# Instantiates a new card template called +name+ for the given note type
|
41
|
+
#
|
42
|
+
def initialize(note_type:, name: nil, args: nil)
|
43
|
+
raise ArgumentError unless (name && args.nil?) || (args && args["name"])
|
44
|
+
|
45
|
+
@note_type = note_type
|
46
|
+
if args
|
47
|
+
setup_card_template_instance_variables_from_existing(args: args)
|
48
|
+
else
|
49
|
+
setup_card_template_instance_variables(name: name)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def setup_card_template_instance_variables_from_existing(args:)
|
56
|
+
@name = args["name"]
|
57
|
+
@ordinal_number = args["ord"]
|
58
|
+
@question_format = args["qfmt"]
|
59
|
+
@answer_format = args["afmt"]
|
60
|
+
@bqfmt = args["bqfmt"]
|
61
|
+
@bafmt = args["bafmt"]
|
62
|
+
@deck_id = args["did"]
|
63
|
+
@browser_font_style = args["bfont"]
|
64
|
+
@browser_font_size = args["bsize"]
|
65
|
+
end
|
66
|
+
|
67
|
+
def setup_card_template_instance_variables(name:)
|
68
|
+
@name = name
|
69
|
+
@ordinal_number = @note_type.templates.length
|
70
|
+
@question_format = ""
|
71
|
+
@answer_format = ""
|
72
|
+
@bqfmt = ""
|
73
|
+
@bafmt = ""
|
74
|
+
@deck_id = nil
|
75
|
+
@browser_font_style = ""
|
76
|
+
@browser_font_size = 0
|
77
|
+
end
|
78
|
+
|
79
|
+
public
|
80
|
+
|
81
|
+
##
|
82
|
+
# Returns the field names that are allowed in the answer format and question format
|
83
|
+
#
|
84
|
+
# These are the field_name values in {{field_name}} in those formats.
|
85
|
+
#
|
86
|
+
# They are equivalent to the names of the fields of the template's note type.
|
87
|
+
def allowed_field_names
|
88
|
+
@note_type.fields.map(&:name)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|