81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
# File 'lib/coradoc/input/html/converters/table.rb', line 81
def ensure_row_column_integrity_and_get_column_sizes(node)
rows = node.xpath(".//tr")
num_rows = rows.length
computed_columns_per_row = [0] * num_rows
cell_references = [nil] * num_rows
cell_matrix = [nil] * num_rows
recompute = proc do
return ensure_row_column_integrity_and_get_column_sizes(node)
end
fits_in_cell_matrix = proc do |y, x, rowspan, colspan|
rowspan.times.all? do |yy|
colspan.times.all? do |xx|
!cell_matrix.dig(y + yy, x + xx)
end
end
end
rows.each_with_index do |row, i|
columns = row.xpath("./td | ./th")
column_id = 0
cell_references[i] ||= []
cell_matrix[i] ||= []
cell_references[i].instance_variable_set(:@row_obj, row)
columns.each do |cell|
colspan = cell["colspan"]&.to_i || 1
rowspan = cell["rowspan"]&.to_i || 1
column_id += 1 until fits_in_cell_matrix.(i, column_id, rowspan,
colspan)
rowspan.times do |j|
computed_columns_per_row[i + j] ||= 0
computed_columns_per_row[i + j] += colspan
cell_references[i + j] ||= []
cell_matrix[i + j] ||= []
colspan.times do |k|
cell_references[i + j] << [cell, k.positive?]
cell_matrix[i + j][column_id] = cell
column_id += 1
end
column_id -= colspan
end
column_id += colspan
end
end
cpr = computed_columns_per_row
if cpr.length > num_rows
cell_references[num_rows].each do |cell,|
next unless cell
cell["rowspan"] = cell["rowspan"].to_i - 1
end
recompute.()
end
if [cpr.first] * num_rows != cpr
modified = false
cpr_max = cpr.max
max_rows = cell_references.sort_by(&:length).reverse
max_rows.each do |row|
break if row.length != cpr_max
cell, spanning = row.last
if spanning
modified = true
cell["colspan"] = cell["colspan"].to_i - 1
end
end
recompute.() if modified
min_rows = cell_references.sort_by(&:length)
cpr_min = cpr.min
min_rows.each do |row|
break if row.length != cpr_min
row_obj = row.last&.first&.parent || row.instance_variable_get(:@row_obj)
doc = row_obj.document
added_node = Nokogiri::XML::Node.new("td", doc)
added_node["x-added"] = "x-added"
row_obj.add_child(added_node)
modified = true
end
recompute.() if modified
warn "**** Couldn't fix table sizes for table on line #{node.line}"
end
cell_matrix_correct = cell_matrix.length.times.all? do |y|
cell_matrix[y].length.times.all? do |x|
cell_matrix[y][x]
end
end
unless cell_matrix_correct
needs_recompute = false
cell_matrix.each do |row|
if row.compact.length != row.length
last_cell = row.last
if last_cell["x-added"]
last_cell.parent.prepend_child(last_cell)
needs_recompute = true
end
end
end
recompute.() if needs_recompute
warn <<~WARNING.tr("\n", " ")
**** Couldn't construct a valid image of a table on line
#{node.line}. We need that to reliably compute column
widths of that table. Please report a bug to metanorma/coradoc
repository.
WARNING
end
column_sizes = []
cell_matrix.each do |row|
row.each_with_index do |(cell, _), i|
next unless !cell || [nil, "", "1"].include?(cell["colspan"])
column_sizes[i] ||= []
column_sizes[i] << cell["width"]
end
end
document_width = Coradoc::Input::Html.config.doc_width.to_r
column_sizes += [nil] * (cpr.first - column_sizes.length)
sizes = column_sizes.map do |col|
col = [] if col.nil?
max = col.map do |i|
if i.nil?
0r
elsif i.end_with?("%")
document_width * i.to_i / 100
else
i.to_r
end
end.max
if max.nil? || max.negative?
0r
else
max
end
end
while sizes.map { |i|
if i.zero?
document_width / 3 / sizes.length
else
i
end
}.sum > document_width
sizes = sizes.map { |i| i * 4 / 5 }
end
rest = document_width - sizes.sum
unset = sizes.count(&:zero?)
sizes = sizes.map do |i|
if i.zero?
rest / unset
else
i
end
end
lcm = sizes.map(&:denominator).inject(1) { |i, j| i.lcm(j) }
sizes = sizes.map { |i| i * lcm }.map(&:to_i)
gcd = sizes.inject(sizes.first) { |i, j| i.gcd(j) }
sizes = sizes.map { |i| i / gcd }
if [sizes.first] * sizes.length == sizes
sizes.length
else
sizes.join(",")
end
end
|