Class: KjDotXml

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = nil, debug: false) ⇒ KjDotXml

Returns a new instance of KjDotXml.



86
87
88
89
90
91
92
93
# File 'lib/kjdotxml.rb', line 86

def initialize(title=nil, debug: false)
  
  @debug = debug
  
  book(title) if title
  @booklist = BOOKS
  
end

Instance Attribute Details

#booklistObject (readonly)

Returns the value of attribute booklist.



84
85
86
# File 'lib/kjdotxml.rb', line 84

def booklist
  @booklist
end

Instance Method Details

#book(s) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/kjdotxml.rb', line 95

def book(s)
      
  filename = s.downcase.gsub(/\s/,'-') + '.xml'
  filepath = File.join('/home/james/jamesrobertson.eu/rorb/r/ruby/kjdotxml', filename)
  #filepath = File.join(File.dirname(__FILE__), '..', 'xml', filename)
  
  if File.exists? filepath then
    
    contents = File.read(filepath)
    @doc = Rexle.new(contents)
    
  else
    raise KjDotXmlError, 'book not found'
  end
  
end

#books(ref = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/kjdotxml.rb', line 112

def books(ref=nil)           
  
  return @booklist.map {|x| books(x) } unless ref

  index = ref.to_s[/^\d+$/] ? (ref.to_i - 1) : find_book(ref.downcase)
  puts 'index: ' + index.inspect if @debug
  
  title = @booklist[index]


end

#text(s) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/kjdotxml.rb', line 124

def text(s)

  s.strip.lines.map do |line|

    [line, verses(line.chomp)]

  end

end

#to_docObject



162
163
164
# File 'lib/kjdotxml.rb', line 162

def to_doc()
  @doc
end

#verses(s) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/kjdotxml.rb', line 134

def verses(s)

  r = s.match(/(?<book>\w+)\s+(?<chapter>\d+)[,\:]?\s*(?<verses>.*)/)
  verses2 = r[:verses] =~ /(?:\.\.|,|-)/ ? Range.new(*r[:verses]\
                      .split(/(?:\.\.|,|-)/).map(&:to_i)).to_a : r[:verses]
  puts 'r[:book]: ' + r[:book].inspect if @debug
  
  doc = book(books(r[:book]))
  
  puts 'verses2: ' + verses2.inspect if @debug
  
  if verses2.is_a? String then
    
    [
      doc.root.element("chapter[@no='#{r[:chapter]}']/verse" + 
                       "[@no='#{verses2}']")
    ]
    
  elsif verses2.is_a? Array
    
    verses2.map do |verse|
      doc.root.element("chapter[@no='#{r[:chapter]}']/verse[@no='#{verse}']")
    end
    
  end
end