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)


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

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



49
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
223
224
225
226
227
228
229
230
231
# File 'lib/paru/filter/table.rb', line 49

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 {|c| c.to_ast},
            @head.to_ast,
            @children.map {|c| c.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 = if config.has_key? :headers then config[:headers] else false end
        footers = if config.has_key? :footers then config[:footers] else false end

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

        @children.each do |row|
            data.concat row.to_array
        end
        
        if footers then
            data.concat @foot.to_array
        end

        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 = []
        if config.has_key? :caption
          caption = create_caption config[:caption]
        end

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

        head = create_endrow []
        if config.has_key? :headers and config[:headers]
          head = create_endrow data.first
          data = data[1..-1]
        end

        foot = create_endrow []
        if config.has_key? :footers and 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

        return self.from_array(data, config) 
    end

    private

    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,
        if data.empty? then [] else [create_row(data)] end
      ]
    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



49
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
223
224
225
226
227
228
229
230
231
# File 'lib/paru/filter/table.rb', line 49

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 {|c| c.to_ast},
            @head.to_ast,
            @children.map {|c| c.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 = if config.has_key? :headers then config[:headers] else false end
        footers = if config.has_key? :footers then config[:footers] else false end

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

        @children.each do |row|
            data.concat row.to_array
        end
        
        if footers then
            data.concat @foot.to_array
        end

        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 = []
        if config.has_key? :caption
          caption = create_caption config[:caption]
        end

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

        head = create_endrow []
        if config.has_key? :headers and config[:headers]
          head = create_endrow data.first
          data = data[1..-1]
        end

        foot = create_endrow []
        if config.has_key? :footers and 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

        return self.from_array(data, config) 
    end

    private

    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,
        if data.empty? then [] else [create_row(data)] end
      ]
    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[]



49
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
223
224
225
226
227
228
229
230
231
# File 'lib/paru/filter/table.rb', line 49

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 {|c| c.to_ast},
            @head.to_ast,
            @children.map {|c| c.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 = if config.has_key? :headers then config[:headers] else false end
        footers = if config.has_key? :footers then config[:footers] else false end

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

        @children.each do |row|
            data.concat row.to_array
        end
        
        if footers then
            data.concat @foot.to_array
        end

        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 = []
        if config.has_key? :caption
          caption = create_caption config[:caption]
        end

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

        head = create_endrow []
        if config.has_key? :headers and config[:headers]
          head = create_endrow data.first
          data = data[1..-1]
        end

        foot = create_endrow []
        if config.has_key? :footers and 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

        return self.from_array(data, config) 
    end

    private

    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,
        if data.empty? then [] else [create_row(data)] end
      ]
    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[]



49
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
223
224
225
226
227
228
229
230
231
# File 'lib/paru/filter/table.rb', line 49

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 {|c| c.to_ast},
            @head.to_ast,
            @children.map {|c| c.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 = if config.has_key? :headers then config[:headers] else false end
        footers = if config.has_key? :footers then config[:footers] else false end

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

        @children.each do |row|
            data.concat row.to_array
        end
        
        if footers then
            data.concat @foot.to_array
        end

        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 = []
        if config.has_key? :caption
          caption = create_caption config[:caption]
        end

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

        head = create_endrow []
        if config.has_key? :headers and config[:headers]
          head = create_endrow data.first
          data = data[1..-1]
        end

        foot = create_endrow []
        if config.has_key? :footers and 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

        return self.from_array(data, config) 
    end

    private

    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,
        if data.empty? then [] else [create_row(data)] end
      ]
    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[]



49
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
223
224
225
226
227
228
229
230
231
# File 'lib/paru/filter/table.rb', line 49

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 {|c| c.to_ast},
            @head.to_ast,
            @children.map {|c| c.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 = if config.has_key? :headers then config[:headers] else false end
        footers = if config.has_key? :footers then config[:footers] else false end

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

        @children.each do |row|
            data.concat row.to_array
        end
        
        if footers then
            data.concat @foot.to_array
        end

        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 = []
        if config.has_key? :caption
          caption = create_caption config[:caption]
        end

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

        head = create_endrow []
        if config.has_key? :headers and config[:headers]
          head = create_endrow data.first
          data = data[1..-1]
        end

        foot = create_endrow []
        if config.has_key? :footers and 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

        return self.from_array(data, config) 
    end

    private

    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,
        if data.empty? then [] else [create_row(data)] end
      ]
    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

.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:



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

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

    caption = []
    if config.has_key? :caption
      caption = create_caption config[:caption]
    end

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

    head = create_endrow []
    if config.has_key? :headers and config[:headers]
      head = create_endrow data.first
      data = data[1..-1]
    end

    foot = create_endrow []
    if config.has_key? :footers and 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:



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

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

    return self.from_array(data, config) 
end

Instance Method Details

#ast_contentsArray

The AST contents of this Table node

Returns:

  • (Array)


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

def ast_contents()
    [
        @attr.to_ast,
        @caption.to_ast,
        @colspec.map {|c| c.to_ast},
        @head.to_ast,
        @children.map {|c| c.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



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

def to_array(config = {})
    headers = if config.has_key? :headers then config[:headers] else false end
    footers = if config.has_key? :footers then config[:footers] else false end

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

    @children.each do |row|
        data.concat row.to_array
    end
    
    if footers then
        data.concat @foot.to_array
    end

    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



114
115
116
117
118
# File 'lib/paru/filter/table.rb', line 114

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