Method: Slaw::Parse::Builder#find_term_references
- Defined in:
- lib/slaw/parse/builder.rb
#find_term_references(doc, terms) ⇒ Object
Find and decorate references to terms in the document. The terms param is a hash from term_id to actual term.
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/slaw/parse/builder.rb', line 296 def find_term_references(doc, terms) logger.info("+ Finding references to terms") i = 0 # sort terms by the length of the defined term, desc, # so that we don't find short terms inside longer # terms terms = terms.to_a.sort_by { |pair| -pair[1].size } # look for each term for term_id, term in terms doc.xpath('//a:body//text()', a: NS).each do |text| # replace all occurrences in this text node # unless we're already inside a def or term element next if (["def", "term"].include?(text.parent.name)) # don't link to a term inside its own definition owner = find_up(text, 'subsection') next if owner and owner.at_xpath(".//a:def[@refersTo='##{term_id}']", a: NS) while posn = (text.content =~ /\b#{Regexp::escape(term)}\b/) # <p>A delegation under subsection (1) shall not prevent the <term refersTo="#term-Minister" id="trm357">Minister</term> from exercising the power himself or herself.</p> node = doc.create_element('term', term, refersTo: "##{term_id}", id: "trm#{i}") pre = (posn > 0) ? text.content[0..posn-1] : nil post = text.content[posn+term.length..-1] text.before(node) node.before(doc.create_text_node(pre)) if pre text.content = post i += 1 end end end end |