-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate_prismic_export
executable file
·247 lines (208 loc) · 5.74 KB
/
generate_prismic_export
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env ruby
require "front_matter_parser"
require "json"
require "SecureRandom"
require "fileutils"
require "colorize"
require "jekyll"
require "nokogiri"
uid = SecureRandom.hex(4)
@file_count = 0
def posts
@posts ||= Dir
.children("_posts")
.map { |filename| "_posts/#{filename}" }
end
AUTHORS = {
"1" => {
# name: "Michael Hayes",
id: "W2GE0B8AAB0Ar4Lc",
mask: "author",
},
"2" => {
# name: "Jamie McHale",
id: "W2GFPB8AACQAr4S_",
mask: "author",
},
"3" => {
# name: "Cally Russell",
id: "W2GFVB8AACEAr4Ux",
mask: "author",
},
"4" => {
# name: "David Wong",
id: "W2GFbh8AACEAr4Wj",
mask: "author",
},
"5" => {
# name: "Gregg O'Malley",
id: "W2GFhR8AACEAr4YX",
mask: "author",
},
"6" => {
# name: "Rich Smith",
id: "W2GFmx8AAB0Ar4Z8",
mask: "author",
},
"7" => {
# name: "Morgan Roberts",
id: "W2GFuh8AACQAr4cE",
mask: "author",
},
"8" => {
# name: "Kyle Bremner",
id: "W2GFwx8AACQAr4cv",
mask: "author",
},
"9" => {
# name: "John Bell",
id: "W2GF3B8AAB8Ar4ei",
mask: "author",
},
"10" => {
# name: "Peter Retson",
id: "W2GF_B8AAB0Ar4g5",
mask: "author",
},
"11" => {
# name: "Michael Carr",
id: "W2GGJB8AAB0Ar4ju",
mask: "author",
},
"12" => {
# name: "Charlotte Dougall",
id: "W2GFDB8AAB8Ar4Pt",
mask: "author",
},
"13" => {
# name: "Rachel Guthartz",
id: "W2GGQR8AAB8Ar4lx",
mask: "author",
},
}
# source path => output path
COPIED_FILES = {}
def imported_image(key, path, base_dir)
return nil unless path
input_location = File.join(FileUtils.pwd, path)
unless File.file? input_location
puts "\tFile at #{path} doesn't exist".red
puts "\t\tFull Path: #{input_location}"
return
end
file_path = if COPIED_FILES[path]
COPIED_FILES[path]
else
ext = path.split(".").last
new_file_path = "images/#{key}.#{ext}"
# Copy the file
output_location = "#{base_dir}/#{new_file_path}"
output_dir = "#{base_dir}/images"
Dir.mkdir(output_dir) unless File.directory?(output_dir)
FileUtils.cp(input_location, output_location)
@file_count += 1
# Cache result url
COPIED_FILES[path] = new_file_path
end
{
origin: {
url: file_path,
},
url: file_path,
}
end
def write_json(post, output_dir)
group_index = @file_count / 200
output_dir = "#{output_dir}-#{group_index}"
Dir.mkdir(output_dir) unless File.directory?(output_dir)
puts "\tProcessing #{post}"
parsed = FrontMatterParser::Parser.parse_file(post)
data = parsed.front_matter
# Get date and uid from permalink
# Permalink is of format /2018/02/17/hello-world-2
# uid is hello-world-2 and post_date is 2018-02-17
permalink = data["permalink"]
uid = permalink.split("/").last
post_date = permalink.split("/")[1..3].join("-")
# Find the jekyll-outputted content file
# Output is to pwd/_site
# Post is either in /year/month/day/uid.html or /year/month/day/uid/index.html
uid_html = File.join(FileUtils.pwd, "_site", permalink.split(".").first + ".html")
index_html = File.join(FileUtils.pwd, "_site", permalink.split(".").first, "index.html")
html_location = if File.file? uid_html
uid_html
elsif File.file? index_html
index_html
else
puts "Couldn't find html file for post".red
puts "Tried:".red
puts "\t#{uid_html}".red
puts "\t#{index_html}".red
end
# Format the html for prismic
html = File.open(html_location, "rb").read
doc = Nokogiri::HTML(html)
post_html = doc.css(".post")
post_html.search(".author").remove
post_html.search(".date").remove
post_html.search(".relatedPosts").remove
post_html = post_html.first.to_s
# Prismic wraps indented <p> tags in <code> for some reason, even in their basic text field
post_html = post_html.gsub(/\n/, "")
post_html = post_html.gsub(/\t/, "")
output = {
type: "blog_post",
lang: "en-gb",
uid: uid,
tags: data["categories"] || [],
published_date: post_date,
title: data["title"],
subtitle: "",
author: AUTHORS["#{data["author"]}"],
header_image: imported_image("#{uid}-header_image", data["headerImage"] || data["image"], output_dir),
list_image: imported_image("#{uid}-list_image", data["image"] || data["headerImage"], output_dir),
body: [
{
"key": "raw-html-md",
"value": {
"repeat": [
{},
],
"non-repeat": {
"text": post_html,
},
},
},
],
}
output_file_path = "#{output_dir}/new_#{SecureRandom.uuid}_en-gb.json"
File.write(output_file_path, JSON.dump(output))
@file_count += 1
end
# Process site
puts "Generating Jekyll site..."
conf = Jekyll.configuration({
"source" => FileUtils.pwd,
})
Jekyll::Site.new(conf).process
puts "...done".green
# Process
puts "Exporting #{posts.length} posts"
# Prismic accepts up to 200 documents per archive, including images
# Create folder
output_dir = "prismic-import-#{uid}"
puts "Created output directory".green
# Add json
puts "Parsing posts"
posts.map do |post|
write_json(post, output_dir)
end
# Done
puts "Finished exporting #{posts.length} posts".green
# TODO Zip folder contents
puts "\n\n--------"
puts "Zip the " + "contents".cyan + " of the folder(s) to upload to prismic."
puts "Do not zip the folder itself, Prismic won't like it."
puts "Export id is #{uid}"
puts "\nhttps://rookieoven.prismic.io/settings/import/"
puts "--------"