Sha256: 430c49287819d485b0c9927534f502b85ef7e3b3c91c68ee344fabaf3bc59005

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

module JekyllImport
  module Importers
    class CSV < Importer
      def self.require_deps
        JekyllImport.require_with_fallback(%w[
          csv
          fileutils
        ])
      end

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

      # Reads a csv with title, permalink, body, published_at, and filter.
      # It creates a post file for each row in the csv
      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

      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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jekyll-import-0.2.0 lib/jekyll-import/importers/csv.rb
jekyll-import-0.1.0 lib/jekyll-import/importers/csv.rb
jekyll-import-0.1.0.rc1 lib/jekyll-import/importers/csv.rb
jekyll-import-0.1.0.beta4 lib/jekyll-import/importers/csv.rb