40 lines
1.4 KiB
Ruby
Executable File
40 lines
1.4 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
||
# frozen_string_literal: true
|
||
|
||
require "time"
|
||
require_relative "schema"
|
||
|
||
ENV["TZ"] = "Europe/Paris"
|
||
ROOT = File.expand_path("..", __dir__)
|
||
CONTENT = File.join(ROOT, "src", "content")
|
||
|
||
begin
|
||
raise "un seul chemin de fiche est requis" unless ARGV.length == 1
|
||
value = ARGV.first
|
||
base = value.start_with?("src/content/") ? ROOT : CONTENT
|
||
path = File.realpath(File.expand_path(value, base))
|
||
relative = path.delete_prefix(CONTENT + "/")
|
||
parts = relative.split("/")
|
||
|
||
valid = path.start_with?(CONTENT + "/") && File.file?(path) && File.extname(path) == ".md" &&
|
||
File.basename(path) != "_index.md" && parts.length > 1 && CONTENT_SECTIONS.include?(parts.first)
|
||
raise "le chemin doit désigner une fiche d’une section connue" unless valid
|
||
|
||
text = File.read(path, encoding: "UTF-8")
|
||
front = text.match(/\A\+\+\+\r?\n(.*?)\r?\n\+\+\+(?:\r?\n|\z)/m)
|
||
raise "front matter TOML délimité par +++ absent" unless front
|
||
|
||
matches = front[1].to_enum(:scan, UPDATED_RE).map { Regexp.last_match }
|
||
raise "la fiche doit contenir exactement une ligne updated" unless matches.length == 1
|
||
|
||
timestamp = Time.now.iso8601
|
||
body = front[1].sub(UPDATED_RE, "updated = #{timestamp}")
|
||
File.write(path, text.sub(front[1], body), encoding: "UTF-8")
|
||
|
||
puts "Fiche mise à jour : #{path.delete_prefix(ROOT + '/')}"
|
||
puts "updated = #{timestamp}"
|
||
rescue StandardError => e
|
||
warn "Erreur : #{e.message}"
|
||
exit 1
|
||
end
|