Class: Jekyll::Converters::Csvy
- Inherits:
-
Object
- Object
- Jekyll::Converters::Csvy
- Defined in:
- lib/jekyll-csvy.rb
Instance Method Summary collapse
- #content_row(row, max_widths) ⇒ Object
- #convert(content) ⇒ Object
- #convert_csv(content) ⇒ Object
-
#initialize(config) ⇒ Csvy
constructor
A new instance of Csvy.
- #matches(ext) ⇒ Object
- #output_ext(ext) ⇒ Object
- #separator_row(row, max_widths, delimiter = "-") ⇒ Object
Constructor Details
#initialize(config) ⇒ Csvy
Returns a new instance of Csvy.
6 7 8 9 10 |
# File 'lib/jekyll-csvy.rb', line 6 def initialize(config) Jekyll::External.require_with_graceful_fail "jekyll-pandoc" @config = config end |
Instance Method Details
#content_row(row, max_widths) ⇒ Object
57 58 59 |
# File 'lib/jekyll-csvy.rb', line 57 def content_row(row, max_widths) row.each_with_index.reduce("|") { |sum, (x,i)| sum + ' ' + x + ' ' * (max_widths[i] - x.length) + " |" } end |
#convert(content) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/jekyll-csvy.rb', line 20 def convert(content) # convert csv content into markdown table using grid table format content = convert_csv(content) # convert markdown into HTML table # uses pandoc with the grid_tables extension site = Jekyll::Site.new(@config) mkconverter = site.find_converter_instance(::Jekyll::Converters::Markdown) mkconverter.convert(content) end |
#convert_csv(content) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jekyll-csvy.rb', line 31 def convert_csv(content) rows = ::CSV.parse(content) # calculate max width of each column columns = rows.transpose max_widths = columns.map { |c| c.max_by { |x| x.length }}.map { |c| c.length } # header table = [] table << separator_row(rows.first, max_widths) # header row table << content_row(rows.first, max_widths) table << separator_row(rows.first, max_widths, delimiter = "=") # body rows rows[1..-1].each do |row| table << content_row(row, max_widths) table << separator_row(row, max_widths) end table.join("\n") + "\n" rescue => e raise Jekyll::Errors::FatalException, "Conversion failed with error: #{e.message}" end |
#matches(ext) ⇒ Object
12 13 14 |
# File 'lib/jekyll-csvy.rb', line 12 def matches(ext) ext =~ /^\.csvy$/i end |
#output_ext(ext) ⇒ Object
16 17 18 |
# File 'lib/jekyll-csvy.rb', line 16 def output_ext(ext) ".html" end |
#separator_row(row, max_widths, delimiter = "-") ⇒ Object
61 62 63 |
# File 'lib/jekyll-csvy.rb', line 61 def separator_row(row, max_widths, delimiter = "-") row.each_with_index.reduce("+") { |sum, (x,i)| sum + delimiter * (max_widths[i] + 2) + "+" } end |