Class: Nokogiri::XML::Node

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

Overview

Add some helper methods to XML node objects

Instance Method Summary collapse

Instance Method Details

#chaptersObject



21
22
23
# File 'lib/slaw/xml_support.rb', line 21

def chapters
  xpath('./a:chapter', a: Slaw::NS)
end

#headingObject



12
13
14
15
# File 'lib/slaw/xml_support.rb', line 12

def heading
  node = at_xpath('./a:heading', a: Slaw::NS)
  node ? node.text : nil
end

#idObject



17
18
19
# File 'lib/slaw/xml_support.rb', line 17

def id
  self['id']
end

#in_schedules?Boolean

Is this element part of a schedule document?

Returns:

  • (Boolean)


74
75
76
# File 'lib/slaw/xml_support.rb', line 74

def in_schedules?
  not ancestors('doc[name="schedules"]').empty?
end

#numObject

The AkomaNtoso number of this node, or nil if unknown. Major AN elements such as chapters, parts and sections almost always have numbers.



7
8
9
10
# File 'lib/slaw/xml_support.rb', line 7

def num
  node = at_xpath('./a:num', a: Slaw::NS)
  node ? node.text.gsub(/\.$/, '') : nil
end

#partsObject



29
30
31
# File 'lib/slaw/xml_support.rb', line 29

def parts
  xpath('./a:part', a: Slaw::NS)
end

#sectionsObject



25
26
27
# File 'lib/slaw/xml_support.rb', line 25

def sections
  xpath('./a:section', a: Slaw::NS)
end

#toc_childrenObject

Get a nodeset of child elements of this node which should show up in the table of contents



35
36
37
38
39
40
41
42
43
# File 'lib/slaw/xml_support.rb', line 35

def toc_children
  if in_schedules?
    # in schedules, we only care about chapters that have numbers in them, because we
    # can't link to them otherwise
    xpath('a:chapter[a:num]', a: Slaw::NS)
  else
    xpath('a:part | a:chapter | a:section', a: Slaw::NS)
  end
end

#toc_container?Boolean

Does this element hold children for the table of contents?

Returns:

  • (Boolean)


46
47
48
# File 'lib/slaw/xml_support.rb', line 46

def toc_container?
  !toc_children.empty?
end

#toc_titleObject

Title for this element in the table of contents



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/slaw/xml_support.rb', line 51

def toc_title
  case name
  when "mainBody"
    "Schedules"
  when "chapter"
    title = in_schedules? ? "Schedule" : "Chapter" 
    title << ' ' + num
    title << ' - ' + heading if heading
    title
  when "part"
    "Part #{num} - #{heading}"
  when "section"
    if not heading or heading.empty?
      "Section #{num}" 
    elsif num
      "#{num}. #{heading}"
    else
      heading
    end
  end        
end