Class: USFX::Document
- Inherits:
-
Nokogiri::XML::SAX::Document
- Object
- Nokogiri::XML::SAX::Document
- USFX::Document
- 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
-
#characters(string) ⇒ Object
event fired when encountering text data in a tag.
-
#end_book_title ⇒ Object
event fired when book title is ended.
-
#end_element(name) ⇒ Object
dispatcher for closing tags.
-
#end_footnote ⇒ Object
event fired when footnote is ended.
-
#end_verse ⇒ Object
event fired when verse is ended.
-
#initialize ⇒ Document
constructor
A new instance of Document.
-
#start_book(attributes) ⇒ Object
event fired when book is started.
-
#start_book_title(attributes) ⇒ Object
event fired when book title is started.
-
#start_chapter(attributes) ⇒ Object
event fired when chapter is started.
-
#start_element(name, attributes) ⇒ Object
main dispatcher, calls other specific element event methods.
-
#start_footnote(attributes) ⇒ Object
event fired when footnote is started.
-
#start_verse(attributes) ⇒ Object
event fired when verse is started.
-
#verse(data) ⇒ Object
event fired upon completion of a verse verse data is passed as a hash of the form:.
Constructor Details
#initialize ⇒ Document
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
95 96 97 98 99 100 101 102 |
# File 'lib/usfx/document.rb', line 95 def characters(string) case @mode when 'book-title' @book << string unless string == "\n" when 'verse' @text << string end end |
#end_book_title ⇒ Object
event fired when book title is ended
62 63 64 |
# File 'lib/usfx/document.rb', line 62 def end_book_title @mode = nil end |
#end_element(name) ⇒ Object
dispatcher for closing tags
36 37 38 39 40 41 42 43 |
# File 'lib/usfx/document.rb', line 36 def end_element(name) case name when 'h' end_book_title when 'f', 'x' end_footnote end end |
#end_footnote ⇒ Object
event fired when footnote is ended
90 91 92 |
# File 'lib/usfx/document.rb', line 90 def end_footnote @mode = @mode.split('|').last end |
#end_verse ⇒ Object
event fired when verse is ended
79 80 81 82 |
# File 'lib/usfx/document.rb', line 79 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
46 47 48 49 50 51 52 53 54 |
# File 'lib/usfx/document.rb', line 46 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
57 58 59 |
# File 'lib/usfx/document.rb', line 57 def start_book_title(attributes) @mode = 'book-title' if @mode == 'book' end |
#start_chapter(attributes) ⇒ Object
event fired when chapter is started
67 68 69 |
# File 'lib/usfx/document.rb', line 67 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 |
# 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' 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
85 86 87 |
# File 'lib/usfx/document.rb', line 85 def start_footnote(attributes) @mode = "footnote|#{@mode}" end |
#start_verse(attributes) ⇒ Object
event fired when verse is started
72 73 74 75 76 |
# File 'lib/usfx/document.rb', line 72 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.
118 119 120 |
# File 'lib/usfx/document.rb', line 118 def verse(data) p(data) end |