ankirb 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d5c0bd25a52aa9d078042c40cc43b8a3bcc3e7f
4
- data.tar.gz: d0ab8e881d55f6e7fe49be5f453091aee417b426
3
+ metadata.gz: 0590f9a5bb353ee322d8c2b465c5dabd26dae92d
4
+ data.tar.gz: 8ad2652a4483b9914be1ad9a673f6741671c7739
5
5
  SHA512:
6
- metadata.gz: 0f88ae571ce7825399b1ed8a465363a4d93e45ff24e72e2fd24e3d88092f6233d11173467243e92b896f7f72fa6384fd512fa920d4e41f9955d5778e5aa0b1b8
7
- data.tar.gz: a75126184e3882843b033f4b6b1083ce719a091200ae5be949aa665306df11c44786c38cee68b19fee7c39ce4067efc7f203f15023991b64b0b6defcbb641fe8
6
+ metadata.gz: 6451de0883d1580cdee90532299bbbcfce158637911a5763ec69a9fd8ad6aa0c626d3ba88677379312c31c286ca59743ca3e83015f8c84e389fe5c5366843add
7
+ data.tar.gz: bcbbf81515ee5576554ca1540431fb1d9cde1509dd2ad889f5f63fa610e163c2a05daa034fb7e9d3e1f84673447ebd8268da30c2d9ae5d3cf4b29de6c60f300c
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ankirb
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/ankirb.svg)](https://badge.fury.io/rb/ankirb)
3
+ [![Gem Version](https://badge.fury.io/rb/ankirb.svg)](https://badge.fury.io/rb/ankirb) [![Build Status](https://travis-ci.org/rkachowski/anki-rb.svg?branch=master)](https://travis-ci.org/rkachowski/anki-rb)
4
4
 
5
5
  A basic gem to create [Anki](http://ankisrs.net/) decks (.apkg)
6
6
 
@@ -25,8 +25,9 @@ Or install it yourself as:
25
25
  ```ruby
26
26
  require 'ankirb'
27
27
 
28
+ card = Anki::Card.new(:front=>"What is the french for cheese omelette?", :back=>"Omelette au fromage")
28
29
  deck = Anki::Deck.new 'FancyExampleDeck'
29
- deck.add_card Anki::Card.new(:front=>"What is the french for cheese omelette?", :back=>"Omelette au fromage")
30
+ deck.add_card card
30
31
 
31
32
  Anki::apkg.export deck, "~/Anki/Decks" #export to ~/Anki/Decks/FancyExampleDeck.apkg
32
33
  ```
data/ankirb.gemspec CHANGED
@@ -27,4 +27,6 @@ Gem::Specification.new do |spec|
27
27
  spec.add_runtime_dependency 'base91', '0.0.1'
28
28
  spec.add_runtime_dependency 'rubyzip', '~> 1.1'
29
29
  spec.add_runtime_dependency 'sqlite3', '~> 1.3'
30
+ spec.add_runtime_dependency 'mimemagic', '~> 0.3'
31
+
30
32
  end
@@ -0,0 +1,67 @@
1
+ module Anki
2
+ class Card
3
+ attr_accessor :tags, :deck
4
+ attr_reader :id
5
+
6
+
7
+ def initialize options={}
8
+ @front = CardFace.new(options[:front] || '')
9
+ @back = CardFace.new(options[:back] || '')
10
+ @tags = options[:tags] || []
11
+
12
+ @id = Time.now.to_i
13
+ end
14
+
15
+ def front
16
+ @front
17
+ end
18
+
19
+ def back
20
+ @back
21
+ end
22
+
23
+ def has_media?
24
+ !@front.media.empty? or !back.media.empty?
25
+ end
26
+ def front= value
27
+ @front.content = value
28
+ end
29
+
30
+ def back= value
31
+ @back.content = value
32
+ end
33
+
34
+ alias :question :front
35
+ alias :question= :front=
36
+ alias :answer :back
37
+ alias :answer= :back=
38
+ end
39
+
40
+ class CardFace
41
+ attr_accessor :content, :media
42
+
43
+ def initialize content
44
+ @content = content
45
+ @media = []
46
+ scan_content_for_media
47
+ end
48
+
49
+ def << thing
50
+ if File.exists?(thing)
51
+ @media << thing
52
+ else
53
+ @content << thing
54
+ scan_content_for_media
55
+ end
56
+ end
57
+
58
+ def to_s
59
+ @content
60
+ end
61
+ private
62
+
63
+ def scan_content_for_media
64
+ #todo: check if content contains a media reference and apply that
65
+ end
66
+ end
67
+ end
@@ -24,23 +24,7 @@ module Anki
24
24
  end
25
25
  end
26
26
 
27
- class Card
28
- attr_accessor :front, :back, :tags, :deck
29
- attr_reader :id
30
27
 
31
- @@defaults = {
32
- :front => 'Card Front',
33
- :back => 'Card Back'
34
- }
35
-
36
- def initialize options={}
37
- @front = options[:front] || @@defaults[:front]
38
- @back = options[:back] || @@defaults[:back]
39
- @tags = options[:tags] || []
40
-
41
- @id = Time.now.to_i
42
- end
43
- end
44
28
 
45
29
  class Type
46
30
  def self.new_type
@@ -0,0 +1,15 @@
1
+ module Anki
2
+ class Media
3
+ def self.audio
4
+
5
+ end
6
+
7
+ def self.image
8
+
9
+ end
10
+
11
+ def self.video
12
+
13
+ end
14
+ end
15
+ end
@@ -2,6 +2,7 @@ require 'zip'
2
2
  require 'json'
3
3
  require 'fileutils'
4
4
  require 'base91'
5
+
5
6
  module Anki
6
7
 
7
8
  SCHEMA_VERSION = 11 unless const_defined?(:SCHEMA_VERSION)
@@ -13,8 +14,8 @@ module Anki
13
14
  Dir.chdir(dir) do
14
15
  db = DB.create 'collection.anki2'
15
16
 
16
- export_deck deck, db
17
17
  export_media deck
18
+ export_deck deck, db
18
19
  filename = deck.name + '.apkg'
19
20
  archive = Zip::File.open(filename, Zip::File::CREATE)
20
21
 
@@ -31,8 +32,19 @@ module Anki
31
32
 
32
33
  private
33
34
  def self.export_media deck
34
- #TODO: correctly export media
35
- File.open('media', 'w'){|f| f << Defaults.media }
35
+ media_cards = deck.cards.select{|c| c.has_media? }
36
+ faces_with_media = media_cards.reduce([]){|arr,c| arr.push(c.front,c.back)}.select{|f|!f.media.empty?}
37
+ media ={}
38
+
39
+ faces_with_media.each do |face|
40
+ face.media.each do |media_reference|
41
+ #face << MediaManager.get_tag_for(media_reference)
42
+ FileUtils.cp media_reference, media.length.to_s
43
+ media[media.size.to_s] = File.basename(media_reference)
44
+ end
45
+ end
46
+
47
+ File.open('media', 'w'){|f| f << media.to_json }
36
48
  end
37
49
 
38
50
  def self.export_deck deck, db
@@ -61,8 +73,8 @@ module Anki
61
73
 
62
74
  deck.cards.each do |card|
63
75
  #flds is front and back of card separated by \xf1
64
- flds_bytes = [card.front, card.back].map{|f|f.bytes}.insert(1, 'f1'.hex)
65
- flds = flds_bytes.flatten.pack('C*').force_encoding('utf-8')
76
+ flds_bytes = [card.front.content, card.back.content].map{|f|f.bytes}.insert(1, 'f1'.hex)
77
+ flds = flds_bytes.flatten.pack('U*')
66
78
 
67
79
  note = {
68
80
  :id => Time.now.to_i,
@@ -72,7 +84,7 @@ module Anki
72
84
  :usn => -1,
73
85
  :tags => nil,
74
86
  :flds => flds,
75
- :sfld => card.front.gsub(/<\/?[^>]*>/, ''), #v dirty html sanitization
87
+ :sfld => card.front.content.gsub(/<\/?[^>]*>/, ''), #v dirty html sanitization
76
88
  :csum => 0,
77
89
  :flags => 0,
78
90
  :data => nil
@@ -0,0 +1,28 @@
1
+ require 'mimemagic'
2
+ module Anki
3
+ class MediaManager
4
+
5
+
6
+ def self.get_tag_for filepath
7
+ media = MimeMagic.by_magic(File.open(filepath))
8
+ case
9
+ when media.image?
10
+ when media.video?
11
+ when media.audio?
12
+ " [sound:#{File.basename(filepath)}]"
13
+ else
14
+ raise "Unknown media type - #{filepath}"
15
+ end
16
+ end
17
+
18
+ def self.valid_media? filepath
19
+ media = MimeMagic.by_magic(File.open(filepath))
20
+ if media.image? or media.video? or media.audio?
21
+ true
22
+ else
23
+ false
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module Anki
2
- VERSION = '0.1.2' unless const_defined?(:VERSION)
2
+ VERSION = '0.1.3' unless const_defined?(:VERSION)
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ankirb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Donald Hutchison
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-11 00:00:00.000000000 Z
11
+ date: 2016-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '1.3'
111
+ - !ruby/object:Gem::Dependency
112
+ name: mimemagic
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.3'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.3'
111
125
  description: A library that lets you export apkg packaged decks for Anki. Designed
112
126
  to deal with creation / exporting and not much else
113
127
  email:
@@ -124,10 +138,13 @@ files:
124
138
  - Rakefile
125
139
  - ankirb.gemspec
126
140
  - lib/ankirb.rb
141
+ - lib/ankirb/anki/card.rb
127
142
  - lib/ankirb/anki/deck.rb
143
+ - lib/ankirb/anki/media.rb
128
144
  - lib/ankirb/export/apkg.rb
129
145
  - lib/ankirb/export/db.rb
130
146
  - lib/ankirb/export/defaults.rb
147
+ - lib/ankirb/util/media_manager.rb
131
148
  - lib/ankirb/version.rb
132
149
  homepage: https://github.com/rkachowski/anki-rb
133
150
  licenses: