Jump to content

Module:Maskin: Difference between revisions

From Programvareverkstedet
Hent bilde automatisk
Fancy overskrift og bildetekst
 
Line 33: Line 33:
local function image_cell(v, args)
local function image_cell(v, args)
local caption = arg("caption", args) or ""
local caption = arg("caption", args) or ""
return "[[File:" .. v .. "|frameless|300px|" .. caption .. "]]<br>" .. caption
local text = caption ~= "" and '<div class="pvv-maskin-caption">' .. caption .. "</div>" or ""
return "[[File:" .. v .. "|frameless|600px|" .. caption .. "]]" .. text
end
end


Line 44: Line 45:
local FIELDS = {
local FIELDS = {
{ "image", nil, image_cell },
{ "image", nil, image_cell },
{ "status", "Status", status_label },
{ "role", "Funksjon" },
{ "role", "Funksjon" },
{ "form_factor", "Formfaktor", form_factor_label },
{ "form_factor", "Formfaktor", form_factor_label },
Line 111: Line 111:


local function table_row(label, value)
local function table_row(label, value)
local cell = label and ("! " .. label .. "\n|") or "| colspan=2 |"
local cell = label and ('! style="text-align:left" | ' .. label .. "\n|") or '| colspan=2 class="pvv-maskin-image" |'
local sep = value:find("^[*#]") and "\n" or " "
local sep = value:find("^[*#]") and "\n" or " "
return "|-\n" .. cell .. sep .. value
return "|-\n" .. cell .. sep .. value
end
local function header_row(host, status)
local head = mw.html.create("div")
:addClass("pvv-maskin-head pvv-status-" .. status)
head:tag("span"):addClass("pvv-maskin-name"):wikitext(host)
head:tag("span"):addClass("pvv-maskin-status"):wikitext(status_label(status))
return '|-\n! colspan=2 class="pvv-maskin-top" | ' .. tostring(head)
end
end


Line 137: Line 145:
end
end
end
end
 
if #out == 1 then
if #out == 1 then
    return ""
    return ""
end
end
 
table.insert(out, 2, header_row(host, arg("status", args) or "unknown"))
 
table.insert(out, "|}")
table.insert(out, "|}")
return table.concat(out, "\n")
local styles = frame:extensionTag("templatestyles", "", { src = frame:getParent():getTitle() .. "/styles.css" })
return styles .. "\n" .. table.concat(out, "\n")
end
end


return p
return p

Latest revision as of 23:30, 27 November 2026

Denne templaten lager en tabell med metadata om de forskjellige maskinene våre.

Templaten vil først og fremst se hva du spesifiserte da du kalte den, etterfulgt av å sjekke Module:Maskin/hosts.json. Dersom raden ikke står noen av stedene vil den hoppe over raden og prøve neste.


local p = {}

local STATUS = {
	live = "I drift",
	planned = "Planlagt",
	unknown = "Ukjent status",
	stored = "Lagret",
	retired = "Utfaset",
}

local FORM_FACTOR = {
	rack = "Rack",
	tower = "Tårn",
	vm = "Virtuell maskin",
	container = "Container",
	other = "Annet",
}

local function status_label(v)
	return STATUS[v] or v
end

local function form_factor_label(v, args)
	local units = arg("rack_units", args)
	local label = FORM_FACTOR[v] or v
	return units and label .. ", " .. units .. "U" or label
end

local function machine_links(v)
	return (v:gsub("[^%s*]+", "[[Maskiner/%0|%0]]"))
end

local function image_cell(v, args)
	local caption = arg("caption", args) or ""
	local text = caption ~= "" and '<div class="pvv-maskin-caption">' .. caption .. "</div>" or ""
	return "[[File:" .. v .. "|frameless|600px|" .. caption .. "]]" .. text
end

local function os_with_version(v, args)
	local version = arg("os_version", args)
	return version and v .. " " .. version or v
end

-- { arg, label , optional render function(value, args) }
local FIELDS = {
	{ "image", nil, image_cell },
	{ "role", "Funksjon" },
	{ "form_factor", "Formfaktor", form_factor_label },
	{ "os", "OS", os_with_version },
	{ "managed_by", "Konfigurasjon" },
	{ "host", "Kjører på" },
	{ "location", "Plassering" },
	{ "model", "Modell" },
	{ "cpu", "CPU" },
	{ "ram", "Minne" },
	{ "storage", "Lagring" },
	{ "gpu", "GPU" },
	{ "hardware_other", "Annen maskinvare" },
	{ "services", "Tjenester" },
	{ "ipv4", "IPv4" },
	{ "ipv6", "IPv6" },
	{ "network_location", "Nettverkslokasjon" },
	{ "named_after", "Navneopphav" },
	{ "origin", "Opphav" },
	{ "date_in_service", "Satt i drift" },
	{ "date_retired", "Utfaset" },
	{ "predecessor", "Forgjenger", machine_links },
	{ "successor", "Etterfølger", machine_links },
}

local DATA_PAGE = "Module:Maskin/hosts.json"

local function data_value(v)
	if type(v) == "table" then
		local items = {}
		for _, item in ipairs(v) do
			table.insert(items, "* " .. item)
		end
		return #items > 0 and table.concat(items, "\n") or nil
	end
	v = v ~= nil and mw.text.trim(tostring(v)) or ""
	return v ~= "" and v or nil
end

local IMAGE_EXTENSIONS = { "jpg", "jpeg", "png", "gif", "webp", "svg" }

local function host_image(host)
	for _, ext in ipairs(IMAGE_EXTENSIONS) do
		local name = host .. "." .. ext
		if mw.title.makeTitle("File", name).file.exists then
			return name
		end
	end
end

local FALLBACKS = {
	image = host_image,
}

function arg(key, args)
	local v = mw.text.trim(args.page[key] or "")
	if v ~= "" then
		return v
	end
	v = data_value(args.data[key])
	if v then
		return v
	end
	return FALLBACKS[key] and FALLBACKS[key](args.host)
end

local function table_row(label, value)
	local cell = label and ('! style="text-align:left" | ' .. label .. "\n|") or '| colspan=2 class="pvv-maskin-image" |'
	local sep = value:find("^[*#]") and "\n" or " "
	return "|-\n" .. cell .. sep .. value
end

local function header_row(host, status)
	local head = mw.html.create("div")
		:addClass("pvv-maskin-head pvv-status-" .. status)
	head:tag("span"):addClass("pvv-maskin-name"):wikitext(host)
	head:tag("span"):addClass("pvv-maskin-status"):wikitext(status_label(status))
	return '|-\n! colspan=2 class="pvv-maskin-top" | ' .. tostring(head)
end

function p.infobox(frame)
	local host = mw.title.getCurrentTitle().text:match("[^/]+$"):lower()
	local args = {
		host = host,
		page = frame:getParent().args,
		data = mw.loadJsonData(DATA_PAGE)[host] or {},
	}
	local style = table.concat({
		"float:right",
		"clear:right",
		"width:22em",
		"margin:0 0 1em 1em",
	}, "; ")
	local out = { '{| class="wikitable" style="' .. style .. '"' }

	for _, f in ipairs(FIELDS) do
		local v = arg(f[1], args)
		if v then
			table.insert(out, table_row(f[2], f[3] and f[3](v, args) or v))
		end
	end

	if #out == 1 then
	    return ""
	end

	table.insert(out, 2, header_row(host, arg("status", args) or "unknown"))

	table.insert(out, "|}")
	local styles = frame:extensionTag("templatestyles", "", { src = frame:getParent():getTitle() .. "/styles.css" })
	return styles .. "\n" .. table.concat(out, "\n")
end

return p