Class: USFX::Document

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/usfx/document.rb

Overview

The Document class used by Nokogiri SAX Parser, with an added “verse” event for convenience

Constant Summary collapse

IGNORE_BOOK_IDS =

list of book ids that should be skipped

%w(FRT)

Instance Method Summary collapse

Constructor Details

#initializeDocument

Returns a new instance of Document.



12
13
14
15
# File 'lib/usfx/document.rb', line 12

def initialize
  super
  @book_num = 0
end

Instance Method Details

#characters(string) ⇒ Object

event fired when encountering text data in a tag



98
99
100
101
102
103
104
105
# File 'lib/usfx/document.rb', line 98

def characters(string)
  case @mode
  when 'book-title'
    @book << string unless string == "\n"
  when 'verse'
    @text << string
  end
end

#end_book_titleObject

event fired when book title is ended



65
66
67
# File 'lib/usfx/document.rb', line 65

def end_book_title
  @mode = nil
end

#end_element(name) ⇒ Object

dispatcher for closing tags



37
38
39
40
41
42
43
44
45
46
# File 'lib/usfx/document.rb', line 37

def end_element(name)
  case name
  when 'book', 'c'
    end_verse if @mode == 'verse'
  when 'h'
    end_book_title
  when 'f', 'x'
    end_footnote
  end
end

#end_footnoteObject

event fired when footnote is ended



93
94
95
# File 'lib/usfx/document.rb', line 93

def end_footnote
  @mode = @mode.split('|').last
end

#end_verseObject

event fired when verse is ended



82
83
84
85
# File 'lib/usfx/document.rb', line 82

def end_verse
  @mode = nil
  verse(book_num: @book_num, book_id: @book_id, book: @book, chapter: @chapter, verse: @verse, text: @text)
end

#start_book(attributes) ⇒ Object

event fired when book is started



49
50
51
52
53
54
55
56
57
# File 'lib/usfx/document.rb', line 49

def start_book(attributes)
  id = Hash[attributes]['id']
  unless IGNORE_BOOK_IDS.include?(id)
    @book_num += 1
    @book_id = id
    @mode = 'book'
    @book = ''
  end
end

#start_book_title(attributes) ⇒ Object

event fired when book title is started



60
61
62
# File 'lib/usfx/document.rb', line 60

def start_book_title(attributes)
  @mode = 'book-title' if @mode == 'book'
end

#start_chapter(attributes) ⇒ Object

event fired when chapter is started



70
71
72
# File 'lib/usfx/document.rb', line 70

def start_chapter(attributes)
  @chapter = Hash[attributes]['id'].to_i
end

#start_element(name, attributes) ⇒ Object

main dispatcher, calls other specific element event methods



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/usfx/document.rb', line 18

def start_element(name, attributes)
  case name
  when 'book'
    start_book(attributes)
  when 'h'
    start_book_title(attributes)
  when 'c'
    start_chapter(attributes)
  when 'v'
    end_verse if @mode == 'verse'
    start_verse(attributes)
  when 've'
    end_verse
  when 'f', 'x'
    start_footnote(attributes)
  end
end

#start_footnote(attributes) ⇒ Object

event fired when footnote is started



88
89
90
# File 'lib/usfx/document.rb', line 88

def start_footnote(attributes)
  @mode = "footnote|#{@mode}"
end

#start_verse(attributes) ⇒ Object

event fired when verse is started



75
76
77
78
79
# File 'lib/usfx/document.rb', line 75

def start_verse(attributes)
  @verse = Hash[attributes]['id'].to_i
  @text = ''
  @mode = 'verse'
end

#verse(data) ⇒ Object

event fired upon completion of a verse verse data is passed as a hash of the form:

{
  book_num: 1,
  book_id: 'GEN',
  book: 'Genesis',
  chapter: 1,
  verse: 1,
  text: 'In the beginning, God created the heavens and the earth.'
}

By default, this event will print the raw verse data. Override in your subclass to do fun stuff.



121
122
123
# File 'lib/usfx/document.rb', line 121

def verse(data)
  p(data)
end