Module:PsychoanalyticOrganization

From No Subject
Jump to navigation Jump to search

Documentation for this module may be created at Module:PsychoanalyticOrganization/doc

-- Module:PsychoanalyticOrganization
-- Lua-backed infobox for psychoanalytic organizations
-- Supports canonical data via Module:PsychoanalyticOrganization/data
-- Page parameters override data-module values

local p = {}

-- =========================
-- Utility functions
-- =========================

-- Trim-safe existence check
local function hasValue(v)
	return v and mw.text.trim(v) ~= ""
end

-- Return first non-empty value: page arg → data → nil
local function getValue(args, data, key)
	if hasValue(args[key]) then
		return args[key]
	end
	if hasValue(data[key]) then
		return data[key]
	end
	return nil
end

-- Check if any field in a list has a value
local function anyValue(args, data, keys)
	for _, k in ipairs(keys) do
		if getValue(args, data, k) then
			return true
		end
	end
	return false
end

-- Render a label/value row
local function renderRow(label, value)
	if not hasValue(value) then
		return ""
	end
	return string.format("|-\n! %s\n| %s\n", label, value)
end

-- Render a full section with conditional header
local function renderSection(title, args, data, fields)
	if not anyValue(args, data, fields) then
		return ""
	end

	local out = {}
	table.insert(out, string.format(
		'|-\n! colspan="2" class="infobox-header" | %s\n',
		title
	))

	for _, field in ipairs(fields) do
		local value = getValue(args, data, field.key)
		table.insert(out, renderRow(field.label, value))
	end

	return table.concat(out)
end

-- =========================
-- Main entry point
-- =========================

function p.main(frame)
	local args = frame:getParent().args

	-- Load data module safely
	local dataModule = nil
	pcall(function()
		dataModule = require("Module:PsychoanalyticOrganization/data")
	end)

	local title = mw.title.getCurrentTitle().text
	local key = args.name or title
	local data = (dataModule and dataModule[key]) or {}

	local html = {}

	-- Open infobox
	table.insert(html,
		'{| class="infobox infobox-psychoanalytic-org" ' ..
		'style="width:22em; font-size:90%; line-height:1.35;"'
	)

	-- Title row
	local displayName = getValue(args, data, "name") or title
	table.insert(html, string.format(
		'|-\n! colspan="2" style="background:#5B92C5; color:white; ' ..
		'font-size:1.05em; padding:0.4em; text-align:center;" | %s\n',
		displayName
	))

	-- =========================
	-- Organization details
	-- =========================
	table.insert(html, renderSection(
		"Organization details",
		args,
		data,
		{ "type", "founded", "dissolved", "founder", "key_figures", "orientation" }
	))

	-- =========================
	-- Institutional context
	-- =========================
	table.insert(html, renderSection(
		"Institutional context",
		args,
		data,
		{ "predecessor", "successor", "affiliation", "ipa_relation" }
	))

	-- =========================
	-- Operations
	-- =========================
	table.insert(html, renderSection(
		"Operations",
		args,
		data,
		{ "hq", "scope", "training", "publications", "website" }
	))

	-- Close infobox
	table.insert(html, "|}")

	-- =========================
	-- Maintenance categories
	-- =========================
	-- Comment out if undesired

	if not getValue(args, data, "type") then
		table.insert(html,
			"[[Category:Psychoanalytic organizations missing type]]"
		)
	end

	if not getValue(args, data, "founded") then
		table.insert(html,
			"[[Category:Psychoanalytic organizations missing founding year]]"
		)
	end

	return table.concat(html)
end

return p