Module: Marktable
- Defined in:
- lib/marktable.rb,
lib/marktable/row.rb,
lib/marktable/table.rb
Defined Under Namespace
Class Method Summary collapse
-
.filter(markdown_table, pattern, headers: true) ⇒ Object
Filter rows matching a pattern.
-
.foreach(markdown_table, headers: true) ⇒ Object
Iterate through each row of a markdown table.
-
.generate(headers: nil) ⇒ Object
Generate a markdown table from provided data.
-
.map(markdown_table, headers: true) ⇒ Object
Map over rows (all values will be converted to strings).
-
.parse(markdown_table, headers: true) ⇒ Object
Parse a markdown table string into an array of rows.
-
.parse_line(markdown_row) ⇒ Object
Parse a single markdown row into an array of cell values.
-
.read(path, headers: true) ⇒ Object
Read a markdown table from a file.
-
.table(array, headers: true) ⇒ Object
Convert an array to a Marktable::Table.
-
.write(path, table_or_data) ⇒ Object
Write a markdown table to a file.
Class Method Details
.filter(markdown_table, pattern, headers: true) ⇒ Object
Filter rows matching a pattern
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/marktable.rb', line 109 def self.filter(markdown_table, pattern, headers: true) table = Table.new(markdown_table, headers: headers) filtered_rows = table.to_a.select do |row| if row.is_a?(Hash) row.values.any? { |v| v.to_s.match?(pattern) } else row.any? { |v| v.to_s.match?(pattern) } end end table(filtered_rows, headers: headers) end |
.foreach(markdown_table, headers: true) ⇒ Object
Iterate through each row of a markdown table
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/marktable.rb', line 22 def self.foreach(markdown_table, headers: true) table = Table.new(markdown_table, headers: headers) return Enumerator.new do |yielder| table.each do |row| yielder << row.data end end unless block_given? table.each do |row| yield row.data end end |
.generate(headers: nil) ⇒ Object
Generate a markdown table from provided data
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/marktable.rb', line 36 def self.generate(headers: nil) result = [] markdown_table = '' if block_given? table_data = [] yield table_data unless table_data.empty? # Ensure all data is stringified string_data = table_data.map do |row| if row.is_a?(Hash) row.transform_values(&:to_s) else row.map(&:to_s) end end # Create a Table object table = table(string_data, headers: headers.nil? ? true : headers) markdown_table = table.generate end end markdown_table end |
.map(markdown_table, headers: true) ⇒ Object
Map over rows (all values will be converted to strings)
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/marktable.rb', line 123 def self.map(markdown_table, headers: true) table = Table.new(markdown_table, headers: headers) mapped_rows = [] table.each do |row| result = yield(row) # Ensure result is string-compatible if result.is_a?(Hash) result = result.transform_values(&:to_s) elsif result.is_a?(Array) result = result.map(&:to_s) end mapped_rows << result end table(mapped_rows, headers: headers) end |
.parse(markdown_table, headers: true) ⇒ Object
Parse a markdown table string into an array of rows
12 13 14 |
# File 'lib/marktable.rb', line 12 def self.parse(markdown_table, headers: true) Table.new(markdown_table, headers: headers).to_a end |
.parse_line(markdown_row) ⇒ Object
Parse a single markdown row into an array of cell values
17 18 19 |
# File 'lib/marktable.rb', line 17 def self.parse_line(markdown_row) Row.parse(markdown_row) end |
.read(path, headers: true) ⇒ Object
Read a markdown table from a file
65 66 67 68 |
# File 'lib/marktable.rb', line 65 def self.read(path, headers: true) content = File.read(path) Table.new(content, headers: headers) end |
.table(array, headers: true) ⇒ Object
Convert an array to a Marktable::Table
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 |
# File 'lib/marktable.rb', line 82 def self.table(array, headers: true) table = Table.new([], headers: headers) # Ensure all data values are strings string_array = array.map do |row| # Handle Row instances by extracting their data if row.is_a?(Row) row.data elsif row.is_a?(Hash) row.transform_values(&:to_s) else row.map(&:to_s) end end if headers && string_array.first.is_a?(Hash) header_keys = string_array.first.keys table.instance_variable_set(:@header_row, header_keys.each_with_object({}) { |k, h| h[k] = k }) table.instance_variable_set(:@rows, string_array.map { |row_data| Row.new(row_data, headers: header_keys) }) else table.instance_variable_set(:@rows, string_array.map { |row_data| Row.new(row_data, headers: nil) }) end table end |
.write(path, table_or_data) ⇒ Object
Write a markdown table to a file
71 72 73 74 75 76 77 78 79 |
# File 'lib/marktable.rb', line 71 def self.write(path, table_or_data) content = if table_or_data.is_a?(Table) table_or_data.to_s else table(table_or_data).to_s end File.write(path, content) end |