Module:Maskin: Difference between revisions
Appearance
m Finere typekommentar |
Bedre håndtering av flere forgjengere og etterkommere |
||
| Line 28: | Line 28: | ||
local function machine_links(v) | local function machine_links(v) | ||
local machine_bullet_regex = "(\n[*%s]*)([A-Za-z0-9_-]+)" | |||
local function link(prefix, name) | |||
local page = name:gsub("^%l", string.upper) | |||
return prefix .. "[[Maskiner/" .. page .. "|" .. name .. "]]" | |||
end | |||
local links = ("\n" .. v):gsub(machine_bullet_regex, link) | |||
return links:sub(2) | |||
end | |||
local function machine_count(v) | |||
return select(2, v:gsub("[^\n]*%S[^\n]*", "")) | |||
end | end | ||
| Line 42: | Line 52: | ||
end | end | ||
-- { arg, label , optional function(value, args) } | { heading | -- { | ||
-- arg : string, | |||
-- label : optional string, | |||
-- render : optional function(value, args) -> string, | |||
-- plural : optional string, | |||
-- } | { | |||
-- heading : string | |||
-- } | |||
local FIELDS = { | local FIELDS = { | ||
{ "image", nil, image_cell }, | { "image", nil, image_cell }, | ||
| Line 69: | Line 86: | ||
{ "date_in_service", "Satt i drift" }, | { "date_in_service", "Satt i drift" }, | ||
{ "date_retired", "Utfaset" }, | { "date_retired", "Utfaset" }, | ||
{ "predecessor", "Forgjenger", machine_links }, | { "predecessor", "Forgjenger", machine_links, plural = "Forgjengere" }, | ||
{ "successor", "Etterfølger", machine_links }, | { "successor", "Etterfølger", machine_links, plural = "Etterfølgere" }, | ||
} | } | ||
| Line 146: | Line 163: | ||
"margin:0 0 1em 1em", | "margin:0 0 1em 1em", | ||
}, "; ") | }, "; ") | ||
local out = { '{| class="wikitable" style="' .. style .. '"' } | local out = { '{| class="wikitable pvv-maskin" style="' .. style .. '"' } | ||
local status = arg("status", args) | local status = arg("status", args) | ||
| Line 160: | Line 177: | ||
heading = nil | heading = nil | ||
end | end | ||
table.insert(out, table_row( | local label = f.plural and machine_count(v) > 1 and f.plural or f[2] | ||
table.insert(out, table_row(label, f[3] and f[3](v, args) or v)) | |||
end | end | ||
end | end | ||
Revision as of 13:57, 28 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)
local machine_bullet_regex = "(\n[*%s]*)([A-Za-z0-9_-]+)"
local function link(prefix, name)
local page = name:gsub("^%l", string.upper)
return prefix .. "[[Maskiner/" .. page .. "|" .. name .. "]]"
end
local links = ("\n" .. v):gsub(machine_bullet_regex, link)
return links:sub(2)
end
local function machine_count(v)
return select(2, v:gsub("[^\n]*%S[^\n]*", ""))
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 : string,
-- label : optional string,
-- render : optional function(value, args) -> string,
-- plural : optional string,
-- } | {
-- heading : string
-- }
local FIELDS = {
{ "image", nil, image_cell },
{ heading = "Generelt" },
{ "role", "Funksjon" },
{ "form_factor", "Formfaktor", form_factor_label },
{ "os", "OS", os_with_version },
{ "managed_by", "Konfigurasjon" },
{ "host", "Kjører på" },
{ "location", "Plassering" },
{ heading = "Maskinvare" },
{ "model", "Modell" },
{ "cpu", "CPU" },
{ "ram", "Minne" },
{ "storage", "Lagring" },
{ "gpu", "GPU" },
{ "hardware_other", "Annen maskinvare" },
{ heading = "Nettverk" },
{ "services", "Tjenester" },
{ "ipv4", "IPv4" },
{ "ipv6", "IPv6" },
{ "network_location", "Nettverkslokasjon" },
{ heading = "Livsløp" },
{ "named_after", "Navneopphav" },
{ "origin", "Opphav" },
{ "date_in_service", "Satt i drift" },
{ "date_retired", "Utfaset" },
{ "predecessor", "Forgjenger", machine_links, plural = "Forgjengere" },
{ "successor", "Etterfølger", machine_links, plural = "Etterfølgere" },
}
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
local function section_row(heading, status)
return '|-\n! colspan=2 class="pvv-maskin-section pvv-status-' .. status .. '" | ' .. heading
end
function p.infobox(frame)
local hostname = mw.text.trim(frame:getParent().args.hostname or "")
local host = (hostname ~= "" and hostname or 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 pvv-maskin" style="' .. style .. '"' }
local status = arg("status", args)
local heading
for _, f in ipairs(FIELDS) do
if f.heading then
heading = f.heading
else
local v = arg(f[1], args)
if v then
if heading then
table.insert(out, section_row(heading, status or "unknown"))
heading = nil
end
local label = f.plural and machine_count(v) > 1 and f.plural or f[2]
table.insert(out, table_row(label, f[3] and f[3](v, args) or v))
end
end
end
if #out == 1 and not status then
return ""
end
table.insert(out, 2, header_row(host, status 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