Zum Inhalt springen

Modul:Dokumentation

Aus Wikonia

Zweck[Quelltext bearbeiten]

Dieses Modul steuert die gesamte Ausgabe der zentralen Vorlage {{Vorlage:Dokumentation}} im Wikonia-Wiki.

Es zeigt automatisch den Dokumentationskopf, Hinweisboxen, die eigentliche Dokumentation (aus der gleichnamigen Unterseite /Doku) sowie ggf. einen <templatedata>-Block.

Verwendung[Quelltext bearbeiten]

Das Modul wird nicht direkt eingebunden, sondern über die Vorlage:

{{Dokumentation
 | lua = 1
 | css = 1
 | subst = kann
 | risiko = warnung
}}

Unterstützte Parameter[Quelltext bearbeiten]

Parameter Wirkung Mögliche Werte
lua Zeigt Lua-Hinweisbox an 1 (Standardtext)
oder Liste: Modul:Foo, Modul:Bar
css Zeigt CSS-Hinweisbox an 1
subst Zeigt Hinweis zur Substitution kann, soll, muss, darf nicht
risiko Zeigt Hinweis auf Änderungsrisiko none, achtung, kritisch

Automatische Funktionen[Quelltext bearbeiten]

  • Bindet /Doku automatisch ein, sofern vorhanden
  • Zeigt <templatedata>-Block, wenn vorhanden
  • Fügt Kategorien automatisch hinzu:

Hauptseite: Vorlage mit Dokumentation / Modul mit Dokumentation /Doku: Vorlagendokumentation / Moduldokumentation

Hinweis[Quelltext bearbeiten]

Dieses Modul ersetzt eine Vielzahl an Hilfskonstruktionen aus älteren Systemen. Die dazugehörige Vorlage {{Vorlage:Dokumentation}} sollte immer verwendet werden.


-- Modul:Dokumentation – Version für Wikonia (2025)
-- Entwickelt nach Analyse der enwiki-Version, angepasst auf Wikonia-Struktur
-- Modul:Dokumentation – FIXED: /Doku-Einbindung robust und dummheitsresistent™
local p = {}
local getArgs = require('Module:Arguments').getArgs

local function pageExists(title)
  local t = mw.title.new(title)
  return t and t.exists
end

local function isDocSubpage(title)
  return title.text:match("/Doku$") ~= nil
end

local function hasTemplateData(docTitle)
  if not docTitle or not docTitle.exists then return false end
  local content = docTitle:getContent()
  return content and content:find("<templatedata>", 1, true) ~= nil
end

local function makeIcon(icon)
  return string.format('[[Datei:%s|40px|class=wikonia-doku-icon]]', icon)
end

local function makeHeader(title, icon, links)
  -- icon kann entweder ein Dateiname (z. B. "Documentation-plain-black.svg")
  -- oder bereits geparster Wikitext/HTML sein (z. B. frame:preprocess('[[Datei:...]]')).
  local iconHtml = ""
  if icon and icon ~= "" then
    local iconStr = tostring(icon)
    -- Wenn iconStr bereits Parser-/HTML-Ausgabe enthält, verwenden wir es direkt.
    if iconStr:find("%[%[") or iconStr:find("<img") or iconStr:find('typeof="mw:File"') then
      iconHtml = iconStr
    else
      -- ansonsten ist es ein Dateiname -> makeIcon erzeugt den Wikitext
      iconHtml = makeIcon(iconStr)
    end
  end

  local linksText
  if type(links) == "table" then
    linksText = table.concat(links, " | ")
  else
    linksText = tostring(links or "")
  end

  return string.format(
    '<div class="wikonia-doku-header">%s <strong>%s</strong> <span class="wikonia-doku-links">%s</span></div>',
    iconHtml,
    title,
    linksText
  )
end


local function makeCategory(cat)
  return string.format('[[Kategorie:%s]]', cat)
end

function p.main(frame)
  local args = getArgs(frame)
  local title = mw.title.getCurrentTitle()
  local ns = title.namespace
  local fullpagename = title.fullText

  local isTemplate = ns == 10
  local isModule = ns == 828
  local isDocPage = isDocSubpage(title)

  local baseName = title.text:gsub("/Doku$", "")
  local baseTitle = mw.title.makeTitle(ns, baseName)
  local dokuPageName = baseTitle.prefixedText .. "/Doku"

  local out = {}

  -- Kategorieverwaltung für /Doku-Seiten
  if isDocPage then
    if isTemplate then table.insert(out, makeCategory("Vorlagendokumentation")) end
    if isModule then table.insert(out, makeCategory("Moduldokumentation")) end
    return table.concat(out, "\n")
  end

  -- Warnung bei falschem Namensraum
  if not (isTemplate or isModule) then
    table.insert(out, '<div class="wikonia-doku-warnung">Diese Vorlage ist nur für Seiten im Namensraum „Vorlage“ oder „Modul“ gedacht.</div>')
    return table.concat(out, "\n")
  end

-- Headerbox mit Icon und funktionierenden Links (ohne extern-Icon)
local icon = isTemplate and "Documentation-plain-black.svg" or "Module-info.svg"
local docTitle = mw.title.new(baseTitle.fullText .. "/Doku")

-- Plainlinks-Wrapper: verhindert extern-Icons bei echten URLs
local headLinks = {
    string.format('[%s ansehen]', docTitle:fullUrl()),
    string.format('[%s bearbeiten]', docTitle:fullUrl('action=edit')),
    string.format('[%s Versionsverlauf]', docTitle:fullUrl('action=history')),
    string.format('[%s Cache leeren]', docTitle:fullUrl('action=purge'))
}
 
-- Umhüllen in <span class="plainlinks">...</span>
local linkBlock = '<span class="plainlinks">' .. table.concat(headLinks, ' ‖ ') .. '</span>'

local iconMarkup = string.format('[[Datei:%s|40px|class=wikonia-doku-icon|link=]]', icon)
local iconParsed = frame:preprocess(iconMarkup)  -- <--- HIER: zwingend parsen
table.insert(out, makeHeader("Dokumentation", iconParsed, { linkBlock }))



  -- Hinweisboxen (rechtsbündig, gesammelt)
  local boxes = {}
  table.insert(boxes, frame:preprocess("{{Versionsschutz}}"))

  if args["lua"] then
    if args["lua"] == "1" then
      table.insert(boxes, frame:preprocess("{{Dokumentation/Lua}}"))
    else
      table.insert(boxes, frame:preprocess(string.format("{{Dokumentation/Lua|luamodule=%s}}", args["lua"])))
    end
  end

  if args["css"] then
    table.insert(boxes, frame:preprocess("{{Dokumentation/CSS}}"))
  end

  if args["subst"] then
    table.insert(boxes, frame:preprocess(string.format("{{Subst|%s}}", args["subst"])))
  end

  if args["risiko"] then
    table.insert(boxes, frame:preprocess(string.format("{{Änderungsrisiko|%s}}", args["risiko"])))
  end
  
  if args["protected"] then
  	if args["protected"] == "1" then
  		table.insert(boxes, frame:preprocess(string.format("{{Versionsschutz|Programmierschutz}}")))
  	else
  		table.insert(boxes, frame:preprocess(string.format("{{Versionsschutz|%s}}", args["protected"])))
  	end
  end

  if #boxes > 0 then
    table.insert(out, '<div class="wikonia-doku-float">' .. table.concat(boxes, "\n") .. '</div>')
  end

  -- Einbindung der Doku-Seite (robust)
  if pageExists(dokuPageName) then
    table.insert(out, frame:preprocess(string.format("{{%s}}", dokuPageName)))
  else
    table.insert(out, '<div class="wikonia-doku-warnung">Diese Seite hat noch keine Dokumentation.</div>')
  end

  -- TemplateData (sofern vorhanden)
  if hasTemplateData(mw.title.new(dokuPageName)) then
    table.insert(out, '<templatedata/>')
  end

  -- Kategorisierung Hauptseite
  if isTemplate then table.insert(out, makeCategory("Vorlage mit Dokumentation")) end
  if isModule then table.insert(out, makeCategory("Modul mit Dokumentation")) end

  return table.concat(out, "\n")
end

return p