class CSVEmbedder::CSVTag

Public Class Methods

new(tag_name, url, tokens) click to toggle source
Calls superclass method
# File lib/embed_csv.rb, line 5
def initialize(tag_name, url, tokens)
  super
  @url = url
end

Public Instance Methods

render(context) click to toggle source
# File lib/embed_csv.rb, line 10
def render(context)
  # current directory
  filedir = File.dirname(context.registers[:page]["path"])

  csvpath = File.path(File.join(filedir, @url.strip))

  table_tag = "<table>"
  table_tag += '<caption>Data from here: <a href="'+ @url + '">' + @url + '</a></caption>'
  count = 0
  CSV.foreach(csvpath) do |row|
    if count == 0
      table_tag += "<thead>"
    else
      table_tag += "<tbody>"
    end
    table_tag += "<tr>"
    for item in row
      table_tag += "<td>#{item}</td>"
    end
    table_tag += "</tr>"
    if count == 0
      table_tag += "</thead>"
    else
      table_tag += "</tbody>"
    end
    count += 1
  end

  table_tag += "</table>"
end