-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathcsv.rb
More file actions
97 lines (83 loc) · 2.94 KB
/
csv.rb
File metadata and controls
97 lines (83 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# frozen_string_literal: true
module JekyllImport
module Importers
class CSV < Importer
def self.require_deps
JekyllImport.require_with_fallback(%w(
csv
fileutils
yaml
))
end
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
# 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" # header
posts += 1
write_post(CSVPost.new(row), options)
end
Jekyll.logger.info "Created #{posts} posts!"
end
class CSVPost
attr_reader :title, :permalink, :body, :markup
MissingDataError = Class.new(RuntimeError)
# Creates a CSVPost
#
# row - Array of data, length of 4 or 5 with the columns:
#
# 1. title
# 2. permalink
# 3. body
# 4. published_at
# 5. markup (markdown, textile)
def initialize(row)
@title = row[0] || missing_data("Post title not present in first column.")
@permalink = row[1] || missing_data("Post permalink not present in second column.")
@body = row[2] || missing_data("Post body not present in third column.")
@published_at = row[3] || missing_data("Post publish date not present in fourth column.")
@markup = row[4] || "markdown"
end
def published_at
if @published_at && !@published_at.is_a?(DateTime)
@published_at = DateTime.parse(@published_at)
else
@published_at
end
end
def filename
"#{published_at.strftime("%Y-%m-%d")}-#{File.basename(permalink, ".*")}.#{markup}"
end
def missing_data(message)
raise MissingDataError, message
end
end
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
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
end
end
end