markdown-tables 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/markdown-tables.rb +89 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d81c56a6b5704f57fe1b02ecf891051306873b50
4
+ data.tar.gz: 78e3d1343aaadb46e5ea768b498960c73f1ea699
5
+ SHA512:
6
+ metadata.gz: f31cc74738da99e034bfef0791247d24c7bba6425295c183f95cd2f4ff0928f78bfec11747ebb078c2de6d6cec314a4f8fdec350200458f0721120658dfcb0c5
7
+ data.tar.gz: 6295dd0283ae39e794cec164469114faa379331271c46ee364d8b7d4c9b88909a7bc8e28e0e5ec68ce78bac94264b2429fe20b49055872c52ba55ce24657b1e3
@@ -0,0 +1,89 @@
1
+ # Sanity checks for make_table.
2
+ def validate(labels, data, align, is_rows)
3
+ if labels.class != Array
4
+ raise('labels must be an Array')
5
+ elsif data.class != Array || data.any? {|datum| datum.class != Array}
6
+ raise('data must be a two-dimensional array')
7
+ elsif labels.empty?
8
+ raise('No column labels given')
9
+ elsif data.empty?
10
+ raise('No columns given')
11
+ elsif data.all? {|datum| datum.empty?}
12
+ raise('No cells given')
13
+ elsif labels.any? {|label| !label.respond_to?(:to_s)}
14
+ raise('One or more column labels cannot be made into a string')
15
+ elsif data.any? {|datum| datum.any? {|cell| !cell.respond_to?(:to_s)}}
16
+ raise('One or more cells cannot be made into a string')
17
+ elsif ![String, Array].include?(align.class)
18
+ raise('align must be a String or Array')
19
+ elsif align.class == Array && align.any? {|val| val.class != String}
20
+ raise('One or more align values is not a String')
21
+ elsif !is_rows && data.length > labels.length
22
+ raise('Too many data columns given')
23
+ elsif is_rows && data.any? {|row| row.length > labels.length}
24
+ raise('One or more rows has too many cells')
25
+ end
26
+ end
27
+
28
+ # Convert all input to strings and replace any '|' characters with
29
+ # non-breaking equivalents,
30
+ def sanitize!(labels, data)
31
+ bar = '|' # Non-breaking HTML vertical bar.
32
+ labels.map! {|label| label.to_s.gsub('|', bar)}
33
+ data.length.times {|i| data[i].map! {|cell| cell.to_s.gsub('|', bar)}}
34
+ end
35
+
36
+ # Generate the alignment line from a string or array.
37
+ # align must be a string or array or strings.
38
+ # n: number of labels in the table to be created.
39
+ def alignment(align, n)
40
+ if align.class == String
41
+ alignment = align == 'l' ? ':-' : align == 'r' ? '-:' : ':-:'
42
+ alignment_line = ([alignment] * n).join('|')
43
+ else
44
+ alignments = align.map {
45
+ |a| a.downcase == 'l' ? ':-' : a.downcase == 'r' ? '-:' : ':-l'
46
+ }
47
+ if alignments.length < n
48
+ alignments += [':-:'] * (n - alignments.length)
49
+ end
50
+ alignment_line = alignments.join('|')
51
+ end
52
+ return alignment_line
53
+ end
54
+
55
+ # Generate a Markdown table.
56
+ # labels and data are one and two-dimensional arrays, respectively.
57
+ # All input must have a to_s method.
58
+ # Pass align: 'l' for left alignment or 'r' for right alignment. Anything
59
+ # else will result in cells being centered. Pass an array of align values
60
+ # to specify alignment per column.
61
+ # If is_rows is true, then each sub-array represents a row.
62
+ # Conversely, if is_rows is false, each sub-array of data represents a column.
63
+ # Empty cells can be given with nil or an empty string.
64
+ def make_table(labels, data, align: '', is_rows: false)
65
+ labels = Marshal.load(Marshal.dump(labels))
66
+ data = Marshal.load(Marshal.dump(data))
67
+ validate(labels, data, align, is_rows)
68
+ sanitize!(labels, data)
69
+
70
+ header_line = labels.join('|')
71
+ alignment_line = alignment(align, labels.length)
72
+
73
+ if is_rows
74
+ rows = data.map {|row| row.join('|')}
75
+ else
76
+ max_len = data.map(&:size).max
77
+ rows = []
78
+ max_len.times do |i|
79
+ row = []
80
+ data.each {|col| row.push(col[i])}
81
+ rows.push(row.join('|'))
82
+ end
83
+ end
84
+
85
+ return [header_line, alignment_line, rows.join("\n")].join("\n")
86
+ end
87
+
88
+ # Print out a Markdown table in human-readable form.
89
+ def print_table(table) end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markdown-tables
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris de Graaf
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: chrisadegraaf@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/markdown-tables.rb
20
+ homepage: https://github.com/christopher-dG/markdown-tables
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.6.8
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Utilities for creating and displaying Markdown tables in Ruby
44
+ test_files: []