csvtomd 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6f3839569ff6e52b08f856889ec6188f05028abe
4
+ data.tar.gz: 79e687699df2a3366e2211bea9bae8402bf6b889
5
+ SHA512:
6
+ metadata.gz: a8888b08be0a0c7ef9903aa6e45094075e0df97e8daa3c514b7af6d86c3b605d87dd14e0669390e59a6c66d04c88b91b1019331fc8b666016a634149facedba2
7
+ data.tar.gz: 36fe3079645cf34354ac6d835625361bd316934929a1958eee1630ee0da63f08e93b98116bb50292f879b9150a885cb324e5ee07548a2ebc770394f0d58a19ad
data/.bin/htmldiff ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'htmldiff' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('diff-lcs', 'htmldiff')
data/.bin/ldiff ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'ldiff' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('diff-lcs', 'ldiff')
data/.bin/rake ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rake', 'rake')
data/.bin/rspec ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rspec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'rspec')
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format doc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in csvtomd.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 SET_ME_LOCALLY
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # CsvToMd
2
+
3
+ Convert CSV text to markdown style table.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'csvtomd'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install csvtomd
18
+
19
+ ## Usage
20
+
21
+ ### Basic
22
+
23
+ ```ruby
24
+ require "csvtomd"
25
+
26
+ csv = <<CSV
27
+ head1, head2, head3, head4
28
+ line1-1, line1-2, line1-3, line1-4
29
+ line2-1, line2-2, line2-3, line2-4
30
+ line3-1, line3-2, line3-3, line3-4
31
+ CSV
32
+
33
+ puts CsvToMd.convert(csv)
34
+ ```
35
+
36
+ Then, got markdown style table.
37
+
38
+ ```md
39
+ |head1|head2|head3|head4|
40
+ |---|---|---|---|
41
+ |line1-1|line1-2|line1-3|line1-4|
42
+ |line2-1|line2-2|line2-3|line2-4|
43
+ |line3-1|line3-2|line3-3|line3-4|
44
+ ```
45
+
46
+ |head1|head2|head3|head4|
47
+ |---|---|---|---|
48
+ |line1-1|line1-2|line1-3|line1-4|
49
+ |line2-1|line2-2|line2-3|line2-4|
50
+ |line3-1|line3-2|line3-3|line3-4|
51
+
52
+ ### Advanced
53
+
54
+ `CsvToMd` allows empty cells in CSV text.
55
+
56
+ ```ruby
57
+ require "csvtomd"
58
+
59
+ csv = <<CSV
60
+ , , , head,
61
+ , ,
62
+ ,
63
+ CSV
64
+
65
+ puts CsvToMd.convert(csv)
66
+ ```
67
+
68
+ results,
69
+
70
+ ```md
71
+ ||||head||
72
+ |---|---|---|---|---|
73
+ ||||||
74
+ ||||||
75
+ ```
76
+
77
+ ||||head||
78
+ |---|---|---|---|---|
79
+ ||||||
80
+ ||||||
81
+
82
+ ## Contributing
83
+
84
+ 1. Fork it ( https://github.com/[my-github-username]/csvtomd/fork )
85
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
86
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
87
+ 4. Push to the branch (`git push origin my-new-feature`)
88
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/csvtomd.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'csvtomd/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "csvtomd"
8
+ spec.version = Csvtomd::VERSION
9
+ spec.authors = ["xoyip"]
10
+ spec.email = ["xoyip@piyox.info"]
11
+ spec.summary = %q{Convert CSV text to markdown style table.}
12
+ spec.description = {}
13
+ spec.homepage = "https://github.com/xoyip/csvtomd"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,13 @@
1
+ require "rubygems"
2
+
3
+ $:.unshift File.expand_path("../lib", File.dirname(__FILE__))
4
+ require "csvtomd"
5
+
6
+ text = <<CSV
7
+ head1, head2, head3, head4
8
+ hoge, fuga, , foo,
9
+ bar, piyo, baz, aaa
10
+ bbb, , , ,bbb
11
+ CSV
12
+
13
+ puts CsvToMd.convert(text)
@@ -0,0 +1,3 @@
1
+ module Csvtomd
2
+ VERSION = "1.0.0"
3
+ end
data/lib/csvtomd.rb ADDED
@@ -0,0 +1,45 @@
1
+ require "csvtomd/version"
2
+
3
+ module CsvToMd
4
+ class << self
5
+ def convert(text)
6
+ return nil unless text.class == String
7
+
8
+ @column_count = nil
9
+ @text = text.gsub(" ", "")
10
+
11
+ if @text.lines.count > 1
12
+ table_lines = @text.lines.map { |l| convert_line(l) }
13
+ table_lines.insert(1, separator)
14
+ table_lines.join("\n")
15
+ else
16
+ @text
17
+ end
18
+ end
19
+
20
+ def convert_line(line)
21
+ tokens = line.chomp.split(",")
22
+ (column_count - tokens.length).times { tokens << "" }
23
+ table_line = tokens.join("|")
24
+ "|#{table_line}|"
25
+ end
26
+
27
+ def separator
28
+ sep = (["---"]*column_count).join("|")
29
+ "|#{sep}|"
30
+ end
31
+
32
+ def column_count
33
+ @column_count ||= calc_column_count
34
+ end
35
+
36
+ def calc_column_count
37
+ count = 0
38
+ @text.lines.each do |line|
39
+ item_count = line.split(',').length
40
+ count = item_count if item_count > count
41
+ end
42
+ @column_count = count
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+
3
+ describe CsvToMd do
4
+ specify "without empty cells" do
5
+ input = fixture("input1.csv").chomp
6
+ expected = fixture("output1.md").chomp
7
+ output = CsvToMd.convert(input)
8
+ expect(output).to eq(expected)
9
+ end
10
+
11
+ specify "with empty cells" do
12
+ input = fixture("input2.csv").chomp
13
+ expected = fixture("output2.md").chomp
14
+ output = CsvToMd.convert(input)
15
+ expect(output).to eq(expected)
16
+ end
17
+
18
+ specify "with empty string" do
19
+ input = ""
20
+ expected = input
21
+ output = CsvToMd.convert(input)
22
+ expect(output).to eq(expected)
23
+ end
24
+
25
+ specify "with single line" do
26
+ input = "hogehoge"
27
+ expected = input
28
+ output = CsvToMd.convert(input)
29
+ expect(output).to eq(expected)
30
+ end
31
+
32
+ specify "with nil" do
33
+ input = nil
34
+ expected = nil
35
+ output = CsvToMd.convert(input)
36
+ expect(output).to eq(expected)
37
+ end
38
+
39
+ specify "with non-string" do
40
+ input = 10
41
+ expected = nil
42
+ output = CsvToMd.convert(input)
43
+ expect(output).to eq(expected)
44
+ end
45
+
46
+ end
@@ -0,0 +1,4 @@
1
+ head1, head2, head3, head4
2
+ line1-1, line1-2, line1-3, line1-4
3
+ line2-1, line2-2, line2-3, line2-4
4
+ line3-1, line3-2, line3-3, line3-4
@@ -0,0 +1,5 @@
1
+ , , , head,
2
+ ,,
3
+ ,
4
+ ,
5
+ ,
@@ -0,0 +1,5 @@
1
+ |head1|head2|head3|head4|
2
+ |---|---|---|---|
3
+ |line1-1|line1-2|line1-3|line1-4|
4
+ |line2-1|line2-2|line2-3|line2-4|
5
+ |line3-1|line3-2|line3-3|line3-4|
@@ -0,0 +1,6 @@
1
+ ||||head||
2
+ |---|---|---|---|---|
3
+ ||||||
4
+ ||||||
5
+ ||||||
6
+ ||||||
@@ -0,0 +1,12 @@
1
+ require "csvtomd"
2
+
3
+ RSpec.configure do |config|
4
+ end
5
+
6
+ def fixture_path
7
+ File.expand_path("../fixtures", __FILE__)
8
+ end
9
+
10
+ def fixture(file)
11
+ open(File.join(fixture_path, file)).read
12
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csvtomd
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - xoyip
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: "{}"
56
+ email:
57
+ - xoyip@piyox.info
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".bin/htmldiff"
63
+ - ".bin/ldiff"
64
+ - ".bin/rake"
65
+ - ".bin/rspec"
66
+ - ".gitignore"
67
+ - ".rspec"
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - csvtomd.gemspec
73
+ - example/example.rb
74
+ - lib/csvtomd.rb
75
+ - lib/csvtomd/version.rb
76
+ - spec/csvtomd_spec.rb
77
+ - spec/fixtures/input1.csv
78
+ - spec/fixtures/input2.csv
79
+ - spec/fixtures/output1.md
80
+ - spec/fixtures/output2.md
81
+ - spec/spec_helper.rb
82
+ homepage: https://github.com/xoyip/csvtomd
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.2.0
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Convert CSV text to markdown style table.
106
+ test_files:
107
+ - spec/csvtomd_spec.rb
108
+ - spec/fixtures/input1.csv
109
+ - spec/fixtures/input2.csv
110
+ - spec/fixtures/output1.md
111
+ - spec/fixtures/output2.md
112
+ - spec/spec_helper.rb