Class: BibleML

Inherits:
Object
  • Object
show all
Defined in:
lib/filters/bible_ml.rb

Constant Summary collapse

VERSION_IDS =
{
  'cev' => 46,
  'niv' => 31,
  'nasb' => 49,
  'message' => 65,
  'amp' => 45,
  'nlt' => 51,
  '21st century kjv' => 48,
  'asv' => 8,
  'darby' => 16,
  'douay-rheims' => 63,
  'esv' => 47,
  'holman' => 77,
  'kjv' => 9,
  'ncv' => 78,
  'nkjv' => 50,
  'new life' => 74,
  'wycliffe' => 53,
  "young" => 15
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ BibleML

Returns a new instance of BibleML.



29
30
31
# File 'lib/filters/bible_ml.rb', line 29

def initialize(input)
  @xml = REXML::Document.new("<fg:body xmlns:fg='http://fromgenesis.org'>#{input}</fg:body>")
end

Class Method Details

.fill_missing_text(input) ⇒ Object



204
205
206
# File 'lib/filters/bible_ml.rb', line 204

def fill_missing_text(input)
  new(input).fill_missing_text.to_s
end

Instance Method Details



241
242
243
244
245
246
247
# File 'lib/filters/bible_ml.rb', line 241

def bg_link(ref, version = nil)
  ref = expand_reference(ref)
  
  ln = "http://www.biblegateway.com/passage/?search=" + ref.downcase.gsub(/\s/, "%20")
  ln += ";&version=#{VERSION_IDS[version.downcase]};" if version
  ln
end

#block_quotesObject

Convent fg:bq tags to block quotes with reference



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/filters/bible_ml.rb', line 84

def block_quotes
  @xml.elements.each("//fg:bq") do | bq_el |
    blockquote = REXML::Element.new("blockquote")
    
    if bq_el.has_elements? || bq_el.has_text? 
      bq_el.children.each do | child |
        blockquote << child
      end
    else
      blockquote << REXML::Text.new(get_passage(bq_el.attributes['p'], bq_el.attributes['v']))
    end
    
    blockquote << REXML::Text.new("\n")
    blockquote << REXML::Element.new("br")
    blockquote << REXML::Text.new("\n(")
    blockquote << reference_a(bq_el.attributes['p'], bq_el.attributes['v'])
    blockquote << REXML::Text.new(")\n")
    
    bq_el.parent.replace_child(bq_el, blockquote)
  end
  self
end

#expand_reference(ref) ⇒ Object

Add book and chapter information to shorthand references



214
215
216
217
218
219
220
221
222
223
# File 'lib/filters/bible_ml.rb', line 214

def expand_reference(ref)
  ref.strip!
  if ref.match(/\A(\d+-?\d*)\Z/) # Just verse(s) given
    "#{@passage_book} #{@passage_chapter}:#{ref}"
  elsif ref.match(/\A(\d+):(\d+-?\d*)\Z/) # Just chapter and verse(s) given
    "#{@passage_book} #{ref}"
  else
    ref
  end
end

#fill_missing_textObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/filters/bible_ml.rb', line 167

def fill_missing_text
  @xml.elements.each("//fg:pp") do | pp_el |
    chapter = pp_el.attributes['p'].split(":")[0]
    m = chapter.match(/(.*)\s(\d+)\Z/)
    @passage_book = m[1]
    @passage_chapter = m[2]
  end

  @xml.elements.each("//fg:iq") do | q_el |
    unless q_el.has_elements? || q_el.has_text? 
      q_el << REXML::Text.new(get_passage(q_el.attributes['p'], q_el.attributes['v']))
    end
  end

  @xml.elements.each("//fg:bq") do | q_el |
    unless q_el.has_elements? || q_el.has_text?
      psg = get_passage(q_el.attributes['p'], q_el.attributes['v'])
      psg = wordwrap(" " + psg) + "\n"
      q_el << REXML::Text.new(psg)
    end
  end
  self
end

#get_passage(ref, version = "NASB") ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/filters/bible_ml.rb', line 249

def get_passage(ref, version = "NASB")
  xhtml = open(bg_link(ref, version)) do |f|
    html = f.read.split('<div class="result-text-style-normal">')[1]
    html = html.split(/<\/?div/)[0]
    REXML::Document.new("<body>#{html}</body>")
  end

  xhtml.root.elements.delete_all("//h4")
  xhtml.root.elements.delete_all("//h5")
  xhtml.root.elements.delete_all("//span")
  xhtml.root.elements.delete_all("//sup")
  xhtml.root.elements.delete_all("//br")

  xhtml.to_s.gsub(/<\/?body>/, '').gsub(/<\/?p\/?>/, '').gsub('&nbsp;', ' ').squeeze(" ").gsub(/ \" /, ' "')
end

#inline_quotesObject

Convent fg:iq tags to inline quotes with reference



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/filters/bible_ml.rb', line 108

def inline_quotes
  @xml.elements.each("//fg:iq") do | iq_el |
    quote = REXML::Element.new("span")

    quote << REXML::Text.new("\"")
    
    if iq_el.has_elements? || iq_el.has_text? 
      iq_el.children.each do | child |
        quote << child
      end
    else
      quote << REXML::Text.new(get_passage(iq_el.attributes['p'], iq_el.attributes['v']))
    end
    
    quote << REXML::Text.new("\" (")
    quote << reference_a(iq_el.attributes['p'], iq_el.attributes['v'])
    quote << REXML::Text.new(")\n")
    
    iq_el.parent.replace_child(iq_el, quote)
  end
  self
end

#primary_passageObject

Convert fg:pp tags to Primary Passage links and set primary passage book and chapter from last fg:pp tag.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/filters/bible_ml.rb', line 50

def primary_passage
  @xml.elements.each("//fg:pp") do | pp_el |
    ref = pp_el.attributes['p']
    ref_link = REXML::Element.new("a")
    ref_link.add_attribute('href', bg_link(ref))
    ref_link.text = "Read #{ref}"
    pp_el.parent.insert_after(pp_el, ref_link)

    marker = REXML::Text.new(" |\n")
    pp_el.parent.insert_after(ref_link, marker)

    chapter = pp_el.attributes['p'].split(":")[0]
    chapter_link = REXML::Element.new("a")
    chapter_link.add_attribute('href', bg_link(chapter))
    chapter_link.text = "Full Chapter"
    pp_el.parent.insert_after(marker, chapter_link)
    
    pp_el.parent.delete_element(pp_el)
    
    m = chapter.match(/(.*)\s(\d+)\Z/)
    @passage_book = m[1]
    @passage_chapter = m[2]
  end
  
  self
end

#reference_a(ref, version = nil, label = nil) ⇒ Object

Generate ‘a’ element for reference link



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/filters/bible_ml.rb', line 226

def reference_a(ref, version = nil, label = nil)
  ref = expand_reference(ref)
  
  unless label
    label = ref
    label += ", #{version}" if version
  end
  
  ref_link = REXML::Element.new("a")
  ref_link.add_attribute('href', bg_link(ref, version))
  ref_link.text = label
  
  ref_link
end

#referencesObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/filters/bible_ml.rb', line 131

def references
  @xml.elements.each("//fg:ref") do | ref_el |
    label = ''
    
    if ref_el.has_elements? || ref_el.has_text? 
      ref_el.children.each do | child |
        label << child.to_s
      end
    end
    
    label = label.empty? ? nil : label.to_s
    a_tag = reference_a(ref_el.attributes['p'], ref_el.attributes['v'], label)
    
    ref_el.parent.replace_child(ref_el, a_tag)
  end
  self
end

#strip_commentsObject

Strip fg:cm tags



78
79
80
81
# File 'lib/filters/bible_ml.rb', line 78

def strip_comments
  @xml = REXML::Document.new(@xml.to_s.gsub(/\<\/?fg\:cm([^>]*)>/, ''))
  self
end

#to_htmlObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/filters/bible_ml.rb', line 33

def to_html
  txt = self.
    primary_passage.
    strip_comments.
    block_quotes.
    inline_quotes.
    references.
    wikipedia_links.
    to_s
  txt = RubyPants.new(txt).to_html
  txt = RDiscount.new(txt).to_html
  txt.gsub(/&amp;amp;/, '&amp;') # Get rid of double &amp's
end

#to_sObject



209
210
211
# File 'lib/filters/bible_ml.rb', line 209

def to_s
  @xml.to_s.gsub(/<\/?fg:body[^>]*>/, '')
end


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/filters/bible_ml.rb', line 149

def wikipedia_links
  @xml.elements.each("//fg:wp") do | wp_el |
    a = REXML::Element.new("a")
    
    a.add_attribute('href', "http://en.wikipedia.org/wiki/#{wp_el.attributes['a']}")
    if wp_el.has_elements? || wp_el.has_text? 
      wp_el.children.each do | child |
        a << child
      end
    else
      a << REXML::Text.new(wp_el.attributes['a'])
    end
    
    wp_el.parent.replace_child(wp_el, a)
  end
  self
end

#wordwrap(txt) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/filters/bible_ml.rb', line 192

def wordwrap(txt)
  txt.gsub(/\t/,"     ").
      gsub(/\"/,"&quot;").
      gsub(/\'/,"&apos;").
      gsub(/.{1,78}(?:\s|\Z)/){($& + 5.chr).
      gsub(/\n\005/,"\n  ").
      gsub(/\005/,"\n  ")}.
      gsub(/&quot;/, "\"").
      gsub(/&apos;/, "\'")
end