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



184
185
186
# File 'lib/filters/bible_ml.rb', line 184

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

Instance Method Details



221
222
223
224
225
226
227
# File 'lib/filters/bible_ml.rb', line 221

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



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

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



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

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



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

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



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/filters/bible_ml.rb', line 229

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



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

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.



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

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



206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/filters/bible_ml.rb', line 206

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

#strip_commentsObject

Strip fg:cm tags



76
77
78
79
# File 'lib/filters/bible_ml.rb', line 76

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
# File 'lib/filters/bible_ml.rb', line 33

def to_html
  txt = self.
    primary_passage.
    strip_comments.
    block_quotes.
    inline_quotes.
    wikipedia_links.
    to_s
  txt = RubyPants.new(txt).to_html
  txt = RDiscount.new(txt).to_html
end

#to_sObject



189
190
191
# File 'lib/filters/bible_ml.rb', line 189

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


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

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



172
173
174
175
176
177
178
179
180
181
# File 'lib/filters/bible_ml.rb', line 172

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