Module: IsoDoc::Function::Table

Included in:
Common
Defined in:
lib/isodoc/function/table.rb

Constant Summary collapse

SW =
"solid windowtext".freeze

Instance Method Summary collapse

Instance Method Details

#bordered_table_style(node, klass = nil) ⇒ Object

Bordering is governed by %plain only, NOT by the presence of a custom carrying a custom class, or the internal modspec variant -- keeps the ISO borders. The internal rouge-line-table (sourcecode line numbering) and dl (a definition list rendered as a table) are exceptions: like %plain they are unbordered and mutually exclusive with MsoISOTable.



55
56
57
58
59
# File 'lib/isodoc/function/table.rb', line 55

def bordered_table_style(node, klass = nil)
  node["plain"] == "true" and return ""
  %w(rouge-line-table dl).include?(klass) and return ""
  "border-width:1px;border-spacing:0;"
end

#colgroup(node, table) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/isodoc/function/table.rb', line 90

def colgroup(node, table)
  colgroup = node.at(ns("./colgroup")) or return
  table.colgroup do |cg|
    colgroup.xpath(ns("./col")).each do |c|
      cg.col style: "width: #{c['width']};"
    end
  end
end

#make_tr_attr(cell, row, totalrows, header, bordered) ⇒ Object

def make_tr_attr(td, row, totalrows, cols, totalcols, header) border-left:#? "#{SW 1.5pt;" : "none;"} border-right:#SW #== totalcols && !header ? "1.5" : "1.0"pt;



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/isodoc/function/table.rb', line 144

def make_tr_attr(cell, row, totalrows, header, bordered)
  style = cell.name == "th" ? "font-weight:bold;" : ""
  cell["style"] and style += "#{cell['style']};"
  cell["align"] and style += "text-align:#{cell['align']};"
  cell["valign"] and style += "vertical-align:#{cell['valign']};"
  rowmax = cell["rowspan"] ? row + cell["rowspan"].to_i - 1 : row
  style += make_tr_attr_style(row, rowmax, totalrows, header, bordered)
  header and scope = (cell["colspan"] ? "colgroup" : "col")
  !header && cell.name == "th" and
    scope = (cell["rowspan"] ? "rowgroup" : "row")
  { rowspan: cell["rowspan"], colspan: cell["colspan"],
    style: style.delete("\n"), scope: scope, class: cell["class"] }
end

#make_tr_attr_style(row, rowmax, totalrows, _header, bordered) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/isodoc/function/table.rb', line 158

def make_tr_attr_style(row, rowmax, totalrows, _header, bordered)
  bordered or return ""
  <<~STYLE.delete("\n")
    border-top:#{row.zero? ? "#{SW} 1.5pt;" : 'none;'}
    border-bottom:#{SW} #{rowmax >= totalrows ? '1.5' : '1.0'}pt;
  STYLE
end

#new_fullcolspan_row(table, tfoot) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/isodoc/function/table.rb', line 207

def new_fullcolspan_row(table, tfoot)
  # how many columns in the table?
  cols = 0
  table.at(".//tr").xpath("./td | ./th").each do |td|
    cols += (td["colspan"] ? td["colspan"].to_i : 1)
  end
  table["class"].nil? or # = plain table
    s = "style='border-top:0pt;border-bottom:#{SW} 1.5pt;'"
  tfoot.add_child("<tr><td colspan='#{cols}' #{s}/></tr>")
  tfoot.xpath(".//td").last
end

#remove_bottom_border(cell) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/isodoc/function/table.rb', line 188

def remove_bottom_border(cell)
  # [^;]* (not +): the preceding property name is the unambiguous
  # delimiter, so zero-or-more is equivalent and avoids polynomial
  # backtracking on the value portion.
  cell["style"] &&=
    cell["style"].gsub(/border-bottom:[^;]*;/, "border-bottom:0pt;")
end

#table_attrs(node) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/isodoc/function/table.rb', line 61

def table_attrs(node)
  c = node["class"]
  style = table_attrs_style(node, c)
  attr_code(id: node["id"],
            class: table_html_class(node, c),
            style: style, title: node["alt"])
end

#table_attrs_style(node, klass) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/isodoc/function/table.rb', line 81

def table_attrs_style(node, klass)
  width = node["width"] ? "width:#{node['width']};" : nil
  bordered = bordered_table_style(node, klass)
  style = node["style"] ? "" : "#{bordered}#{width}"
  style += keep_style(node) || ""
  style.empty? and style = nil
  style
end

#table_bordered?(node) ⇒ Boolean

Cell bordering follows %plain only, not the custom @class, with rouge-line-table excluded like %plain (see bordered_table_style). metanorma/metanorma-pdfa#33

Returns:

  • (Boolean)


169
170
171
172
173
174
# File 'lib/isodoc/function/table.rb', line 169

def table_bordered?(node)
  table = node.parent.parent
  table["plain"] == "true" and return false
  %w(rouge-line-table dl).include?(table["class"]) and return false
  true
end

#table_get_or_make_tfoot(table) ⇒ Object



196
197
198
199
200
201
202
203
204
205
# File 'lib/isodoc/function/table.rb', line 196

def table_get_or_make_tfoot(table)
  tfoot = table.at(".//tfoot")
  if tfoot.nil?
    table.add_child("<tfoot></tfoot>")
    tfoot = table.at(".//tfoot")
  else
    tfoot.xpath(".//td | .//th").each { |td| remove_bottom_border(td) }
  end
  tfoot
end

#table_html_class(node, custom) ⇒ Object

metanorma/metanorma-pdfa#33: a custom @class is ADDITIVE -- it augments the computed base class (MsoISOTable, or "plain" under %plain) rather than replacing it, and is orthogonal to bordering. modspec, the sourcecode line-numbering rouge-line-table, and dl (a definition list rendered as a table) stay grandfathered internal values with replace semantics (mutually exclusive with MsoISOTable).



75
76
77
78
79
# File 'lib/isodoc/function/table.rb', line 75

def table_html_class(node, custom)
  node["plain"] == "true" and return ["plain", custom].compact.join(" ")
  %w(modspec rouge-line-table dl).include?(custom) and return custom
  ["MsoISOTable", custom].compact.join(" ")
end

#table_parse(node, out) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/isodoc/function/table.rb', line 99

def table_parse(node, out)
  @in_table = true
  out.table(**table_attrs(node)) do |t|
    table_title_parse(node, t)
    table_parse_core(node, t)
    table_parse_tail(node, t)
  end
  @in_table = false
end

#table_parse_core(node, out) ⇒ Object



131
132
133
134
135
136
# File 'lib/isodoc/function/table.rb', line 131

def table_parse_core(node, out)
  colgroup(node, out)
  thead_parse(node, out)
  tbody_parse(node, out)
  tfoot_parse(node, out)
end

#table_parse_tail(node, out) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/isodoc/function/table.rb', line 114

def table_parse_tail(node, out)
  table_parse_tail?(node) or return
  tfoot = table_get_or_make_tfoot(out.parent)
  ins = new_fullcolspan_row(out.parent, tfoot)
  b = Nokogiri::XML::Builder.with(ins)
  table_parse_tail_elems.each do |k|
    node.xpath(ns(k)).each { |n| parse(n, b) }
  end
end

#table_parse_tail?(node) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
# File 'lib/isodoc/function/table.rb', line 124

def table_parse_tail?(node)
  table_parse_tail_elems.each do |k|
    !node.xpath(ns(k)).empty? and return true
  end
  false
end

#table_parse_tail_elemsObject



109
110
111
112
# File 'lib/isodoc/function/table.rb', line 109

def table_parse_tail_elems
  ["./key", "./fmt-source", "./note",
   "./fmt-footnote-container/fmt-fn-body"]
end

#table_title_parse(node, out) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/isodoc/function/table.rb', line 4

def table_title_parse(node, out)
  name = node.at(ns("./fmt-name"))
  summ =  node["summary"]
  name || summ or return
  # out.p class: "TableTitle", style: "text-align:center;" do |p|
  out.caption do |p|
    children_parse(name, p)
    summ and p.span style: "display:none" do |s|
      s << summ
    end
  end
end

#tbody_parse(node, table) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/isodoc/function/table.rb', line 28

def tbody_parse(node, table)
  tbody = node.at(ns("./tbody")) or return
  rowcount = tbody.xpath(ns("./tr")).size
  table.tbody do |h|
    tbody.element_children.each_with_index do |n, i|
      tr_parse(n, h, i, rowcount, false)
    end
  end
end

#tfoot_parse(node, table) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/isodoc/function/table.rb', line 38

def tfoot_parse(node, table)
  tfoot = node.at(ns("./tfoot"))
  if tfoot
    table.tfoot do |h|
      tfoot.element_children.each_with_index do |n, i|
        tr_parse(n, h, i, tfoot.xpath(ns("./tr")).size, false)
      end
    end
  end
end

#thead_parse(node, table) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/isodoc/function/table.rb', line 17

def thead_parse(node, table)
  thead = node.at(ns("./thead"))
  if thead
    table.thead do |h|
      thead.element_children.each_with_index do |n, i|
        tr_parse(n, h, i, thead.xpath(ns("./tr")).size, true)
      end
    end
  end
end

#tr_parse(node, out, ord, totalrows, header) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/isodoc/function/table.rb', line 176

def tr_parse(node, out, ord, totalrows, header)
  out.tr(**attr_code(style: node["style"])) do |r|
    node.elements.each do |td|
      attrs = make_tr_attr(td, ord, totalrows - 1, header,
                           table_bordered?(node))
      r.send td.name, **attr_code(attrs) do |entry|
        td.children.each { |n| parse(n, entry) }
      end
    end
  end
end