Class: CSVEmbedder::CSVTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/embed_csv.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, url, tokens) ⇒ CSVTag

Returns a new instance of CSVTag.



5
6
7
8
# File 'lib/embed_csv.rb', line 5

def initialize(tag_name, url, tokens)
  super
  @url = url
end

Instance Method Details

#render(context) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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