csv2html 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8a8a95222dfeae0e554544e7537130251ceb7a90
4
+ data.tar.gz: a424b67f6c0bd7631e66622224d4e7104b0259cc
5
+ SHA512:
6
+ metadata.gz: 502b4dae4ed2a78be27c2ac9e4275419f876ee5c30a8487c2aa2f00b5de58f2791fe4909c8f883c4a2b27b61f97aa0a601ca52963808a34c6039fcaf9ab2c09c
7
+ data.tar.gz: df25d934acc39ecc342bdfae9cdbb315c1856f1148423cb05f065a4b1674abb468ef3adf521e92f38620c357c0fd983b5e02d2e7ccc887d1a705483c956bc8ff
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in csv2html.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Marcos Vanetta
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.
@@ -0,0 +1,36 @@
1
+ # CSV to HTML (cli)
2
+
3
+ ## Just run:
4
+
5
+ $ gem install csv2html
6
+ $ ruby csv2html data.csv > table.html
7
+
8
+ You can customize the table's id, table's classes, header's classes and field's classes. You can also use linux pipes and it's only one small file long with **NO** dependencies.
9
+
10
+ ## Pseudo installation (not needed)
11
+
12
+ You can also add execution permissions with:
13
+
14
+ $ chmod +x csvtohtml.rb
15
+
16
+ And maybe add it to you system path. In my case I have a `~/bin` folder on my path:
17
+
18
+ $ mkdir -p ~/bin
19
+ $ cp csvtohtml.rb ~/bin
20
+ $ export PATH=$HOME/bin:$PATH
21
+
22
+ ## Some options:
23
+
24
+ Usage: csvtohtml.rb data.csv [options]
25
+ -i, --table-id TABLEID Table's ID
26
+ -c, --table-class TABLECLASS Table's class
27
+ -d, --td-class class1,class2 Field's classes
28
+ --th-class class1,class2 Header's classes
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it ( https://github.com/[my-github-username]/csv2html/fork )
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "csv2html"
4
+
5
+
6
+ begin
7
+ CSV2HTML::Application.new(ARGV).run
8
+ rescue Errno::ENOENT => err
9
+ abort "csvtohtml: #{err.message}"
10
+ rescue OptionParser::InvalidOption => err
11
+ abort "csvtohtml: #{err.message}\nusage: csvtohtml.rb data.csv [options]"
12
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'csv2html/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "csv2html"
8
+ spec.version = Csv2html::VERSION
9
+ spec.authors = ["Marcos Vanetta"]
10
+ spec.email = ["marcosvanetta@gmail.com"]
11
+ spec.summary = %q{Take a CSV file and make it a HTML table}
12
+ spec.description = %q{Take a CSV file and make it a HTML table}
13
+ spec.homepage = "http://codingnews.info"
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,114 @@
1
+ # encoding: UTF-8
2
+
3
+ require "csv"
4
+ require "erb"
5
+ require "optparse"
6
+ require "csv2html/version"
7
+
8
+
9
+ module CSV2HTML
10
+ class Conversor
11
+ attr_reader :headers, :options, :rows
12
+
13
+ def initialize(data, options)
14
+ parser = CSV.parse(data, headers: true)
15
+ @headers = parser.headers
16
+ @options = options
17
+ @rows = parser
18
+ end
19
+
20
+ def get_binding
21
+ binding
22
+ end
23
+
24
+ def tag(name, opts={})
25
+ output = [name]
26
+ output << "class=\"#{opts[:class]}\"" if opts.has_key?(:class)
27
+ output << "id=\"#{opts[:id]}\"" if opts.has_key?(:id)
28
+ "<" + output.join(' ') + ">"
29
+ end
30
+
31
+ def row_class(options, index)
32
+ classes = options[:row_class]
33
+ if classes.nil? || classes[index].nil?
34
+ ''
35
+ else
36
+ " class=\"#{classes[index]}\""
37
+ end
38
+ end
39
+
40
+ def header_class(options, index)
41
+ classes = options[:header_class]
42
+ if classes.nil? || classes[index].nil?
43
+ ''
44
+ else
45
+ " class=\"#{classes[index]}\""
46
+ end
47
+ end
48
+ end
49
+
50
+ class Application
51
+ def initialize(argv)
52
+ @options, @file = parse_options(argv)
53
+ end
54
+
55
+ def run
56
+ csv_to_html = if @file.empty?
57
+ Conversor.new(STDIN.read, @options)
58
+ else
59
+ Conversor.new(File.read(ARGV[0]), @options)
60
+ end
61
+ print ERB.new(DATA.read).result(csv_to_html.get_binding)
62
+ end
63
+
64
+ def parse_options(argv)
65
+ options = {}
66
+ parser = OptionParser.new
67
+
68
+ parser.banner = "Usage: csvtohtml.rb data.csv [options]"
69
+
70
+ # Optional argument; Table's ID
71
+ parser.on("-i", "--table-id TABLEID", "Table's ID") do |v|
72
+ options[:id] = v
73
+ end
74
+
75
+ # Optional argument; Table's class
76
+ parser.on("-c", "--table-class TABLECLASS", "Table's class") do |v|
77
+ options[:class] = v
78
+ end
79
+
80
+ parser.on("-d", "--td-class class1,class2", Array, "Field's classes") do |v|
81
+ options[:row_class] = v
82
+ end
83
+
84
+ parser.on("--th-class class1,class2", Array, "Header's classes") do |v|
85
+ options[:header_class] = v
86
+ end
87
+
88
+ files = parser.parse(argv)
89
+ [options, files]
90
+ end
91
+ end
92
+ end
93
+
94
+ begin
95
+ CSV2HTML::Application.new(ARGV).run
96
+ rescue Errno::ENOENT => err
97
+ abort "csvtohtml: #{err.message}"
98
+ rescue OptionParser::InvalidOption => err
99
+ abort "csvtohtml: #{err.message}\nusage: csvtohtml.rb data.csv [options]"
100
+ end
101
+
102
+ __END__
103
+ <%= tag('table', options) %>
104
+ <thead><% headers.each_with_index do |header, index| %>
105
+ <th<%= header_class(options, index) %>>
106
+ <%= header %>
107
+ </th><% end %>
108
+ </thead>
109
+ <tbody><% rows.each do |row| %>
110
+ <tr><% row.each_with_index do |r, index| %>
111
+ <td data-title="<%= r[0] %>"<%= row_class(options, index) %>><%= r[1] %></td><% end %>
112
+ </tr><% end %>
113
+ </tbody>
114
+ </table>
@@ -0,0 +1,3 @@
1
+ module Csv2html
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv2html
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marcos Vanetta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-19 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Take a CSV file and make it a HTML table
42
+ email:
43
+ - marcosvanetta@gmail.com
44
+ executables:
45
+ - csv2html
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/csv2html
55
+ - csv2html.gemspec
56
+ - lib/csv2html.rb
57
+ - lib/csv2html/version.rb
58
+ homepage: http://codingnews.info
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Take a CSV file and make it a HTML table
82
+ test_files: []