aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2025-09-06 22:06:27 +0200
committerEugen Wissner <belka@caraus.de>2025-09-06 22:08:12 +0200
commit61a9584fb67c026002953ef02046ca0f78575040 (patch)
treeee9c7ffcb51c5e5e6f144383bf85c959cd02e7ac /bin
parent0a575a9731979b546dd74bda0976d230ddb686da (diff)
downloadkazbek-61a9584fb67c026002953ef02046ca0f78575040.tar.gz
Add pg_jekyll script
Diffstat (limited to 'bin')
-rwxr-xr-xbin/pg_jekyll.rb43
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