Module:Utils: Difference between revisions

From Eli's Software Encyclopedia
mNo edit summary
mNo edit summary
Line 18: Line 18:
-- Build a human‑readable author list (label + wikilink string)
-- Build a human‑readable author list (label + wikilink string)
--------------------------------------------------------------------
--------------------------------------------------------------------
function M.buildAuthorOutput(rawAuthors)
function M.buildAuthorOutput(rawAuthors, labelSingle, labelMulti)
   local authors = M.splitAndTrim(rawAuthors)
   local authors = M.splitAndTrim(rawAuthors)
   local authorLabel = (#authors > 1) and 'Authors' or 'Author'
   local authorLabel = (#authors > 1) and labelMulti or labelSingle
   local linkParts = {}
   local linkParts = {}
   for _, name in ipairs(authors) do
   for _, name in ipairs(authors) do

Revision as of 15:18, 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

return M