class JekyllImport::Importers::CSV

Public Class Methods

process(options) click to toggle source

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

# 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_deps() click to toggle source
# 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) click to toggle source
# 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. (default: false)"
end
write_frontmatter(f, post, options) click to toggle source
# 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 = {}) click to toggle source
# 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