Skip to content

Instantly share code, notes, and snippets.

@malev
Last active August 29, 2015 14:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malev/71fc62ffb5219b15c1b5 to your computer and use it in GitHub Desktop.
Save malev/71fc62ffb5219b15c1b5 to your computer and use it in GitHub Desktop.
Convert CSV files into HTML

CSV to HTML (cli)

Just run:

$ ruby csvtohtml.rb data.csv > table.html

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.

Pseudo installation (not needed)

You can also add execution permissions with:

$ chmod +x csvtohtml.rb

And maybe add it to you system path. In my case I have a ~/bin folder on my path:

$ mkdir -p ~/bin
$ cp csvtohtml.rb ~/bin
$ export PATH=$HOME/bin:$PATH

Some options:

Usage: csvtohtml.rb data.csv [options]
    -i, --table-id TABLEID           Table's ID
    -c, --table-class TABLECLASS     Table's class
    -d, --td-class class1,class2     Field's classes
        --th-class class1,class2     Header's classes
#!/usr/bin/env ruby
# encoding: UTF-8
require 'csv'
require 'erb'
require 'optparse'
class CsvToHtml
attr_reader :headers, :options, :rows
def initialize(data, options)
parser = CSV.parse(data, headers: true)
@headers = parser.headers
@options = options
@rows = parser
end
def get_binding
binding
end
def tag(name, opts={})
output = [name]
output << "class=\"#{opts[:class]}\"" if opts.has_key?(:class)
output << "id=\"#{opts[:id]}\"" if opts.has_key?(:id)
"<" + output.join(' ') + ">"
end
def row_class(options, index)
classes = options[:row_class]
if classes.nil? || classes[index].nil?
''
else
" class=\"#{classes[index]}\""
end
end
def header_class(options, index)
classes = options[:header_class]
if classes.nil? || classes[index].nil?
''
else
" class=\"#{classes[index]}\""
end
end
end
class Application
def initialize(argv)
@options, @file = parse_options(argv)
end
def run
csv_to_html = if @file.empty?
CsvToHtml.new(STDIN.read, @options)
else
CsvToHtml.new(File.read(ARGV[0]), @options)
end
print ERB.new(DATA.read).result(csv_to_html.get_binding)
end
def parse_options(argv)
options = {}
parser = OptionParser.new
parser.banner = "Usage: csvtohtml.rb data.csv [options]"
# Optional argument; Table's ID
parser.on("-i", "--table-id TABLEID", "Table's ID") do |v|
options[:id] = v
end
# Optional argument; Table's class
parser.on("-c", "--table-class TABLECLASS", "Table's class") do |v|
options[:class] = v
end
parser.on("-d", "--td-class class1,class2", Array, "Field's classes") do |v|
options[:row_class] = v
end
parser.on("--th-class class1,class2", Array, "Header's classes") do |v|
options[:header_class] = v
end
files = parser.parse(argv)
[options, files]
end
end
begin
Application.new(ARGV).run
rescue Errno::ENOENT => err
abort "csvtohtml: #{err.message}"
rescue OptionParser::InvalidOption => err
abort "csvtohtml: #{err.message}\nusage: csvtohtml.rb data.csv [options]"
end
__END__
<%= tag('table', options) %>
<thead><% headers.each_with_index do |header, index| %>
<th<%= header_class(options, index) %>>
<%= header %>
</th><% end %>
</thead>
<tbody><% rows.each do |row| %>
<tr><% row.each_with_index do |r, index| %>
<td data-title="<%= r[0] %>"<%= row_class(options, index) %>><%= r[1] %></td><% end %>
</tr><% end %>
</tbody>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment