diff options
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/pg_jekyll.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/bin/pg_jekyll.rb b/bin/pg_jekyll.rb new file mode 100755 index 0000000..ed2ac3b --- /dev/null +++ b/bin/pg_jekyll.rb @@ -0,0 +1,43 @@ +#!/usr/bin/env ruby +# This Source Code Form is subject to the terms of the Mozilla Public License, +# v. 2.0. If a copy of the MPL was not distributed with this file, You can +# obtain one at https://mozilla.org/MPL/2.0/. -} + +# frozen_string_literal: true + +require 'pg' + +connection = PG.connect(user: ENV['user'], dbname: ENV['DBNAME']) +query = <<~SQL + SELECT articles.*, categories.name + FROM articles + INNER JOIN categories on categories.id = articles.category_id +SQL +connection.exec query do |result| + result.each do |row| + post_contents = <<~POST + --- + layout: post + POST + + date = row['created_at'].gsub /\.[[:digit:]]+$/, '' # Remove milliseconds from the value. + post_contents << "date: #{date}\n" + category = row['name'] + post_contents << "tags: #{category}\n" + + if row['title'].include? '"' + post_contents << "title: |\n #{row['title']}\n" + else + post_contents << "title: #{row['title']}\n" + end + + post_contents << "teaser:\n #{row['teaser']}\n" unless row['teaser'].nil? + post_contents << "---\n" + post_contents << "#{row['description'].delete("\r")}\n" + + post_path = (Pathname.new('posts') + row['slug']).sub_ext('.html') + + post_path.dirname.mkpath + post_path.write post_contents + end +end |
