Class: PDFConverterTableBreak

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor-pdf-table-break.rb

Instance Method Summary collapse

Instance Method Details

#convert_table(node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/asciidoctor-pdf-table-break.rb', line 54

def convert_table node
  body_rows = node.rows[:body]

  # Save node state before any early return so the ensure block always has
  # valid references. In a prepend approach, returning early before these
  # assignments leaves them nil, and the ensure block would then corrupt the
  # node by setting body/foot to nil.
  orig_body  = node.rows[:body]
  orig_foot  = node.rows[:foot]
  orig_id    = node.id
  orig_title = node.title if node.title?
  orig_attrs = node.attributes.dup

  # Detect break rows: every cell must have source text '<<<'.
  # Covers both N+| <<< (single spanning cell) and | <<< | <<< | ... (one per column).
  break_idxs = body_rows.each_index.select do |i|
    body_rows[i].all? { |c| c.source.strip == '<<<' }
  end
  return super if break_idxs.empty?

  # Build one range per segment between break rows.
  ranges = []
  prev = 0
  break_idxs.each { |i| ranges << (prev...i); prev = i + 1 }
  ranges << (prev...body_rows.size)
  last_idx = ranges.size - 1

  # Compute the grid-line width used at break edges so borders are seamless.
  frame_widths = expand_rect_values @theme.table_border_width, 0  # [top, right, bottom, left]
  grid_raw     = @theme.table_grid_width || [frame_widths[0], frame_widths[3]]
  grid_rows_w  = (expand_grid_values grid_raw, 0)[0]
  orig_tbl_bw  = @theme.table_border_width

  ranges.each_with_index do |range, seg_idx|
    is_first = seg_idx == 0
    is_last  = seg_idx == last_idx

    advance_page unless is_first

    node.rows.body = orig_body[range]
    node.rows.foot = is_last ? orig_foot : []
    node.instance_variable_set :@attributes, orig_attrs.dup
    node.set_attr 'rowcount', node.rows[:head].size + node.rows[:body].size + node.rows[:foot].size
    node.remove_attr 'unbreakable-option'
    node.remove_attr 'breakable-option'
    node.instance_variable_set :@id,    nil unless is_first
    node.instance_variable_set :@title, nil unless is_first

    # Patch frame border widths so each segment gets the correct edge:
    #   first segment:   full frame top,  grid bottom
    #   middle segments: grid top,         grid bottom
    #   last segment:    grid top,         full frame bottom
    unless is_first && is_last
      patched = frame_widths.dup
      patched[0] = grid_rows_w unless is_first
      patched[2] = grid_rows_w unless is_last
      @theme.table_border_width = patched
    end

    begin
      super
    ensure
      @theme.table_border_width = orig_tbl_bw
    end
  end

ensure
  node.rows.body = orig_body
  node.rows.foot = orig_foot
  node.instance_variable_set :@id, orig_id
  node.instance_variable_set :@title, orig_title if defined?(orig_title) && orig_title
  node.instance_variable_set :@attributes, orig_attrs if orig_attrs
end