#!/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 . 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, permitted_classes: [Time]) 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, permitted_classes: [Time]) unless meta[autoset] == updatetime.round raise "failed to auto-insert " + autoset end end puts("---") puts(metadata) puts("---") end puts(content)