Module: Marktable

Defined in:
lib/marktable.rb,
lib/marktable/row.rb,
lib/marktable/table.rb

Defined Under Namespace

Classes: Row, Table

Class Method Summary collapse

Class Method Details

.filter(markdown_table, pattern, headers: true) ⇒ Object

Filter rows matching a pattern



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/marktable.rb', line 105

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



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/marktable.rb', line 18

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/marktable.rb', line 32

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)



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/marktable.rb', line 119

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



8
9
10
# File 'lib/marktable.rb', line 8

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



13
14
15
# File 'lib/marktable.rb', line 13

def self.parse_line(markdown_row)
  Row.parse(markdown_row)
end

.read(path, headers: true) ⇒ Object

Read a markdown table from a file



61
62
63
64
# File 'lib/marktable.rb', line 61

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



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
# File 'lib/marktable.rb', line 78

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



67
68
69
70
71
72
73
74
75
# File 'lib/marktable.rb', line 67

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