Modul:Rezepte

Aus Dreamlight Valley Wiki

Die Dokumentation für dieses Modul kann unter Modul:Rezepte/Doku erstellt werden

local p = {}

-- Map: Zutat-Kategorie -> Rezept-Kategorie
local CAT_MAP = {
  ["Gemüse"] = "Gericht mit Gemüse",
  ["Fleisch"] = "Gericht mit Fleisch",
  ["Fisch"] = "Gericht mit Fisch",
  ["Meeresfrüchte"] = "Gericht mit Meeresfrüchten",
  ["Frucht"] = "Gericht mit Früchten",
  ["Getreide"] = "Gericht mit Getreide",
  ["Gewürz"] = "Gericht mit Gewürzen",
  ["Protein"] = "Gericht mit Protein",
  ["Süß"] = "Gericht mit süßen Zutaten",
  ["Gefroren"] = "Gericht mit gefrorenen Zutaten",
}

local function trim(s)
  return (s or ""):gsub("^%s+", ""):gsub("%s+$", "")
end

-- Extrahiert Link-Ziele aus gerendertem Wikitext: [[Aubergine]] / [[Aubergine|...]]
-- Filtert Datei:, Kategorie: usw. raus.
local function extract_links(wikitext)
  local seen, out = {}, {}
  if not wikitext then return out end

  for target in wikitext:gmatch("%[%[([^%]|#]+)") do
    target = trim(target)

    if target ~= ""
      and not target:match("^[Dd]atei:")
      and not target:match("^[Ff]ile:")
      and not target:match("^[Kk]ategorie:")
      and not target:match("^[Cc]ategory:")
    then
      if not seen[target] then
        seen[target] = true
        table.insert(out, target)
      end
    end
  end

  return out
end

-- Liest Kategorien aus dem Wikitext einer Seite (direkte [[Kategorie:...]] im Quelltext)
local function extract_categories(wikitext)
  local cats = {}
  if not wikitext then return cats end

  for cat in wikitext:gmatch("%[%[%s*[Kk]ategorie%s*:%s*([^%]|]+)") do
    cats[trim(cat)] = true
  end
  for cat in wikitext:gmatch("%[%[%s*[Cc]ategory%s*:%s*([^%]|]+)") do
    cats[trim(cat)] = true
  end

  return cats
end

local function current_is_template_namespace()
  local t = mw.title.getCurrentTitle()
  return t and (t.nsText == "Vorlage" or t.namespace == 10)
end

-- Debug: zeigt nur, welche Zutaten erkannt werden (setzt KEINE Kategorien)
function p.debug(frame)
local args = frame.args
if not args.rezept and frame:getParent() then
  args = frame:getParent().args
end
  local rezeptname = args.rezept or args[1] or ""
  if rezeptname == "" then
    return "Kein rezept= übergeben."
  end

  local rendered = frame:expandTemplate{ title = "rezept", args = { rezeptname } }
  local ingredients = extract_links(rendered)

  if #ingredients == 0 then
    return "Keine Zutaten-Links erkannt. Prüfe, ob {{name|...}} wirklich Wikilinks rendert."
  end

  return "Erkannte Zutaten: " .. table.concat(ingredients, ", ")
end

-- cats: setzt Kategorien anhand Zutatenseiten-Kategorien
function p.cats(frame)
  if current_is_template_namespace() then
    return "" -- niemals die Vorlage selbst kategorisieren
  end

  local args = frame:getParent() and frame:getParent().args or frame.args
  local rezeptname = args.rezept or args[1] or ""
  if rezeptname == "" then
    return ""
  end

  local rendered = frame:expandTemplate{ title = "rezept", args = { rezeptname } }
  local ingredients = extract_links(rendered)

  local recipeCats = {}
  for _, ing in ipairs(ingredients) do
    local title = mw.title.new(ing)
    if title and title.exists then
      local content = title:getContent()
      local ingCats = extract_categories(content)

      for ingCat, _ in pairs(ingCats) do
        local mapped = CAT_MAP[ingCat]
        if mapped then
          recipeCats[mapped] = true
        end
      end
    end
  end

  local out = {}
  for catName, _ in pairs(recipeCats) do
    table.insert(out, string.format("[[Kategorie:%s]]", catName))
  end

  return table.concat(out, "")
end

return p