cskit 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,117 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ module Formatters
5
+ module Bible
6
+ class BiblePlainTextFormatter < CSKit::Formatters::Formatter
7
+
8
+ def format_readings(readings)
9
+ join_readings(readings) do |reading|
10
+ format_reading(reading)
11
+ end
12
+ end
13
+
14
+ def format_annotated_readings(readings, annotation_formatter)
15
+ join_readings(readings) do |reading|
16
+ format_annotated_reading(reading, annotation_formatter)
17
+ end
18
+ end
19
+
20
+ def join(texts)
21
+ texts.join(separator)
22
+ end
23
+
24
+ protected
25
+
26
+ def join_readings(readings)
27
+ join(
28
+ readings.map do |reading|
29
+ join(yield(reading))
30
+ end
31
+ )
32
+ end
33
+
34
+ def format_annotated_reading(reading, annotation_formatter)
35
+ reading.texts.each_with_index.map do |text, index|
36
+ starter_pos = starter_position(text, reading.verse.starter)
37
+ terminator_pos = terminator_position(text, reading.verse.terminator)
38
+
39
+ annotated_str = CSKit::AnnotatedString.new(text, reading.annotations_at(index))
40
+ annotated_str.delete(terminator_pos + 1, text.length - (terminator_pos + 1)) if terminator_pos
41
+ annotated_str.delete(0, starter_pos) if starter_pos
42
+
43
+ text = annotated_str.render do |snippet, annotation|
44
+ annotation_formatter.format_annotation(annotation, snippet)
45
+ end
46
+
47
+ verse_number = reading.verse.start + index
48
+ attach_verse_number(verse_number, text)
49
+ end
50
+ end
51
+
52
+ def format_reading(reading)
53
+ reading.texts.each_with_index.map do |text, index|
54
+ if index == 0
55
+ pos = starter_position(text, reading.verse.starter)
56
+ text = format_starter(pos ? text[pos..-1].strip : text.strip, pos)
57
+ end
58
+
59
+ if index == reading.texts.size - 1
60
+ pos = terminator_position(text, reading.verse.terminator)
61
+ text = format_terminator(pos ? text[0..pos] : text, pos)
62
+ end
63
+
64
+ verse_number = reading.verse.start + index
65
+ attach_verse_number(verse_number, text)
66
+ end
67
+ end
68
+
69
+ def attach_verse_number(verse_number, text)
70
+ include_verse_number? ? "#{verse_number} #{text}" : text
71
+ end
72
+
73
+ def starter_position(text, starter)
74
+ if starter
75
+ regex = /\s+#{starter.fragment}([\s,\.\-_\?\!\.;:]|$)/
76
+ find_position(text, regex, starter.cardinality)
77
+ end
78
+ end
79
+
80
+ def format_starter(text, pos)
81
+ pos ? "..." + text : text
82
+ end
83
+
84
+ def terminator_position(text, terminator)
85
+ if terminator
86
+ regex = /#{Regexp.escape(terminator.fragment)}/
87
+ find_position(text, regex, terminator.cardinality)
88
+ end
89
+ end
90
+
91
+ def format_terminator(text, pos)
92
+ text
93
+ end
94
+
95
+ def find_position(text, regex, cardinality)
96
+ (0...cardinality).inject(-1) do |last_index, _|
97
+ return nil unless last_index
98
+ text.index(regex, last_index + 1)
99
+ end
100
+ end
101
+
102
+ def separator
103
+ options[:separator] || " "
104
+ end
105
+
106
+ def include_verse_number?
107
+ if options[:include_verse_number].nil?
108
+ true # default
109
+ else
110
+ !!options[:include_verse_number]
111
+ end
112
+ end
113
+
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ module Formatters
5
+ module Bible
6
+
7
+ autoload :BiblePlainTextFormatter, "cskit/formatters/bible/bible_plain_text_formatter"
8
+ autoload :BibleHtmlFormatter, "cskit/formatters/bible/bible_html_formatter"
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ module Formatters
5
+ module ScienceHealth
6
+ class ScienceHealthHtmlFormatter < ScienceHealthPlainTextFormatter
7
+
8
+ # _word_
9
+ ITALICS_REGEX = /_([a-zA-Z0-9\-\.,\"\'\?]+)_/
10
+
11
+ protected
12
+
13
+ def format_lines(lines, citation_line)
14
+ detect_italics(super)
15
+ end
16
+
17
+ def detect_italics(lines)
18
+ lines.map do |line|
19
+ line.gsub(ITALICS_REGEX) do |match|
20
+ "<em>#{$1}</em>"
21
+ end
22
+ end
23
+ end
24
+
25
+ def separator
26
+ options[:separator] || "<br />"
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,73 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ module Formatters
5
+ module ScienceHealth
6
+ class ScienceHealthPlainTextFormatter < CSKit::Formatters::Formatter
7
+
8
+ # semicolon, question mark, or period
9
+ SENTENCE_TERMINATOR_REGEX = /[;\?\.]/
10
+
11
+ # either a period + space, quotes, or start of line followed by the first capital letter or number.
12
+ SENTENCE_START_REGEX = /(\.\s+|\.\"\s+|\.\'\s+|^)[A-Z0-9\"\']/
13
+
14
+ def format_readings(readings)
15
+ join(
16
+ readings.map do |reading|
17
+ format_lines(reading.texts, reading.citation)
18
+ end
19
+ )
20
+ end
21
+
22
+ def join(texts)
23
+ texts.join(separator)
24
+ end
25
+
26
+ protected
27
+
28
+ def format_lines(lines, citation_line)
29
+ lines.each_with_index.map do |line, line_index|
30
+ line_text = line.text
31
+
32
+ line_text = if line_index == 0
33
+ if citation_line.start_fragment
34
+ index = line_text.index(citation_line.start_fragment)
35
+ index ? line_text[index..-1] : line_text
36
+ else
37
+ matches = line_text.match(SENTENCE_START_REGEX)
38
+ if matches && matches.length == 2
39
+ offset = matches.offset(1)
40
+ spaces = " " * offset.first # indent to match physical position in S&H book
41
+ spaces + line_text[matches.offset(1).last..-1]
42
+ else
43
+ line_text
44
+ end
45
+ end
46
+ else
47
+ line_text
48
+ end
49
+
50
+ line_text = if line_index == (lines.size - 1)
51
+ index = line_text.index(SENTENCE_TERMINATOR_REGEX)
52
+ index ? line_text[0..index] : line_text
53
+ else
54
+ line_text
55
+ end
56
+
57
+ # indent start of paragraph
58
+ if line.paragraph_start?
59
+ " #{line_text}"
60
+ else
61
+ line_text
62
+ end
63
+ end
64
+ end
65
+
66
+ def separator
67
+ options[:separator] || "\n"
68
+ end
69
+
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ module Formatters
5
+ module ScienceHealth
6
+
7
+ autoload :ScienceHealthPlainTextFormatter, "cskit/formatters/science_health/science_health_plain_text_formatter"
8
+ autoload :ScienceHealthHtmlFormatter, "cskit/formatters/science_health/science_health_html_formatter"
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ module Formatters
5
+
6
+ autoload :Bible, "cskit/formatters/bible"
7
+ autoload :ScienceHealth, "cskit/formatters/science_health"
8
+
9
+
10
+ class Formatter
11
+ attr_reader :options
12
+
13
+ def initialize(options = {})
14
+ @options = options
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,72 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'json'
4
+
5
+ module CSKit
6
+ module Lesson
7
+ class Lesson
8
+
9
+ attr_reader :sections
10
+
11
+ class << self
12
+ def from_file(file)
13
+ ext = File.extname(file)[1..-1]
14
+ send(:"from_#{ext}", File.read(file))
15
+ end
16
+
17
+ def from_json(data)
18
+ sections = JSON.parse(data).map do |section_data|
19
+ Section.new(
20
+ section_data["section"],
21
+ section_data["readings"].inject({}) do |ret, (k, v)|
22
+ ret[k.to_sym] = v; ret
23
+ end
24
+ )
25
+ end
26
+
27
+ new(sections)
28
+ end
29
+ end
30
+
31
+ def initialize(sections)
32
+ @sections = sections
33
+ end
34
+
35
+ def each_reading(volumes)
36
+ sections.each do |section|
37
+ volumes.each do |volume|
38
+ section.each_reading_group_for(volume) do |readings, citation|
39
+ yield section, citation, volume, readings
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ def each_formatted_reading(formatters)
46
+ each_reading(formatters.keys) do |section, citation, volume, readings|
47
+ formatter = formatters[volume]
48
+ yield section, citation, volume, formatter.format_readings(readings)
49
+ end
50
+ end
51
+
52
+ def each_formatted_section(formatters)
53
+ last_section = nil
54
+ current_texts = nil
55
+
56
+ each_formatted_reading(formatters) do |section, citation, volume, text|
57
+ if section.name != (last_section.name rescue nil)
58
+ yield section, current_texts if current_texts
59
+ current_texts = {}
60
+ end
61
+
62
+ last_section = section
63
+ formatter = formatters[volume]
64
+ current_texts[volume] = formatter.join([current_texts[volume], text])
65
+ end
66
+
67
+ yield last_section, current_texts if current_texts
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ module Lesson
5
+ class Section
6
+
7
+ attr_reader :name, :citation_texts
8
+
9
+ def initialize(name, citation_texts)
10
+ @name = name
11
+ @citation_texts = citation_texts
12
+ end
13
+
14
+ def each_reading_group_for(volume_name)
15
+ volume_name = volume_name.to_sym
16
+ volume = CSKit.get_volume(volume_name)
17
+
18
+ (citation_texts[volume_name] || []).each do |citation_text|
19
+ citation = volume.parse_citation(citation_text)
20
+ yield volume.readings_for(citation), citation
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+
3
+ module CSKit
4
+ module Lesson
5
+
6
+ autoload :Lesson, "cskit/lesson/lesson"
7
+ autoload :Section, "cskit/lesson/section"
8
+
9
+ end
10
+ end