aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2021-06-06 13:54:47 +0200
committerHarald Eilertsen <haraldei@anduin.net>2021-06-06 13:54:47 +0200
commit399e916fe15b08a6b0c3ca155d92e874f936193a (patch)
treecd8d862a224ed174ad2f278faa7aaca01cb40613
parent5ca6bfad73bec0a2f3b9932c7bedf9d9b50602c0 (diff)
downloadhmnoweb-399e916fe15b08a6b0c3ca155d92e874f936193a.tar.gz
hmnoweb-399e916fe15b08a6b0c3ca155d92e874f936193a.tar.bz2
hmnoweb-399e916fe15b08a6b0c3ca155d92e874f936193a.zip
Refactor export script, and produce nicer yaml.
Don't use serialized ruby object format for users and categories, just list the attributes as separate keys under the object identified by it's slug. Also properly export yaml frontmatter for posts.
-rw-r--r--script/export.rb46
1 files changed, 31 insertions, 15 deletions
diff --git a/script/export.rb b/script/export.rb
index 2fc6f4b..f18263e 100644
--- a/script/export.rb
+++ b/script/export.rb
@@ -20,8 +20,24 @@ require 'fileutils'
output_dir = "export"
FileUtils.mkdir(output_dir)
-IO.write(File.join(output_dir, '_users.yml'), Refinery::User.all.to_yaml)
-IO.Write(File.join(output_dir, '_categories.yml'), Refinery::Categories.all.to_yaml)
+
+puts "Exporting users..."
+
+users = Refinery::User.all.reduce({}) do |m, user|
+ { user.slug => user.attributes }
+end
+
+IO.write(File.join(output_dir, '_users.yml'), users.to_yaml)
+
+puts "Exporting categories..."
+
+categories = Refinery::Blog::Category.all.reduce({}) do |m, cat|
+ m.merge({ cat.slug => cat.attributes })
+end
+
+IO.write(File.join(output_dir, '_categories.yml'), categories.to_yaml)
+
+puts "Exporting posts..."
posts_dir = File.join(output_dir, '_posts')
@@ -29,21 +45,21 @@ FileUtils.mkdir_p(posts_dir)
Refinery::Blog::Post.all.each do |post|
filename = "#{post.published_at.to_date}-#{post.slug}.html"
- puts filename
+
+ frontmatter = {
+ 'title' => post.title,
+ 'published_at' => post.published_at,
+ 'creates_at' => post.created_at,
+ 'updated_at' => post.updated_at,
+ 'draft' => post.draft,
+ 'author' => post.author.username,
+ 'categories' => post.categories.map { |cat| cat.slug },
+ 'tags' => post.tags.map { |tag| tag.name },
+ 'custom_teaser' => post.custom_teaser,
+ }
File.open(File.join(posts_dir, filename), "w", 0744) do |f|
- f.write "---\n"
- f.write "title: #{post.title}\n"
- f.write "published_at: #{post.published_at}\n"
- f.write "created_at: #{post.created_at}\n"
- f.write "updated_at: #{post.updated_at}\n"
- f.write "post_id: #{post.id}\n"
- f.write "draft: #{post.draft}\n"
- f.write "user_id: #{post.user_id}\n"
- f.write "author: #{post.author.username}\n"
- f.write "categories: #{post.categories}\n"
- f.write "custom_teaser: #{post.custom_teaser}\n"
- f.write "---\n\n"
+ f.write frontmatter.to_yaml + "\n---\n\n"
f.write post.body
end
end