Module:Utils: Difference between revisions
From Eli's Software Encyclopedia
mNo edit summary |
mNo edit summary |
||
| Line 48: | Line 48: | ||
tr:tag('td'):wikitext(value):done() | tr:tag('td'):wikitext(value):done() | ||
return tr | return tr | ||
end | |||
-------------------------------------------------------------------- | |||
-- Utility to retrieve file name and alt text from the file link syntax. | |||
-------------------------------------------------------------------- | |||
function M.parseFileLink(raw) | |||
local inner = raw:match("%[%[(%w+:%s*[^%]]+)%]%]") | |||
if not inner then return nil, nil end | |||
local parts = {} | |||
for part in inner:gmatch("[^|]+") do | |||
table.insert(parts, part) | |||
end | |||
local imageFilename = nil | |||
for _, p in ipairs(parts) do | |||
if p:find("^%w+:%s*") then -- starts with "Image:" or "File:" | |||
imageFilename = p:gsub("^%w+:%s*", "") -- strip keyword | |||
break | |||
end | |||
end | |||
if not imageFilename then return nil, nil end | |||
local imageAlt = nil | |||
for _, p in ipairs(parts) do | |||
if not p:find("=") and p ~= "" then -- unnamed & non‑empty | |||
imageAlt = p | |||
break | |||
end | |||
end | |||
return imageFilename, imageAlt | |||
end | end | ||
return M | return M | ||
Revision as of 15:49, August 19, 2025
Documentation for this module may be created at Module:Utils/doc
local M = {}
--------------------------------------------------------------------
-- Utility that splits a comma‑separated string and trims spaces
--------------------------------------------------------------------
function M.splitAndTrim(raw)
local t = {}
for part in raw:gmatch("[^,]+") do
part = part:match("^%s*(.-)%s*$")
if part ~= '' then
table.insert(t, part)
end
end
return t
end
--------------------------------------------------------------------
-- Build a human‑readable author list (label + wikilink string)
--------------------------------------------------------------------
function M.buildAuthorOutput(rawAuthors, labelSingle, labelMulti)
local authors = M.splitAndTrim(rawAuthors)
local authorLabel = (#authors > 1) and labelMulti or labelSingle
local linkParts = {}
for _, name in ipairs(authors) do
table.insert(linkParts, string.format('<div>[[%s]]</div>', name))
end
local authorLinks = table.concat(linkParts, '')
return authorLabel, authorLinks
end
--------------------------------------------------------------------
-- Return a <tr> that contains the author label/value pair
--------------------------------------------------------------------
function M.authorRow(authorLabel, authorLinks)
local tr = mw.html.create('tr'):addClass('data-row')
tr:tag('td'):wikitext(authorLabel):done()
tr:tag('td'):wikitext(authorLinks):done()
return tr
end
--------------------------------------------------------------------
-- Return a <tr> that contains any generic label/value pair
--------------------------------------------------------------------
function M.addLabelValue(label, value)
if not value or value == '' then return nil end
local tr = mw.html.create('tr'):addClass('data-row')
tr:tag('td'):wikitext(label):done()
tr:tag('td'):wikitext(value):done()
return tr
end
--------------------------------------------------------------------
-- Utility to retrieve file name and alt text from the file link syntax.
--------------------------------------------------------------------
function M.parseFileLink(raw)
local inner = raw:match("%[%[(%w+:%s*[^%]]+)%]%]")
if not inner then return nil, nil end
local parts = {}
for part in inner:gmatch("[^|]+") do
table.insert(parts, part)
end
local imageFilename = nil
for _, p in ipairs(parts) do
if p:find("^%w+:%s*") then -- starts with "Image:" or "File:"
imageFilename = p:gsub("^%w+:%s*", "") -- strip keyword
break
end
end
if not imageFilename then return nil, nil end
local imageAlt = nil
for _, p in ipairs(parts) do
if not p:find("=") and p ~= "" then -- unnamed & non‑empty
imageAlt = p
break
end
end
return imageFilename, imageAlt
end
return M
