Module:Utils

From Eli's Software Encyclopedia
Revision as of 15:15, August 19, 2025 by WikiVisor (talk | contribs)

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)
  local authors = M.splitAndTrim(rawAuthors)
  local authorLabel = (#authors > 1) and 'Authors' or 'Author'
  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