Skip to content

Commit

Permalink
Merge pull request urvin-compliance#104 from AliceVerner:repeat_headers
Browse files Browse the repository at this point in the history
Add repeat table headers

# Conflicts:
#	lib/caracal/core/models/table_model.rb
#	lib/caracal/renderers/document_renderer.rb
  • Loading branch information
goulvench committed Mar 18, 2020
2 parents 3f81d23 + 29d9f2d commit c76b895
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
18 changes: 16 additions & 2 deletions lib/caracal/core/models/table_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class TableModel < BaseModel
const_set(:DEFAULT_TABLE_BORDER_COLOR, 'auto')
const_set(:DEFAULT_TABLE_BORDER_LINE, :single)
const_set(:DEFAULT_TABLE_BORDER_SIZE, 0) # units in 1/8 points
const_set(:DEFAULT_TABLE_BORDER_SPACING, 0)
const_set(:DEFAULT_TABLE_BORDER_SPACING, 0)
const_set(:DEFAULT_TABLE_REPEAT_HEADER, false)
const_set(:DEFAULT_TABLE_HEADER_ROWS, 1)

# accessors
attr_reader :table_align
Expand All @@ -37,6 +39,8 @@ class TableModel < BaseModel
attr_reader :table_border_horizontal # returns border model
attr_reader :table_border_vertical # returns border model
attr_reader :table_column_widths
attr_reader :table_repeat_header
attr_reader :table_header_rows

# initialization
def initialize(options={}, &block)
Expand All @@ -45,6 +49,8 @@ def initialize(options={}, &block)
@table_border_line = DEFAULT_TABLE_BORDER_LINE
@table_border_size = DEFAULT_TABLE_BORDER_SIZE
@table_border_spacing = DEFAULT_TABLE_BORDER_SPACING
@table_repeat_header = DEFAULT_TABLE_REPEAT_HEADER
@table_header_rows = DEFAULT_TABLE_HEADER_ROWS

super options, &block
end
Expand Down Expand Up @@ -120,9 +126,16 @@ def cell_style(models, options={})


#=============== SETTERS ==============================

# booleans
[:repeat_header].each do |m|
define_method "#{ m }" do |value|
instance_variable_set("@table_#{ m }", !!value)
end
end

# integers
[:border_size, :border_spacing, :width].each do |m|
[:border_size, :border_spacing, :width, :header_rows].each do |m|
define_method "#{ m }" do |value|
instance_variable_set("@table_#{ m }", value.to_i)
end
Expand Down Expand Up @@ -203,6 +216,7 @@ def option_keys
k << [:border_color, :border_line, :border_size, :border_spacing]
k << [:border_bottom, :border_left, :border_right, :border_top, :border_horizontal, :border_vertical]
k << [:column_widths]
k << [:repeat_header, :header_rows]
k.flatten
end

Expand Down
10 changes: 9 additions & 1 deletion lib/caracal/renderers/document_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,18 @@ def render_table(xml, model)
end

rowspan_hash = {}
model.rows.each do |row|
model.rows.each_with_index do |row, index|
xml['w'].tr do
tc_index = 0
row.each do |tc|
if model.table_repeat_header
if index < model.table_header_rows
xml['w'].trPr do
xml['w'].tblHeader
end
end
end
row.each_with_index do |tc, tc_index|
xml['w'].tc do
xml['w'].tcPr do
xml['w'].shd({ 'w:fill' => tc.cell_background })
Expand Down

0 comments on commit c76b895

Please sign in to comment.