Class: JekyllImport::Importers::CSV

Inherits:
JekyllImport::Importer show all
Defined in:
lib/jekyll-import/importers/csv.rb

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.build_name(row) ⇒ Object



45
46
47
# File 'lib/jekyll-import/importers/csv.rb', line 45

def self.build_name(row)
  row[3].split(" ")[0]+"-"+row[1]+(row[4] =~ /markdown/ ? ".markdown" : ".textile")
end

.process(options) ⇒ Object

Reads a csv with title, permalink, body, published_at, and filter. It creates a post file for each row in the csv



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jekyll-import/importers/csv.rb', line 17

def self.process(options)
  file = options.fetch('file', "posts.csv")

  FileUtils.mkdir_p "_posts"
  posts = 0
  abort "Cannot find the file '#{file}'. Aborting." unless File.file?(file)

  ::CSV.foreach(file) do |row|
    next if row[0] == "title"
    posts += 1
    name = build_name(row)
    write_post(name, row[0], row[2])
  end
  "Created #{posts} posts!"
end

.require_depsObject



4
5
6
7
8
9
# File 'lib/jekyll-import/importers/csv.rb', line 4

def self.require_deps
  JekyllImport.require_with_fallback(%w[
    csv
    fileutils
  ])
end

.specify_options(c) ⇒ Object



11
12
13
# File 'lib/jekyll-import/importers/csv.rb', line 11

def self.specify_options(c)
  c.option 'file', '--file NAME', 'The CSV file to import (default: "posts.csv")'
end

.write_post(name, title, content) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jekyll-import/importers/csv.rb', line 33

def self.write_post(name, title, content)
  File.open("_posts/#{name}", "w") do |f|
    f.puts <<-HEADER
---
layout: post
title: #{title}
---
HEADER
    f.puts content
  end
end