Class: Paru::PandocFilter::Table

Inherits:
Block
  • Object
show all
Defined in:
lib/paru/filter/table.rb

Overview

A Table node represents a table with an inline caption, column definition, widths, headers, and rows.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents) ⇒ Table

Create a new Table based on the contents

Parameters:

  • contents (Array)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/paru/filter/table.rb', line 56

def initialize(contents)
  @attr = Attr.new contents[0]
  @caption = Caption.new contents[1]
  @colspec = contents[2].map { |p| ColSpec.new p }
  @head = TableHead.new contents[3]
  super([])
  contents[4].each do |table_body|
    @children.push TableBody.new table_body
  end
  @foot = TableFoot.new contents[5]
end

Instance Attribute Details

#attrObject

Returns Attr.

Returns:

  • Attr



50
51
52
53
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
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
# File 'lib/paru/filter/table.rb', line 50

class Table < Block
  attr_accessor :caption, :attr, :colspec, :head, :foot

  # Create a new Table based on the contents
  #
  # @param contents [Array]
  def initialize(contents)
    @attr = Attr.new contents[0]
    @caption = Caption.new contents[1]
    @colspec = contents[2].map { |p| ColSpec.new p }
    @head = TableHead.new contents[3]
    super([])
    contents[4].each do |table_body|
      @children.push TableBody.new table_body
    end
    @foot = TableFoot.new contents[5]
  end

  # The AST contents of this Table node
  #
  # @return [Array]
  def ast_contents
    [
      @attr.to_ast,
      @caption.to_ast,
      @colspec.map(&:to_ast),
      @head.to_ast,
      @children.map(&:to_ast),
      @foot.to_ast
    ]
  end

  # Convert this table to a 2D table of markdown strings for each
  # cell
  #
  # @param config [Hash] configuraton of the table output
  #   Config can contain properties :headers
  #
  # @return [String[][]] This Table as a 2D array of cells
  # represented by their markdown strings.
  def to_array(config = {})
    headers = config.key?(:headers) ? config[:headers] : false
    footers = config.key?(:footers) ? config[:footers] : false

    data = []
    data.concat @head.to_array if headers

    @children.each do |row|
      data.concat row.to_array
    end

    data.concat @foot.to_array if footers

    data
  end

  # Convert this Table to a CSV file. See to_array for the config
  # options
  #
  # @param filename [String] filename to write to
  # @param config [Hash] See #to_array for config options
  def to_file(filename, config = {})
    CSV.open(filename, 'wb') do |csv|
      to_array(**config).each { |row| csv << row }
    end
  end

  # Create a new Table from an 2D array and an optional
  # configuration
  #
  # @param data [String[][]] an array of markdown strings
  # @param config [Hash] configuration of the list.
  #   properties:
  #     :headers [Boolean] True if data includes headers on first
  #     row. Defailts to false.
  #     :caption [String] The table's caption
  #     :footers [Boolean] True if data includes footers on last row,
  #     default to false.
  #
  # @return [Table]
  def self.from_array(data, config = {})
    table_attribute = create_attr

    caption = []
    caption = create_caption config[:caption] if config.key? :caption

    col_spec = data[0].map { |_c| ColSpec.new.to_ast }

    head = create_endrow []
    if config.key?(:headers) && config[:headers]
      head = create_endrow data.first
      data = data[1..]
    end

    foot = create_endrow []
    if config.key?(:footers) && config[:footers]
      foot = create_endrow data.last
      data = data[0...-1]
    end

    body = create_body data

    table = [
      table_attribute,
      caption,
      col_spec,
      head,
      body,
      foot
    ]

    Table.new table
  end

  # Create a new Table from a CSV file.
  #
  # @param filename [String] filename to read CSV data from
  # @param config [Hash] See #from_file for details
  #
  # @return [Table]
  def self.from_file(filename, config = {})
    data = []
    CSV.foreach(filename) do |row|
      data << row
    end

    from_array(data, config)
  end

  def self.create_caption(contents)
    [
      nil,
      [Node.from_markdown(contents).to_ast]
    ]
  end

  def self.create_body(data)
    [[
      create_attr,
      0,
      [],
      data.map { |r| create_row(r) }
    ]]
  end

  def self.create_endrow(data)
    [
      create_attr,
      data.empty? ? [] : [create_row(data)]
    ]
  end

  def self.create_row(data)
    [
      create_attr,
      data.map { |c| create_cell(c) }
    ]
  end

  def self.create_cell(contents)
    [
      create_attr,
      { 't' => 'AlignDefault', 'c' => nil },
      1,
      1,
      [Node.from_markdown(contents).to_ast]
    ]
  end

  def self.create_attr
    ['', [], []]
  end
end

#captionObject

Returns Caption.

Returns:

  • Caption



50
51
52
53
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
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
# File 'lib/paru/filter/table.rb', line 50

class Table < Block
  attr_accessor :caption, :attr, :colspec, :head, :foot

  # Create a new Table based on the contents
  #
  # @param contents [Array]
  def initialize(contents)
    @attr = Attr.new contents[0]
    @caption = Caption.new contents[1]
    @colspec = contents[2].map { |p| ColSpec.new p }
    @head = TableHead.new contents[3]
    super([])
    contents[4].each do |table_body|
      @children.push TableBody.new table_body
    end
    @foot = TableFoot.new contents[5]
  end

  # The AST contents of this Table node
  #
  # @return [Array]
  def ast_contents
    [
      @attr.to_ast,
      @caption.to_ast,
      @colspec.map(&:to_ast),
      @head.to_ast,
      @children.map(&:to_ast),
      @foot.to_ast
    ]
  end

  # Convert this table to a 2D table of markdown strings for each
  # cell
  #
  # @param config [Hash] configuraton of the table output
  #   Config can contain properties :headers
  #
  # @return [String[][]] This Table as a 2D array of cells
  # represented by their markdown strings.
  def to_array(config = {})
    headers = config.key?(:headers) ? config[:headers] : false
    footers = config.key?(:footers) ? config[:footers] : false

    data = []
    data.concat @head.to_array if headers

    @children.each do |row|
      data.concat row.to_array
    end

    data.concat @foot.to_array if footers

    data
  end

  # Convert this Table to a CSV file. See to_array for the config
  # options
  #
  # @param filename [String] filename to write to
  # @param config [Hash] See #to_array for config options
  def to_file(filename, config = {})
    CSV.open(filename, 'wb') do |csv|
      to_array(**config).each { |row| csv << row }
    end
  end

  # Create a new Table from an 2D array and an optional
  # configuration
  #
  # @param data [String[][]] an array of markdown strings
  # @param config [Hash] configuration of the list.
  #   properties:
  #     :headers [Boolean] True if data includes headers on first
  #     row. Defailts to false.
  #     :caption [String] The table's caption
  #     :footers [Boolean] True if data includes footers on last row,
  #     default to false.
  #
  # @return [Table]
  def self.from_array(data, config = {})
    table_attribute = create_attr

    caption = []
    caption = create_caption config[:caption] if config.key? :caption

    col_spec = data[0].map { |_c| ColSpec.new.to_ast }

    head = create_endrow []
    if config.key?(:headers) && config[:headers]
      head = create_endrow data.first
      data = data[1..]
    end

    foot = create_endrow []
    if config.key?(:footers) && config[:footers]
      foot = create_endrow data.last
      data = data[0...-1]
    end

    body = create_body data

    table = [
      table_attribute,
      caption,
      col_spec,
      head,
      body,
      foot
    ]

    Table.new table
  end

  # Create a new Table from a CSV file.
  #
  # @param filename [String] filename to read CSV data from
  # @param config [Hash] See #from_file for details
  #
  # @return [Table]
  def self.from_file(filename, config = {})
    data = []
    CSV.foreach(filename) do |row|
      data << row
    end

    from_array(data, config)
  end

  def self.create_caption(contents)
    [
      nil,
      [Node.from_markdown(contents).to_ast]
    ]
  end

  def self.create_body(data)
    [[
      create_attr,
      0,
      [],
      data.map { |r| create_row(r) }
    ]]
  end

  def self.create_endrow(data)
    [
      create_attr,
      data.empty? ? [] : [create_row(data)]
    ]
  end

  def self.create_row(data)
    [
      create_attr,
      data.map { |c| create_cell(c) }
    ]
  end

  def self.create_cell(contents)
    [
      create_attr,
      { 't' => 'AlignDefault', 'c' => nil },
      1,
      1,
      [Node.from_markdown(contents).to_ast]
    ]
  end

  def self.create_attr
    ['', [], []]
  end
end

#colspecObject

Returns ColSpec[].

Returns:

  • ColSpec[]



50
51
52
53
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
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
# File 'lib/paru/filter/table.rb', line 50

class Table < Block
  attr_accessor :caption, :attr, :colspec, :head, :foot

  # Create a new Table based on the contents
  #
  # @param contents [Array]
  def initialize(contents)
    @attr = Attr.new contents[0]
    @caption = Caption.new contents[1]
    @colspec = contents[2].map { |p| ColSpec.new p }
    @head = TableHead.new contents[3]
    super([])
    contents[4].each do |table_body|
      @children.push TableBody.new table_body
    end
    @foot = TableFoot.new contents[5]
  end

  # The AST contents of this Table node
  #
  # @return [Array]
  def ast_contents
    [
      @attr.to_ast,
      @caption.to_ast,
      @colspec.map(&:to_ast),
      @head.to_ast,
      @children.map(&:to_ast),
      @foot.to_ast
    ]
  end

  # Convert this table to a 2D table of markdown strings for each
  # cell
  #
  # @param config [Hash] configuraton of the table output
  #   Config can contain properties :headers
  #
  # @return [String[][]] This Table as a 2D array of cells
  # represented by their markdown strings.
  def to_array(config = {})
    headers = config.key?(:headers) ? config[:headers] : false
    footers = config.key?(:footers) ? config[:footers] : false

    data = []
    data.concat @head.to_array if headers

    @children.each do |row|
      data.concat row.to_array
    end

    data.concat @foot.to_array if footers

    data
  end

  # Convert this Table to a CSV file. See to_array for the config
  # options
  #
  # @param filename [String] filename to write to
  # @param config [Hash] See #to_array for config options
  def to_file(filename, config = {})
    CSV.open(filename, 'wb') do |csv|
      to_array(**config).each { |row| csv << row }
    end
  end

  # Create a new Table from an 2D array and an optional
  # configuration
  #
  # @param data [String[][]] an array of markdown strings
  # @param config [Hash] configuration of the list.
  #   properties:
  #     :headers [Boolean] True if data includes headers on first
  #     row. Defailts to false.
  #     :caption [String] The table's caption
  #     :footers [Boolean] True if data includes footers on last row,
  #     default to false.
  #
  # @return [Table]
  def self.from_array(data, config = {})
    table_attribute = create_attr

    caption = []
    caption = create_caption config[:caption] if config.key? :caption

    col_spec = data[0].map { |_c| ColSpec.new.to_ast }

    head = create_endrow []
    if config.key?(:headers) && config[:headers]
      head = create_endrow data.first
      data = data[1..]
    end

    foot = create_endrow []
    if config.key?(:footers) && config[:footers]
      foot = create_endrow data.last
      data = data[0...-1]
    end

    body = create_body data

    table = [
      table_attribute,
      caption,
      col_spec,
      head,
      body,
      foot
    ]

    Table.new table
  end

  # Create a new Table from a CSV file.
  #
  # @param filename [String] filename to read CSV data from
  # @param config [Hash] See #from_file for details
  #
  # @return [Table]
  def self.from_file(filename, config = {})
    data = []
    CSV.foreach(filename) do |row|
      data << row
    end

    from_array(data, config)
  end

  def self.create_caption(contents)
    [
      nil,
      [Node.from_markdown(contents).to_ast]
    ]
  end

  def self.create_body(data)
    [[
      create_attr,
      0,
      [],
      data.map { |r| create_row(r) }
    ]]
  end

  def self.create_endrow(data)
    [
      create_attr,
      data.empty? ? [] : [create_row(data)]
    ]
  end

  def self.create_row(data)
    [
      create_attr,
      data.map { |c| create_cell(c) }
    ]
  end

  def self.create_cell(contents)
    [
      create_attr,
      { 't' => 'AlignDefault', 'c' => nil },
      1,
      1,
      [Node.from_markdown(contents).to_ast]
    ]
  end

  def self.create_attr
    ['', [], []]
  end
end

#footObject

Returns TableHead[].

Returns:

  • TableHead[]



50
51
52
53
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
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
# File 'lib/paru/filter/table.rb', line 50

class Table < Block
  attr_accessor :caption, :attr, :colspec, :head, :foot

  # Create a new Table based on the contents
  #
  # @param contents [Array]
  def initialize(contents)
    @attr = Attr.new contents[0]
    @caption = Caption.new contents[1]
    @colspec = contents[2].map { |p| ColSpec.new p }
    @head = TableHead.new contents[3]
    super([])
    contents[4].each do |table_body|
      @children.push TableBody.new table_body
    end
    @foot = TableFoot.new contents[5]
  end

  # The AST contents of this Table node
  #
  # @return [Array]
  def ast_contents
    [
      @attr.to_ast,
      @caption.to_ast,
      @colspec.map(&:to_ast),
      @head.to_ast,
      @children.map(&:to_ast),
      @foot.to_ast
    ]
  end

  # Convert this table to a 2D table of markdown strings for each
  # cell
  #
  # @param config [Hash] configuraton of the table output
  #   Config can contain properties :headers
  #
  # @return [String[][]] This Table as a 2D array of cells
  # represented by their markdown strings.
  def to_array(config = {})
    headers = config.key?(:headers) ? config[:headers] : false
    footers = config.key?(:footers) ? config[:footers] : false

    data = []
    data.concat @head.to_array if headers

    @children.each do |row|
      data.concat row.to_array
    end

    data.concat @foot.to_array if footers

    data
  end

  # Convert this Table to a CSV file. See to_array for the config
  # options
  #
  # @param filename [String] filename to write to
  # @param config [Hash] See #to_array for config options
  def to_file(filename, config = {})
    CSV.open(filename, 'wb') do |csv|
      to_array(**config).each { |row| csv << row }
    end
  end

  # Create a new Table from an 2D array and an optional
  # configuration
  #
  # @param data [String[][]] an array of markdown strings
  # @param config [Hash] configuration of the list.
  #   properties:
  #     :headers [Boolean] True if data includes headers on first
  #     row. Defailts to false.
  #     :caption [String] The table's caption
  #     :footers [Boolean] True if data includes footers on last row,
  #     default to false.
  #
  # @return [Table]
  def self.from_array(data, config = {})
    table_attribute = create_attr

    caption = []
    caption = create_caption config[:caption] if config.key? :caption

    col_spec = data[0].map { |_c| ColSpec.new.to_ast }

    head = create_endrow []
    if config.key?(:headers) && config[:headers]
      head = create_endrow data.first
      data = data[1..]
    end

    foot = create_endrow []
    if config.key?(:footers) && config[:footers]
      foot = create_endrow data.last
      data = data[0...-1]
    end

    body = create_body data

    table = [
      table_attribute,
      caption,
      col_spec,
      head,
      body,
      foot
    ]

    Table.new table
  end

  # Create a new Table from a CSV file.
  #
  # @param filename [String] filename to read CSV data from
  # @param config [Hash] See #from_file for details
  #
  # @return [Table]
  def self.from_file(filename, config = {})
    data = []
    CSV.foreach(filename) do |row|
      data << row
    end

    from_array(data, config)
  end

  def self.create_caption(contents)
    [
      nil,
      [Node.from_markdown(contents).to_ast]
    ]
  end

  def self.create_body(data)
    [[
      create_attr,
      0,
      [],
      data.map { |r| create_row(r) }
    ]]
  end

  def self.create_endrow(data)
    [
      create_attr,
      data.empty? ? [] : [create_row(data)]
    ]
  end

  def self.create_row(data)
    [
      create_attr,
      data.map { |c| create_cell(c) }
    ]
  end

  def self.create_cell(contents)
    [
      create_attr,
      { 't' => 'AlignDefault', 'c' => nil },
      1,
      1,
      [Node.from_markdown(contents).to_ast]
    ]
  end

  def self.create_attr
    ['', [], []]
  end
end

#headObject

Returns TableHead[].

Returns:

  • TableHead[]



50
51
52
53
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
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
# File 'lib/paru/filter/table.rb', line 50

class Table < Block
  attr_accessor :caption, :attr, :colspec, :head, :foot

  # Create a new Table based on the contents
  #
  # @param contents [Array]
  def initialize(contents)
    @attr = Attr.new contents[0]
    @caption = Caption.new contents[1]
    @colspec = contents[2].map { |p| ColSpec.new p }
    @head = TableHead.new contents[3]
    super([])
    contents[4].each do |table_body|
      @children.push TableBody.new table_body
    end
    @foot = TableFoot.new contents[5]
  end

  # The AST contents of this Table node
  #
  # @return [Array]
  def ast_contents
    [
      @attr.to_ast,
      @caption.to_ast,
      @colspec.map(&:to_ast),
      @head.to_ast,
      @children.map(&:to_ast),
      @foot.to_ast
    ]
  end

  # Convert this table to a 2D table of markdown strings for each
  # cell
  #
  # @param config [Hash] configuraton of the table output
  #   Config can contain properties :headers
  #
  # @return [String[][]] This Table as a 2D array of cells
  # represented by their markdown strings.
  def to_array(config = {})
    headers = config.key?(:headers) ? config[:headers] : false
    footers = config.key?(:footers) ? config[:footers] : false

    data = []
    data.concat @head.to_array if headers

    @children.each do |row|
      data.concat row.to_array
    end

    data.concat @foot.to_array if footers

    data
  end

  # Convert this Table to a CSV file. See to_array for the config
  # options
  #
  # @param filename [String] filename to write to
  # @param config [Hash] See #to_array for config options
  def to_file(filename, config = {})
    CSV.open(filename, 'wb') do |csv|
      to_array(**config).each { |row| csv << row }
    end
  end

  # Create a new Table from an 2D array and an optional
  # configuration
  #
  # @param data [String[][]] an array of markdown strings
  # @param config [Hash] configuration of the list.
  #   properties:
  #     :headers [Boolean] True if data includes headers on first
  #     row. Defailts to false.
  #     :caption [String] The table's caption
  #     :footers [Boolean] True if data includes footers on last row,
  #     default to false.
  #
  # @return [Table]
  def self.from_array(data, config = {})
    table_attribute = create_attr

    caption = []
    caption = create_caption config[:caption] if config.key? :caption

    col_spec = data[0].map { |_c| ColSpec.new.to_ast }

    head = create_endrow []
    if config.key?(:headers) && config[:headers]
      head = create_endrow data.first
      data = data[1..]
    end

    foot = create_endrow []
    if config.key?(:footers) && config[:footers]
      foot = create_endrow data.last
      data = data[0...-1]
    end

    body = create_body data

    table = [
      table_attribute,
      caption,
      col_spec,
      head,
      body,
      foot
    ]

    Table.new table
  end

  # Create a new Table from a CSV file.
  #
  # @param filename [String] filename to read CSV data from
  # @param config [Hash] See #from_file for details
  #
  # @return [Table]
  def self.from_file(filename, config = {})
    data = []
    CSV.foreach(filename) do |row|
      data << row
    end

    from_array(data, config)
  end

  def self.create_caption(contents)
    [
      nil,
      [Node.from_markdown(contents).to_ast]
    ]
  end

  def self.create_body(data)
    [[
      create_attr,
      0,
      [],
      data.map { |r| create_row(r) }
    ]]
  end

  def self.create_endrow(data)
    [
      create_attr,
      data.empty? ? [] : [create_row(data)]
    ]
  end

  def self.create_row(data)
    [
      create_attr,
      data.map { |c| create_cell(c) }
    ]
  end

  def self.create_cell(contents)
    [
      create_attr,
      { 't' => 'AlignDefault', 'c' => nil },
      1,
      1,
      [Node.from_markdown(contents).to_ast]
    ]
  end

  def self.create_attr
    ['', [], []]
  end
end

Class Method Details

.create_attrObject



219
220
221
# File 'lib/paru/filter/table.rb', line 219

def self.create_attr
  ['', [], []]
end

.create_body(data) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/paru/filter/table.rb', line 186

def self.create_body(data)
  [[
    create_attr,
    0,
    [],
    data.map { |r| create_row(r) }
  ]]
end

.create_caption(contents) ⇒ Object



179
180
181
182
183
184
# File 'lib/paru/filter/table.rb', line 179

def self.create_caption(contents)
  [
    nil,
    [Node.from_markdown(contents).to_ast]
  ]
end

.create_cell(contents) ⇒ Object



209
210
211
212
213
214
215
216
217
# File 'lib/paru/filter/table.rb', line 209

def self.create_cell(contents)
  [
    create_attr,
    { 't' => 'AlignDefault', 'c' => nil },
    1,
    1,
    [Node.from_markdown(contents).to_ast]
  ]
end

.create_endrow(data) ⇒ Object



195
196
197
198
199
200
# File 'lib/paru/filter/table.rb', line 195

def self.create_endrow(data)
  [
    create_attr,
    data.empty? ? [] : [create_row(data)]
  ]
end

.create_row(data) ⇒ Object



202
203
204
205
206
207
# File 'lib/paru/filter/table.rb', line 202

def self.create_row(data)
  [
    create_attr,
    data.map { |c| create_cell(c) }
  ]
end

.from_array(data, config = {}) ⇒ Table

Create a new Table from an 2D array and an optional configuration

Parameters:

  • data (String[][])

    an array of markdown strings

  • config (Hash) (defaults to: {})

    configuration of the list. properties:

    :headers [Boolean] True if data includes headers on first
    row. Defailts to false.
    :caption [String] The table's caption
    :footers [Boolean] True if data includes footers on last row,
    default to false.
    

Returns:



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
# File 'lib/paru/filter/table.rb', line 130

def self.from_array(data, config = {})
  table_attribute = create_attr

  caption = []
  caption = create_caption config[:caption] if config.key? :caption

  col_spec = data[0].map { |_c| ColSpec.new.to_ast }

  head = create_endrow []
  if config.key?(:headers) && config[:headers]
    head = create_endrow data.first
    data = data[1..]
  end

  foot = create_endrow []
  if config.key?(:footers) && config[:footers]
    foot = create_endrow data.last
    data = data[0...-1]
  end

  body = create_body data

  table = [
    table_attribute,
    caption,
    col_spec,
    head,
    body,
    foot
  ]

  Table.new table
end

.from_file(filename, config = {}) ⇒ Table

Create a new Table from a CSV file.

Parameters:

  • filename (String)

    filename to read CSV data from

  • config (Hash) (defaults to: {})

    See #from_file for details

Returns:



170
171
172
173
174
175
176
177
# File 'lib/paru/filter/table.rb', line 170

def self.from_file(filename, config = {})
  data = []
  CSV.foreach(filename) do |row|
    data << row
  end

  from_array(data, config)
end

Instance Method Details

#ast_contentsArray

The AST contents of this Table node

Returns:

  • (Array)


71
72
73
74
75
76
77
78
79
80
# File 'lib/paru/filter/table.rb', line 71

def ast_contents
  [
    @attr.to_ast,
    @caption.to_ast,
    @colspec.map(&:to_ast),
    @head.to_ast,
    @children.map(&:to_ast),
    @foot.to_ast
  ]
end

#to_array(config = {}) ⇒ String[][]

Convert this table to a 2D table of markdown strings for each cell

represented by their markdown strings.

Parameters:

  • config (Hash) (defaults to: {})

    configuraton of the table output Config can contain properties :headers

Returns:

  • (String[][])

    This Table as a 2D array of cells



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/paru/filter/table.rb', line 90

def to_array(config = {})
  headers = config.key?(:headers) ? config[:headers] : false
  footers = config.key?(:footers) ? config[:footers] : false

  data = []
  data.concat @head.to_array if headers

  @children.each do |row|
    data.concat row.to_array
  end

  data.concat @foot.to_array if footers

  data
end

#to_file(filename, config = {}) ⇒ Object

Convert this Table to a CSV file. See to_array for the config options

Parameters:

  • filename (String)

    filename to write to

  • config (Hash) (defaults to: {})

    See #to_array for config options



111
112
113
114
115
# File 'lib/paru/filter/table.rb', line 111

def to_file(filename, config = {})
  CSV.open(filename, 'wb') do |csv|
    to_array(**config).each { |row| csv << row }
  end
end