Class: Jekyll::Csvy
- Inherits:
-
Converter
- Object
- Converter
- Jekyll::Csvy
- Defined in:
- lib/jekyll-csvy.rb
Instance Method Summary collapse
- #content_rows(row, max_widths, max_height) ⇒ 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.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/jekyll-csvy.rb', line 8 def initialize(config) Jekyll::External.require_with_graceful_fail "jekyll-pandoc" # requires the Pandoc markdown converter # install jekyll-pandoc gem and set "markdown: Pandoc" in _config.yml unless config["markdown"] == "Pandoc" raise Jekyll::Errors::FatalException, "Pandoc markdown converter required" end @config = config end |
Instance Method Details
#content_rows(row, max_widths, max_height) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/jekyll-csvy.rb', line 65 def content_rows(row, max_widths, max_height) content_row = [] 0.upto(max_height - 1) do |j| content_row << row.each_with_index.reduce("|") do |sum, (x,i)| sum + ' ' + x.lines[j].to_s.chomp + ' ' * (max_widths[i] - x.lines[j].to_s.chomp.length) + " |" end end content_row end |
#convert(content) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/jekyll-csvy.rb', line 28 def convert(content) # convert csv content into markdown table using grid table format content = convert_csv(content) # convert grid_tables markdown into HTML table using pandoc site = Jekyll::Site.new(@config) markdown_converter = site.find_converter_instance(Jekyll::Converters::Markdown) markdown_converter.convert(content) end |
#convert_csv(content) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/jekyll-csvy.rb', line 38 def convert_csv(content) rows = ::CSV.parse(content) # calculate max height of each row max_heights = rows.map { |r| r.max_by { |x| x.lines.count }}.map { |r| r.lines.count } # 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) # rows rows.each_with_index do |row,i| delimiter = i == 0 ? "=" : "-" table += content_rows(row, max_widths, max_heights[i]) table << separator_row(row, max_widths, delimiter) end table.join("\n") + "\n" rescue => e raise Jekyll::Errors::FatalException, "Conversion failed with error: #{e.}" end |
#matches(ext) ⇒ Object
20 21 22 |
# File 'lib/jekyll-csvy.rb', line 20 def matches(ext) ext =~ /^\.csvy$/i end |
#output_ext(ext) ⇒ Object
24 25 26 |
# File 'lib/jekyll-csvy.rb', line 24 def output_ext(ext) ".html" end |
#separator_row(row, max_widths, delimiter = "-") ⇒ Object
75 76 77 78 79 |
# File 'lib/jekyll-csvy.rb', line 75 def separator_row(row, max_widths, delimiter = "-") row.each_with_index.reduce("+") do |sum, (x,i)| sum + delimiter * (max_widths[i] + 2) + "+" end end |