Class: DoctorNinja::Parsers::List

Inherits:
Base
  • Object
show all
Defined in:
lib/doctor_ninja/parsers/list.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #parse_children

Constructor Details

This class inherits a constructor from DoctorNinja::Parsers::Base

Class Method Details

.applicable_to?(node) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/doctor_ninja/parsers/list.rb', line 4

def self.applicable_to?(node)
  node.name == "p" && node.xpath("./w:pPr/w:numPr").length > 0
end

Instance Method Details

#get_pr(pr, node) ⇒ Object



43
44
45
46
47
# File 'lib/doctor_ninja/parsers/list.rb', line 43

def get_pr pr, node
  node.xpath(".//#{pr}/@w:val")[0].value
rescue
  nil
end

#parseObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/doctor_ninja/parsers/list.rb', line 8

def parse
  num_id = get_pr "w:numId", @node
  lvl = get_pr "w:ilvl", @node
  style = @document.numbering.style(lvl, num_id)
  tag = style == "bullet" ? "ul" : "ol"

  num_id_query = ".//w:numId/@w:val=\"#{num_id}\""

  previous = @node.xpath("preceding-sibling::w:p[#{num_id_query}][1]")
  following = @node.xpath("following-sibling::w:p[#{num_id_query}][1]")

  prefix = ""
  # Add <ul> if is start of new list
  if previous.length == 0 || get_pr("w:ilvl", previous) < lvl
    prefix += "<#{tag}>"
  end

  prefix += "<li>"
  
  suffix = ""

  if following.length == 0
    suffix += "</li></#{tag}>"
  else
    f_lvl = get_pr("w:ilvl", following)
    if f_lvl < lvl
      suffix += "</li></#{tag}></li>"
    elsif f_lvl == lvl
      suffix += "</li>"
    end
  end

  "#{prefix}#{parse_children}#{suffix}"
end