#!/usr/bin/env ruby # Generates hugo/data/methoden.json and hugo/data/varianten.json: creation # date, last-modified date, and a full chronological change history # (verlauf, newest first) per method/variant - all derived from the git # history of _content/. Replaces generate-dates.sh (same underlying reason # for reading git history ourselves instead of relying on Hugo's # enableGitInfo/.GitInfo, which doesn't see _content/'s separate git repo - # see the installation guide on the project website. No Hugo-native # "created" equivalent exists anyway, https://github.com/gohugoio/hugo/issues/11810). # # Recomputed fresh on every sync rather than maintained (see # sync-methoden.sh, which calls this script automatically) - no manual # date/history field, git stays the single source of truth (Manifest # Prinzip 8). require_relative 'lib/frontmatter' require 'json' ROOT = File.expand_path('..', __dir__) CONTENT_REPO = File.join(ROOT, '_content') # One combined "git log" call per file instead of one per commit - %x09 is # a literal tab byte as field separator, safe against contents containing # regular spaces/dashes. --name-only together with --follow gives the # historical path at each commit (needed to read that commit's content via # "git show", exactly the technique scripts/validate.rb's # vorherige_kennung already uses for the single most recent rename). # Oldest commit first (git log's default is newest-first, hence "reverse"). def commits_for(relpath, content_dir) raw = `cd #{content_dir} && git log --follow --format='%H%x09%an%x09%aI' --name-only -- #{relpath}` zeilen = raw.split("\n").reject(&:empty?) commits = [] i = 0 while i < zeilen.length hash, autor, datum = zeilen[i].split("\t") pfad = zeilen[i + 1] commits << { hash: hash, autor: autor, datum: datum, pfad: pfad } i += 2 end commits.reverse end def inhalt_bei(hash, pfad, content_dir) text = `cd #{content_dir} && git show #{hash}:#{pfad} 2>/dev/null`.force_encoding('UTF-8') return nil if text.empty? Frontmatter.parse(text) end # Compares frontmatter key-by-key (simple value inequality, including for # list-type fields - a reordered-but-unchanged list would show as # "changed", accepted as a rare, harmless false positive rather than # building element-level list diffing for this) plus the document body, for # variants where the actual content (Vorbereitung/Ablauf/Hintergründe) # lives. def geaenderte_felder(vorher_fm, vorher_body, jetzt_fm, jetzt_body) felder = (vorher_fm.keys | jetzt_fm.keys).select { |k| vorher_fm[k] != jetzt_fm[k] } felder << 'inhalt' if vorher_body != jetzt_body felder end def verlauf_fuer(relpath, content_dir) commits = commits_for(relpath, content_dir) return nil if commits.empty? eintraege = [] vorher_fm = nil vorher_body = nil commits.each do |c| parsed = inhalt_bei(c[:hash], c[:pfad], content_dir) next unless parsed fm, body = parsed if vorher_fm.nil? eintraege << { datum: c[:datum], autor: c[:autor], typ: 'erstellt', felder: [] } else felder = geaenderte_felder(vorher_fm, vorher_body, fm, body) eintraege << { datum: c[:datum], autor: c[:autor], typ: 'geaendert', felder: felder } unless felder.empty? end vorher_fm = fm vorher_body = body end return nil if eintraege.empty? eintraege = eintraege.reverse # newest first { erstellt: eintraege.last[:datum], geaendert: eintraege.first[:datum], verlauf: eintraege } end # Both methods and variants are one folder per entry with content in # index.md (Hugo page bundle). def generate_bundle_verlauf(name) dir = File.join(CONTENT_REPO, name) out = File.join(ROOT, 'hugo', 'data', "#{name}.json") require 'fileutils' FileUtils.mkdir_p(File.dirname(out)) entries = {} if Dir.exist?(dir) Dir.glob(File.join(dir, '*')).select { |d| File.directory?(d) }.each do |entry_dir| slug = File.basename(entry_dir) next unless File.exist?(File.join(entry_dir, 'index.md')) daten = verlauf_fuer("#{name}/#{slug}/index.md", CONTENT_REPO) entries[slug] = daten if daten end end File.write(out, JSON.pretty_generate(entries)) puts "#{name}.json aktualisiert: #{entries.length} Einträge" end generate_bundle_verlauf('methoden') generate_bundle_verlauf('varianten')