]> git.draconx.ca Git - homepage.git/blob - tools/weblog-update.rb
Implement dark mode without using CSS variables.
[homepage.git] / tools / weblog-update.rb
1 #!/usr/bin/env ruby
2 #
3 # Nick's web site: Autogenerate timestamps for nanoc items.
4 #
5 # Copyright © 2020 Nick Bowler
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
20 require 'yaml'
21
22 updatetime = Time.now
23
24 content = STDIN.read
25 if content =~ /\A---(--)?\s*$/
26     parts = content.split(/^---(--)?[ \t]*\r?\n?/, 3)
27     metadata = parts[1]
28     content = parts[2]
29 end
30
31 if metadata
32     meta = YAML.load(metadata)
33     timefmt = "%FT%T%z"
34     updatestr = updatetime.round.strftime(timefmt)
35     autoset = nil
36
37     if meta["published"] and meta["updated"].is_a?(Time)
38         s = meta["updated"].strftime(timefmt)
39         if metadata.sub!(/^updated:\s*#{s}\s*$/, "updated: " + updatestr)
40             autoset = "updated"
41         end
42     elsif meta["published"] and !meta["updated"]
43         metadata += "updated: " + updatestr
44         autoset = "updated"
45     elsif !meta["published"]
46         metadata += "published: " + updatestr
47         autoset = "published"
48     end
49
50     if autoset
51         # Revalidate YAML
52         meta = YAML.load(metadata)
53         unless meta[autoset] == updatetime.round
54             raise "failed to auto-insert " + autoset
55         end
56     end
57
58     puts("---")
59     puts(metadata)
60     puts("---")
61 end
62 puts(content)