timestamp (621B)
1 #!/usr/bin/env ruby 2 3 require "date" 4 5 abort "Usage: bin/timestamp [path/to/index.md]" if ARGV.length > 1 6 ts = DateTime.now.iso8601 7 8 if ARGV.empty? 9 IO.popen("pbcopy", "w") { |pb| pb.write(ts) } 10 else 11 path = ARGV.first 12 content = File.read(path) 13 frontmatter = content.match(/\A---\r?\n(.*?)\r?\n---(?:\r?\n|\z)/m) 14 abort "#{path}: expected YAML frontmatter with one date field" unless frontmatter && frontmatter[1].scan(/^date:[^\r\n]*/).length == 1 15 updated = frontmatter[0].sub(/^date:[^\r\n]*/, "date: #{ts}") 16 File.write(path, updated + content[frontmatter[0].length..-1]) 17 puts "Updated #{path}" 18 end 19 20 puts ts