#!/usr/bin/env ruby # Sets draft: true in the LOCAL copy of every method (_content/methoden/*/index.md) # whose variants are all still drafts (or has none) - not in the content repo # itself, only in the working copy freshly fetched on every sync (see sync-methoden.sh). # # Methods deliberately no longer have their own draft field # (admin/config.template.yml): publication is derived from the variants - as # soon as at least one variant is published, the method is too. This can't # be modeled as a Decap field (no access to other collections while saving), # hence this preprocessing step before the Hugo build. Hugo's own draft # mechanism then reliably takes care of the rest (e.g. automatic exclusion # from the homepage/search/taxonomies, not just the detail page itself) - # more robust than rebuilding a custom visibility check in the templates. # # Assumes apply-veroeffentlicht.rb has already run (see sync-methoden.sh) - # otherwise variants would still have "veroeffentlicht" instead of "draft", # published_methoden would stay empty, and EVERY method would be incorrectly # marked as draft without anything reporting the error. Hence this safeguard # here (Befund G/Schritt 6, see the project website (background/analysis notes)): instead of # silently accepting that, the script aborts loudly as soon as it finds a # variant with a still-untranslated "veroeffentlicht". require_relative 'lib/frontmatter' require 'set' root = File.expand_path('..', __dir__) content = File.join(root, '_content') methoden_dir = File.join(content, 'methoden') varianten_dir = File.join(content, 'varianten') published_methoden = Set.new if Dir.exist?(varianten_dir) Dir.glob(File.join(varianten_dir, '*', 'index.md')).each do |file| result = Frontmatter.read(file) next unless result fm, = result if fm.key?('veroeffentlicht') warn "compute-methoden-sichtbarkeit.rb: #{file} hat noch 'veroeffentlicht' statt 'draft' - apply-veroeffentlicht.rb muss vorher laufen (siehe sync-methoden.sh)" exit 1 end next unless fm['draft'] == false methode = fm['methode'] published_methoden << methode if methode end end changed = 0 Dir.glob(File.join(methoden_dir, '*', 'index.md')).each do |file| result = Frontmatter.read(file) next unless result fm, body = result kennung = fm['kennung'] should_be_visible = published_methoden.include?(kennung) # fm['draft'] == true instead of fm.key?('draft'): methods used to have # their own draft field (since removed, see admin/config.template.yml) - # so a file may still contain a leftover old "draft: false" value. # Checking only for the key's presence would incorrectly treat that as # "currently hidden". currently_draft = fm['draft'] == true if should_be_visible == currently_draft if should_be_visible fm.delete('draft') else fm['draft'] = true end Frontmatter.write(file, fm, body, feldreihenfolge: Frontmatter::METHODEN_FELDER) changed += 1 end end puts "Methoden-Sichtbarkeit aktualisiert: #{published_methoden.size} mit veröffentlichter Variante, #{changed} Datei(en) angepasst"