aboutsummaryrefslogtreecommitdiffstats
path: root/script/export.rb
blob: f18263ec1a21e19a565397697e7d50682b493f86 (plain) (blame)
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
#!/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 })
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