Module: Mrtable

Defined in:
lib/mrtable.rb,
lib/mrtable/version.rb

Defined Under Namespace

Classes: Table

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.col_len(col) ⇒ Object



89
90
91
92
93
# File 'lib/mrtable.rb', line 89

def self.col_len(col)
  (0...col.size).inject(0) { |sum, i|
    sum + (hankaku?(col[i]) ? 1 : 2)
  }
end

.complement_cols(cols, num_cols_max, val) ⇒ Object



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

def self.complement_cols(cols, num_cols_max, val)
  (0...num_cols_max).map { |ci|
    if ci < cols.size
      cols[ci]
    else
      val
    end
  }
end

.generate(header_cols, rows) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/mrtable.rb', line 228

def self.generate(header_cols, rows)
  table = Table.new(header_cols, rows)

  serealized = table.map_col_with_ci { |col, _|
    serealize_col col
  }

  maxlens = serealized.calc_maxlens()

  padded = serealized.map_col_with_ci { |col, ci|
    pad_col col, maxlens[ci]
  }

  lines = []
  lines << to_table_row(padded.header_cols)
  lines << to_table_row(maxlens.map { |len| "-" * len })
  lines += padded.rows.map { |cols|
    to_table_row(cols)
  }
  lines.map { |line| line + "\n" }.join("")
end

.hankaku?(c) ⇒ Boolean

32-126(0x20-0x7E), 65377-65439(0xFF61-0xFF9F)

Returns:

  • (Boolean)


131
132
133
# File 'lib/mrtable.rb', line 131

def self.hankaku?(c)
  (/^[ -~。-゚]$/ =~ c) ? true : false
end

.int?(s) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/mrtable.rb', line 77

def self.int?(s)
  /^\-?[\d,]+$/ =~ s
end

.json_decode(str) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/mrtable.rb', line 146

def self.json_decode(str)
  if /^".*"$/ =~ str
    JSON.parse('[' + str + ']')[0]
  else
    JSON.parse('["' + str + '"]')[0]
  end
end

.json_encode(val) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/mrtable.rb', line 135

def self.json_encode(val)
  json = JSON.generate([val])
  if /^\["(.*)"\]$/ =~ json
    $1
  elsif /^\[(.+)\]$/ =~ json
    $1
  else
    json
  end
end

.pad_col(col, maxlen) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/mrtable.rb', line 81

def self.pad_col(col, maxlen)
  if int? col
    pad_left col, maxlen
  else
    pad_right col, maxlen
  end
end

.pad_left(s, n) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/mrtable.rb', line 103

def self.pad_left(s, n)
  rest = n - col_len(s)
  if rest <= 0
    return s
  end
  (" " * rest) + s
end

.pad_right(s, n) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/mrtable.rb', line 95

def self.pad_right(s, n)
  rest = n - col_len(s)
  if rest <= 0
    return s
  end
  s + (" " * rest)
end

.parse(text, opts = {}) ⇒ Object



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

def self.parse(text, opts = {})
  lines = text.split(/\r?\n/)
  lines2 = lines.reject { |line|
    /^\s*$/ =~ line ||
    /^\| \-\-\-+ \|/ =~ line
  }
  rows = lines2.map { |line|
    split_row(line)
  }
  raw = Table.new rows[0], rows[1..-1]

  stripped = raw.map_col_with_ci { |col, _|
    col.strip
  }

  parsed = stripped.map_col_with_ci { |col, _|
    parse_col col
  }

  if opts.has_key? :complement
    unless opts[:complement].is_a? String or opts[:complement].nil?
      raise "opts[:complement] must be String or nil"
    end
    parsed = parsed.complement opts[:complement]
  end

  {
    :header_cols => parsed.header_cols,
    :rows => parsed.rows
  }
end

.parse_col(col) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/mrtable.rb', line 154

def self.parse_col(col)
  if col == ''
    nil
  elsif col == '""'
    ""
  else
    json_decode(col)
  end
end

.serealize_col(col) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/mrtable.rb', line 111

def self.serealize_col(col)
  if col.nil?
    return ""
  elsif col == ""
    return '""'
  end

  ret = json_encode(col)
  if /^\s+/ =~ ret || /\s+$/ =~ ret || /^\-+$/ =~ ret
    ret = '"' + ret + '"'
  end

  ret.gsub("|", "\\|")
end

.split_row(line) ⇒ Object



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

def self.split_row(line)
  line2 = line + " "
  cols = []
  buf = ""
  pos = 2
  pos_delta = nil

  num_repeat_max = line2.size
  num_repeat_max.times do
    break if pos >= line2.size
    pos_delta = 1
    rest = line2[pos..-1]
    if /^ \| / =~ rest
      cols << buf; buf = ""
      pos_delta = 3
    elsif /^\\/ =~ rest
      if rest[1] == "|"
        buf += rest[1]
        pos_delta = 2
      else
        buf += rest[0..1]
        pos_delta = 2
      end
    else
      buf += rest[0]
    end
    pos += pos_delta
  end

  cols
end

.to_table_row(cols) ⇒ Object



126
127
128
# File 'lib/mrtable.rb', line 126

def self.to_table_row(cols)
  "| " + cols.join(" | ") + " |"
end