Class: Html2Doc::JIS

Inherits:
Html2Doc show all
Defined in:
lib/html2doc/lists.rb

Instance Method Summary collapse

Instance Method Details

#cleanup(docxml) ⇒ Object



192
193
194
195
196
197
198
199
# File 'lib/html2doc/lists.rb', line 192

def cleanup(docxml)
  super
  docxml.xpath("//div[@class = 'Quote' or @class = 'Example' or " \
               "@class = 'Note']").each do |d|
    d.delete("class")
  end
  docxml
end

#indent_lists(docxml) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/html2doc/lists.rb', line 131

def indent_lists(docxml)
  docxml.xpath("//div[@class = 'Note' or @class = 'Example' or " \
               "@class = 'Quote']").each do |d|
    d.xpath(".//p").each do |p|
      indent_lists1(p)
    end
  end
end

#indent_lists1(para) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/html2doc/lists.rb', line 140

def indent_lists1(para)
  m = /^(ListContinue|ListNumber|MsoListContinue|MsoListNumber)(\d)$/
    .match(para["class"]) or return
  base = m[1].sub(/^Mso/, "")
  level = m[2].to_i + 1
  level = 5 if level > 5
  para["class"] = "#{base}#{level}-"
end

#list2para(list) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/html2doc/lists.rb', line 3

def list2para(list)
  return if list.xpath("./li").empty?

  list.xpath("./li").each do |l|
    list2para_level(l, list)
  end
end

#list2para_class(listtype, depth) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/html2doc/lists.rb', line 61

def list2para_class(listtype, depth)
  case listtype
  when "ol" then "MsoList"
  when "ul"
    case depth
    when "1" then "MsoListBullet"
    when "2", "3", "4", "5" then "MsoListBullet#{depth}"
    else "MsoListBullet6"
    end
  end
end

#list2para_level(item, list) ⇒ Object



17
18
19
20
21
22
# File 'lib/html2doc/lists.rb', line 17

def list2para_level(item, list)
  level = item["level"]
  item.delete("level")
  item.name = "p"
  list2para_nest(item, level, list) if level
end

#list2para_nest(item, level, list) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/html2doc/lists.rb', line 24

def list2para_nest(item, level, list)
  item["class"] = list2para_style(list.name, level)
  item.xpath("./p").each do |p|
    p["class"] = list2para_class(list.name, level)
    p["style"] = item["style"]
    (a = list2para_style(list.name, level)) && !a.empty? and
      p["style"] = "#{a};#{p['style']}"
    p["id"] = item["id"]
  end
  item.at("./p") or return
  item.replace(item.children)
=begin
  prev = p1.xpath("./preceding-sibling::* | ./preceding-sibling::text()")
  if prev[-1].name == "span" && prev[-1]["style"] == "mso-tab-count:1" &&
      prev.size == 2
    p1.children.first.previous = prev[1]
    p1.children.first.previous = prev[0]
  end
=end
end

#list2para_style(listtype, depth) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/html2doc/lists.rb', line 73

def list2para_style(listtype, depth)
  case listtype
  when "ol"
    ret = case depth
    when "1" then "margin-left: 36.0pt;"
    when "2" then "margin-left: 54.0pt;"
    when "3" then "margin-left: 72.0pt;"
    when "4" then "margin-left: 90.0pt;"
    when "5" then "margin-left: 108.0pt;"
    when "6" then "margin-left: 126.0pt;"
    when "7" then "margin-left: 144.0pt;"
    when "8" then "margin-left: 162.0pt;"
    else "margin-left: 180.0pt;"
    end
    "#{ret}text-indent:-18.0pt;"
  when "ul"
    ret = case depth
   when "1" then "margin-left: 36.0pt;"
    when "2" then "margin-left: 45.95pt;"
    when "3" then "margin-left: 72.0pt;"
    when "4" then "margin-left: 90.0pt;"
    when "5" then "margin-left: 108.0pt;"
    when "6" then "margin-left: 126.0pt;"
    when "7" then "margin-left: 144.0pt;"
    when "8" then "margin-left: 162.0pt;"
    else "margin-left: 180.0pt;"
    end
    "#{ret}text-indent:-18.0pt;"

  end
end

#list2para_unnest_para(para, first_p, last_p) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/html2doc/lists.rb', line 45

def list2para_unnest_para(para, first_p, last_p)
  return if last_p.xpath("./following-sibling::* | ./following-sibling::text()")
    .any? do |x|
              !x.text.strip.empty?
            end

  prev = first_p.xpath("./preceding-sibling::* | " \
                       "./preceding-sibling::text()[normalize-space()]")
  # bullet, tab, paragraph: ignore bullet, tab
  if prev.empty? then para.replace(para.children)
  elsif prev.size == 2 && prev[-1].name == "span" &&
      prev[-1]["style"] == "mso-tab-count:1"
    first_p.replace(first_p.children)
  end
end

#list_add(xpath, liststyles, listtype, level) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/html2doc/lists.rb', line 149

def list_add(xpath, liststyles, listtype, level)
  xpath.each do |l|
    level == 1 and l["seen"] = true and @listnumber += 1
    l["seen"] = true if level == 1
    l["id"] ||= UUIDTools::UUID.random_create
    list_add_number(l, liststyles, listtype, level)
    list_add_tail(l, liststyles, listtype, level)
  end
end

#list_add_number(list, liststyles, listtype, level) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/html2doc/lists.rb', line 159

def list_add_number(list, liststyles, listtype, level)
  i = list["start"] and @listnumber = list["start"].to_i - 1
  (list.xpath(".//li") - list.xpath(".//ol//li | .//ul//li")).each do |li|
    style_list(li, level, liststyles[listtype], @listnumber)
    list_add1(li, liststyles, listtype, level)
  end
end

#list_add_tail(list, liststyles, listtype, level) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/html2doc/lists.rb', line 167

def list_add_tail(list, liststyles, listtype, level)
  list.xpath(".//ul[not(ancestor::li/ancestor::*/@id = '#{list['id']}')] | " \
             ".//ol[not(ancestor::li/ancestor::*/@id = '#{list['id']}')]")
    .each do |li|
    list_add1(li.parent, liststyles, listtype, level - 1)
  end
end

#listidx(idx, level) ⇒ Object



182
183
184
185
186
187
188
189
190
# File 'lib/html2doc/lists.rb', line 182

def listidx(idx, level)
  case level
  when "a" then (96 + idx).chr.to_s
  when "1" then idx.to_s
  when "i" then RomanNumerals.to_roman(idx).downcase
  when "A" then (64 + idx).chr.to_s
  when "I" then RomanNumerals.to_roman(idx).upcase
  end
end

#listlabel(listtype, idx, level) ⇒ Object



175
176
177
178
179
180
# File 'lib/html2doc/lists.rb', line 175

def listlabel(listtype, idx, level)
  case listtype
  when :ul then "—"
  when :ol then "#{listidx(idx, level)})"
  end
end

#lists(docxml, liststyles) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/html2doc/lists.rb', line 105

def lists(docxml, liststyles)
  super
  docxml.xpath("//p[ol | ul]").each do |p|
    list2para_unnest_para(p, p.at("./ol | ./ul"),
                          p.at("./*[name() = 'ul' or name() = 'ol'][last()]"))
  end
  docxml.xpath("//ol | //ul").each do |u|
    u.replace(u.children)
  end
  unnest_list_paras(docxml)
  indent_lists(docxml)
end

#style_list(elem, level, liststyle, listnumber) ⇒ Object



11
12
13
14
15
# File 'lib/html2doc/lists.rb', line 11

def style_list(elem, level, liststyle, listnumber)
  return unless liststyle
  elem["level"] = level
  super
end

#unnest_list_paras(docxml) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/html2doc/lists.rb', line 118

def unnest_list_paras(docxml)
  docxml.xpath("//p[@class = 'ListContinue1' or @class = 'ListNumber1']" \
               "[.//p]").each do |p|
                 p.at("./p") and
                   list2para_unnest_para(p, p.at("./p"),
                                         p.at("./p[last()]"))
                 p.xpath(".//p[p]").each do |p1|
                   list2para_unnest_para(p1, p1.at("./p"),
                                         p1.at("./p[last()]"))
                 end
               end
end