]> git.draconx.ca Git - homepage.git/blobdiff - tools/weblog-update.rb
Let's start a blog!
[homepage.git] / tools / weblog-update.rb
diff --git a/tools/weblog-update.rb b/tools/weblog-update.rb
new file mode 100755 (executable)
index 0000000..f955f24
--- /dev/null
@@ -0,0 +1,62 @@
+#!/usr/bin/env ruby
+#
+# Nick's web site: Autogenerate timestamps for nanoc items.
+#
+# Copyright © 2020 Nick Bowler
+#
+# 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 <https://www.gnu.org/licenses/>.
+
+require 'yaml'
+
+updatetime = Time.now
+
+content = STDIN.read
+if content =~ /\A---(--)?\s*$/
+    parts = content.split(/^---(--)?[ \t]*\r?\n?/, 3)
+    metadata = parts[1]
+    content = parts[2]
+end
+
+if metadata
+    meta = YAML.load(metadata)
+    timefmt = "%FT%T%z"
+    updatestr = updatetime.round.strftime(timefmt)
+    autoset = nil
+
+    if meta["published"] and meta["updated"].is_a?(Time)
+        s = meta["updated"].strftime(timefmt)
+        if metadata.sub!(/^updated:\s*#{s}\s*$/, "updated: " + updatestr)
+            autoset = "updated"
+        end
+    elsif meta["published"] and !meta["updated"]
+        metadata += "updated: " + updatestr
+        autoset = "updated"
+    elsif !meta["published"]
+        metadata += "published: " + updatestr
+        autoset = "published"
+    end
+
+    if autoset
+        # Revalidate YAML
+        meta = YAML.load(metadata)
+        unless meta[autoset] == updatetime.round
+            raise "failed to auto-insert " + autoset
+        end
+    end
+
+    puts("---")
+    puts(metadata)
+    puts("---")
+end
+puts(content)