aboutsummaryrefslogblamecommitdiffstats
path: root/script/export.rb
blob: 0e4b8d79ab8496940ab065cc16de4a7b037fbdcc (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
















                                                                       




                           











                                                                
                                                                                             




                                                                      



                                           


                                                             











                                                           
 
                                                             
                                             


                     
#!/usr/bin/env ruby
# This file is part of hmnoweb
# Copyright (C) 2020  Harald Eilertsen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require 'fileutils'

output_dir = "export"

FileUtils.mkdir(output_dir)

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.merge({ 'title' => cat.title, 'slug' => cat.slug }) })
end

IO.write(File.join(output_dir, '_categories.yml'), categories.to_yaml)

puts "Exporting posts..."

posts_dir = File.join(output_dir, '_posts')

FileUtils.mkdir_p(posts_dir)

Refinery::Blog::Post.all.each do |post|
  filename = "#{post.published_at.to_date}-#{post.slug}.html"

  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 frontmatter.to_yaml + "\n---\n\n"
    f.write post.body
  end
end