Class: JekyllImport::Importers::CSV

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

Defined Under Namespace

Classes: CSVPost

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jekyll-import/importers/csv.rb', line 21

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" # header

    posts += 1
    write_post(CSVPost.new(row), options)
  end
  Jekyll.logger.info "Created #{posts} posts!"
end

.require_depsObject



6
7
8
9
10
11
12
# File 'lib/jekyll-import/importers/csv.rb', line 6

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

.specify_options(c) ⇒ Object



14
15
16
17
# File 'lib/jekyll-import/importers/csv.rb', line 14

def self.specify_options(c)
  c.option "file",            "--file NAME",       'The CSV file to import (default: "posts.csv")'
  c.option "no-front-matter", "--no-front-matter", "Do not add the default front matter to the post body"
end

.write_frontmatter(f, post, options) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jekyll-import/importers/csv.rb', line 83

def self.write_frontmatter(f, post, options)
  no_frontmatter = options.fetch("no-front-matter", false)
  unless no_frontmatter
    f.puts YAML.dump(
      "layout"    => "post",
      "title"     => post.title,
      "date"      => post.published_at.to_s,
      "permalink" => post.permalink
    )
    f.puts "---"
  end
end

.write_post(post, options = {}) ⇒ Object



76
77
78
79
80
81
# File 'lib/jekyll-import/importers/csv.rb', line 76

def self.write_post(post, options = {})
  File.open(File.join("_posts", post.filename), "w") do |f|
    write_frontmatter(f, post, options)
    f.puts post.body
  end
end