Files
kazbek/bin/pg_jekyll.rb
2025-09-06 22:08:12 +02:00

44 lines
1.2 KiB
Ruby
Executable File

#!/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