Module:Maskin: Difference between revisions
Appearance
Lua go brrr |
Hent bilde automatisk |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 61: | Line 61: | ||
{ "ipv6", "IPv6" }, | { "ipv6", "IPv6" }, | ||
{ "network_location", "Nettverkslokasjon" }, | { "network_location", "Nettverkslokasjon" }, | ||
{ "named_after", " | { "named_after", "Navneopphav" }, | ||
{ "origin", "Opphav" }, | { "origin", "Opphav" }, | ||
{ "date_in_service", "Satt i drift" }, | { "date_in_service", "Satt i drift" }, | ||
| Line 67: | Line 67: | ||
{ "predecessor", "Forgjenger", machine_links }, | { "predecessor", "Forgjenger", machine_links }, | ||
{ "successor", "Etterfølger", 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) | function arg(key, args) | ||
local v = mw.text.trim(args[key] or "") | 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 | end | ||
| Line 81: | Line 117: | ||
function p.infobox(frame) | function p.infobox(frame) | ||
local args = frame:getParent().args | 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({ | local style = table.concat({ | ||
"float:right", | "float:right", | ||
| Line 96: | Line 137: | ||
end | end | ||
end | end | ||
if #out == 1 then | |||
return "" | |||
end | |||
table.insert(out, "|}") | table.insert(out, "|}") | ||
return table.concat(out, "\n") | return table.concat(out, "\n") | ||
Latest revision as of 21:08, 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 ""
return "[[File:" .. v .. "|frameless|300px|" .. caption .. "]]<br>" .. caption
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 },
{ "status", "Status", status_label },
{ "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 ("! " .. label .. "\n|") or "| colspan=2 |"
local sep = value:find("^[*#]") and "\n" or " "
return "|-\n" .. cell .. sep .. value
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, "|}")
return table.concat(out, "\n")
end
return p