Module:Utils

From Eli's Software Encyclopedia
Revision as of 10:38, August 20, 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, 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 the URL from the external link
--------------------------------------------------------------------
function M.getUrlFromLink(externalLink)
	if not externalLink or type(externalLink) ~= "string" then
        return nil
    end
    local trimmed = (externalLink:gsub("^%s+", ""):gsub("%s+$", ""))
    if trimmed:match("^https?://") then
        return trimmed
    end
    local url = trimmed:match("^%[(%S+)%s+(.-)%]%s*$")
    return url
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

--------------------------------------------------------------------
-- Utility to unbracket wiki links
--------------------------------------------------------------------
function M.unwrapLink(text)
    text = mw.text.trim(text)
    -- Case 1: Links with a pipe [[Target|Label]]
    local label = text:match("%[%[[^|]+|([^%]]+)%]%]")
    if label then
        return label
    end
    -- Case 2: Links without a pipe [[Target]]
    local target = text:match("%[%[([^%]]+)%]%]")
    if target then
        -- Strip leading ":" and namespace like "Category:"
        target = target:gsub("^:", ""):gsub("^[^:]+:", "")
        return target
    end
    -- Fallback: return as-is
    return text
end

function M.unwrapList(input)
    local results = {}
    -- Split on commas
    for part in mw.text.gsplit(input, ",") do
        table.insert(results, unwrapLink(part))
    end
    return results
end


return M