cskit 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +15 -0
  2. data/Gemfile +8 -0
  3. data/History.txt +3 -0
  4. data/LICENSE +177 -0
  5. data/README.md +246 -0
  6. data/Rakefile +29 -0
  7. data/cskit.gemspec +25 -0
  8. data/lib/cskit/annotated_string.rb +73 -0
  9. data/lib/cskit/annotator.rb +16 -0
  10. data/lib/cskit/formatters/bible/bible_html_formatter.rb +41 -0
  11. data/lib/cskit/formatters/bible/bible_plain_text_formatter.rb +117 -0
  12. data/lib/cskit/formatters/bible.rb +12 -0
  13. data/lib/cskit/formatters/science_health/science_health_html_formatter.rb +32 -0
  14. data/lib/cskit/formatters/science_health/science_health_plain_text_formatter.rb +73 -0
  15. data/lib/cskit/formatters/science_health.rb +12 -0
  16. data/lib/cskit/formatters.rb +19 -0
  17. data/lib/cskit/lesson/lesson.rb +72 -0
  18. data/lib/cskit/lesson/section.rb +26 -0
  19. data/lib/cskit/lesson.rb +10 -0
  20. data/lib/cskit/parsers/bible/bible.rb +1005 -0
  21. data/lib/cskit/parsers/bible/bible.treetop +64 -0
  22. data/lib/cskit/parsers/bible/nodes.rb +153 -0
  23. data/lib/cskit/parsers/bible/objects.rb +81 -0
  24. data/lib/cskit/parsers/science_health/nodes.rb +82 -0
  25. data/lib/cskit/parsers/science_health/objects.rb +47 -0
  26. data/lib/cskit/parsers/science_health/science_health.rb +607 -0
  27. data/lib/cskit/parsers/science_health/science_health.treetop +44 -0
  28. data/lib/cskit/parsers.rb +8 -0
  29. data/lib/cskit/readers/bible_reader.rb +58 -0
  30. data/lib/cskit/readers/reading.rb +54 -0
  31. data/lib/cskit/readers/science_health_reader.rb +97 -0
  32. data/lib/cskit/readers.rb +9 -0
  33. data/lib/cskit/resources/volumes/bible.rb +45 -0
  34. data/lib/cskit/resources/volumes/science_health.rb +49 -0
  35. data/lib/cskit/resources/volumes.rb +8 -0
  36. data/lib/cskit/version.rb +5 -0
  37. data/lib/cskit/volume.rb +33 -0
  38. data/lib/cskit.rb +73 -0
  39. metadata +122 -0
@@ -0,0 +1,54 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ module Readers
5
+
6
+ Reading = Struct.new(:texts, :citation, :chapter, :verse) do
7
+ def params
8
+ @params || {}
9
+ end
10
+
11
+ def to_annotated_reading
12
+ AnnotatedReading.new(texts, citation, chapter, verse)
13
+ end
14
+
15
+ def annotated?
16
+ false
17
+ end
18
+ end
19
+
20
+ class AnnotatedReading < Reading
21
+ def add_annotation(text_index, annotation)
22
+ if idx = index(text_index, annotation)
23
+ annotations[text_index].delete_at(idx)
24
+ end
25
+
26
+ (annotations[text_index] ||= []) << annotation
27
+ end
28
+
29
+ def annotations_at(index)
30
+ annotations[index]
31
+ end
32
+
33
+ def annotations
34
+ @annotations ||= {}
35
+ end
36
+
37
+ def annotated?
38
+ true
39
+ end
40
+
41
+ private
42
+
43
+ def index(text_index, annotation_to_find)
44
+ (annotations[text_index] || []).each_with_index do |annotation, index|
45
+ if annotation.start == annotation_to_find.start && annotation.finish == annotation_to_find.finish
46
+ return index
47
+ end
48
+ end
49
+ nil
50
+ end
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,97 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ module Readers
5
+ class ScienceHealthReader
6
+
7
+ attr_reader :volume
8
+
9
+ def initialize(volume)
10
+ @volume = volume
11
+ end
12
+
13
+ def get_page(page_number)
14
+ volume.get_page(page_number)
15
+ end
16
+
17
+ def get_line(line_number, page_number)
18
+ get_page(page_number).lines[line_number - 1]
19
+ end
20
+
21
+ # be careful - this will go all the way to the end of the book unless you break
22
+ def each_line(line_number, page_number)
23
+ while page = get_page(page_number)
24
+ yield get_line(line_number, page_number), line_number, page_number
25
+
26
+ line_number, page_number = if (line_number + 1) > page.lines.size
27
+ [1, next_page_number(page_number)]
28
+ else
29
+ [line_number + 1, page_number]
30
+ end
31
+ end
32
+ end
33
+
34
+ def get_paragraph(page_number, line_number)
35
+ first_line = true
36
+ lines = []
37
+
38
+ each_line(line_number, page_number) do |line, line_number, page_number|
39
+ if first_line
40
+ lines << line
41
+ else
42
+ if line.paragraph_start? && !first_line
43
+ break
44
+ else
45
+ lines << line
46
+ end
47
+ end
48
+
49
+ first_line = false
50
+ end
51
+
52
+ lines
53
+ end
54
+
55
+ def get_line_range(range, page_number)
56
+ lines = []
57
+ each_line(range.first, page_number) do |line, line_number, page_number|
58
+ lines << line
59
+ break if line_number == range.last
60
+ end
61
+ lines
62
+ end
63
+
64
+ def readings_for(citation)
65
+ citation.lines.map do |citation_line|
66
+ lines = if citation_line.finish
67
+ get_line_range(citation_line.start..citation_line.finish, citation.page)
68
+ else
69
+ if citation_line.only?
70
+ [get_line(citation_line.start, citation.page)]
71
+ else
72
+ get_paragraph(citation.page, citation_line.start)
73
+ end
74
+ end
75
+
76
+ Reading.new(lines, citation_line)
77
+ end
78
+ end
79
+
80
+ protected
81
+
82
+ def next_page_number(page_number)
83
+ numerals = ["vi", "vii", "ix", "x", "xi", "xii"]
84
+ if index = numerals.index(page_number)
85
+ if index == numerals.size - 1
86
+ "1"
87
+ else
88
+ numerals[index + 1]
89
+ end
90
+ else
91
+ (page_number.to_i + 1).to_s
92
+ end
93
+ end
94
+
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ module Readers
5
+ autoload :ScienceHealthReader, "cskit/readers/science_health_reader"
6
+ autoload :BibleReader, "cskit/readers/bible_reader"
7
+ autoload :Reading, "cskit/readers/reading"
8
+ end
9
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ module Volumes
5
+ module Bible
6
+
7
+ Book = Struct.new(:name, :chapters) do
8
+ def to_hash
9
+ { "name" => name, "chapters" => chapters.map(&:to_hash) }
10
+ end
11
+
12
+ def self.from_hash(hash)
13
+ Book.new(
14
+ hash["name"],
15
+ hash["chapters"].map { |chapter_hash| Chapter.from_hash(chapter_hash) }
16
+ )
17
+ end
18
+ end
19
+
20
+ Chapter = Struct.new(:number, :verses) do
21
+ def to_hash
22
+ { "number" => number, "verses" => verses.map(&:to_hash) }
23
+ end
24
+
25
+ def self.from_hash(hash)
26
+ Chapter.new(
27
+ hash["number"],
28
+ hash["verses"].map { |verse_hash| Verse.from_hash(verse_hash) }
29
+ )
30
+ end
31
+ end
32
+
33
+ Verse = Struct.new(:text) do
34
+ def to_hash
35
+ { "text" => text }
36
+ end
37
+
38
+ def self.from_hash(hash)
39
+ Verse.new(hash["text"])
40
+ end
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,49 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ module Volumes
5
+ module ScienceHealth
6
+
7
+ Page = Struct.new(:number, :lines) do
8
+ def preface?
9
+ !!(number =~ /vi{1,3}|xi{1,2}|i{0,1}x/)
10
+ end
11
+
12
+ def to_hash
13
+ { "number" => number, "lines" => lines.map(&:to_hash) }
14
+ end
15
+
16
+ def self.from_hash(hash)
17
+ Page.new(
18
+ hash["number"],
19
+ hash["lines"].map { |line_hash| Line.from_hash(line_hash) }
20
+ )
21
+ end
22
+ end
23
+
24
+ Line = Struct.new(:text, :flyout_text, :paragraph_start) do
25
+ def has_flyout?
26
+ !!flyout_text
27
+ end
28
+
29
+ alias :paragraph_start? :paragraph_start
30
+
31
+ def to_hash
32
+ { "text" => text,
33
+ "flyout_text" => flyout_text,
34
+ "paragraph_start" => paragraph_start
35
+ }
36
+ end
37
+
38
+ def self.from_hash(hash)
39
+ Line.new(
40
+ hash["text"],
41
+ hash["flyout_text"],
42
+ !!hash["paragraph_start"]
43
+ )
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,8 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ module Volumes
5
+ autoload :ScienceHealth, "cskit/resources/volumes/science_health"
6
+ autoload :Bible, "cskit/resources/volumes/bible"
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ class Volume
5
+
6
+ attr_reader :config
7
+
8
+ def initialize(config)
9
+ @config = config
10
+ end
11
+
12
+ def parse_citation(citation_text)
13
+ parser.parse(citation_text).to_object
14
+ end
15
+
16
+ def readings_for(citation)
17
+ reader.readings_for(citation)
18
+ end
19
+
20
+ def parser
21
+ @parser ||= config[:parser].new
22
+ end
23
+
24
+ def reader
25
+ @reader ||= config[:reader].new(self)
26
+ end
27
+
28
+ def resource_path
29
+ config[:resource_path]
30
+ end
31
+
32
+ end
33
+ end
data/lib/cskit.rb ADDED
@@ -0,0 +1,73 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'cskit/formatters'
4
+ require 'cskit/lesson'
5
+ require 'cskit/parsers'
6
+ require 'cskit/readers'
7
+ require 'cskit/volume'
8
+ require 'cskit/resources/volumes'
9
+
10
+ require 'treetop'
11
+ require 'json'
12
+
13
+ module CSKit
14
+
15
+ autoload :Annotator, "cskit/annotator"
16
+ autoload :Annotation, "cskit/annotator"
17
+ autoload :AnnotatedString, "cskit/annotated_string"
18
+
19
+ class << self
20
+
21
+ def register_volume(config)
22
+ register(:volume, config[:id], config[:volume].new(config))
23
+ end
24
+
25
+ def register_annotator(config)
26
+ register(:annotator, config[:id], config[:annotator].new(config))
27
+ end
28
+
29
+ def get_volume(volume_id)
30
+ get(:volume, volume_id)
31
+ end
32
+
33
+ def get_annotator(annotator_id)
34
+ get(:annotator, annotator_id)
35
+ end
36
+
37
+ def volume_available?(volume_id)
38
+ available?(:volume, volume_id)
39
+ end
40
+
41
+ def annotator_available?(annotator_id)
42
+ available?(:annotator, annotator_id)
43
+ end
44
+
45
+ private
46
+
47
+ def register(family, key, obj)
48
+ (registry[family.to_sym] ||= {})[key.to_sym] = obj
49
+ end
50
+
51
+ def get(family, key)
52
+ registry[family.to_sym][key.to_sym] || get_for_type(family, key)
53
+ end
54
+
55
+ def get_for_type(family, type)
56
+ found = registry[family.to_sym].find do |key, obj|
57
+ obj.config[:type] == type
58
+ end
59
+
60
+ found.last if found
61
+ end
62
+
63
+ def available?(family, key)
64
+ !!registry[family.to_sym][key.to_sym] rescue false
65
+ end
66
+
67
+ def registry
68
+ @registry ||= {}
69
+ end
70
+
71
+ end
72
+
73
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cskit
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Cameron Dutro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
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: treetop
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: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Christian Science citation library for Ruby.
56
+ email:
57
+ - camertron@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/cskit/annotated_string.rb
63
+ - lib/cskit/annotator.rb
64
+ - lib/cskit/formatters/bible/bible_html_formatter.rb
65
+ - lib/cskit/formatters/bible/bible_plain_text_formatter.rb
66
+ - lib/cskit/formatters/bible.rb
67
+ - lib/cskit/formatters/science_health/science_health_html_formatter.rb
68
+ - lib/cskit/formatters/science_health/science_health_plain_text_formatter.rb
69
+ - lib/cskit/formatters/science_health.rb
70
+ - lib/cskit/formatters.rb
71
+ - lib/cskit/lesson/lesson.rb
72
+ - lib/cskit/lesson/section.rb
73
+ - lib/cskit/lesson.rb
74
+ - lib/cskit/parsers/bible/bible.rb
75
+ - lib/cskit/parsers/bible/bible.treetop
76
+ - lib/cskit/parsers/bible/nodes.rb
77
+ - lib/cskit/parsers/bible/objects.rb
78
+ - lib/cskit/parsers/science_health/nodes.rb
79
+ - lib/cskit/parsers/science_health/objects.rb
80
+ - lib/cskit/parsers/science_health/science_health.rb
81
+ - lib/cskit/parsers/science_health/science_health.treetop
82
+ - lib/cskit/parsers.rb
83
+ - lib/cskit/readers/bible_reader.rb
84
+ - lib/cskit/readers/reading.rb
85
+ - lib/cskit/readers/science_health_reader.rb
86
+ - lib/cskit/readers.rb
87
+ - lib/cskit/resources/volumes/bible.rb
88
+ - lib/cskit/resources/volumes/science_health.rb
89
+ - lib/cskit/resources/volumes.rb
90
+ - lib/cskit/version.rb
91
+ - lib/cskit/volume.rb
92
+ - lib/cskit.rb
93
+ - Gemfile
94
+ - History.txt
95
+ - LICENSE
96
+ - README.md
97
+ - Rakefile
98
+ - cskit.gemspec
99
+ homepage: http://github.com/camertron
100
+ licenses: []
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.0.5
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Christian Science citation library for Ruby.
122
+ test_files: []