Module:EveryCRTImages

Revision as of 00:59, 15 December 2025 by Arclight (talk | contribs)

Documentation for this module may be created at Module:EveryCRTImages/doc

local p = {}

local function trim(s)
  return (s:gsub("^%s+", ""):gsub("%s+$", ""))
end

local function normalize_file_title(s)
  s = trim(s or "")
  if s == "" then return nil end

  -- If user pasted "File:Something.jpg", drop the prefix and re-add consistently
  s = s:gsub("^%s*[Ff]ile%s*:%s*", "")

  -- MediaWiki titles prefer underscores internally; spaces usually work too,
  -- but underscores avoid edge cases in SMW "Page" values.
  s = s:gsub("%s+", "_")

  return "File:" .. s
end

local function split_csv(s)
  local out = {}
  if not s or s == "" then return out end
  for part in tostring(s):gmatch("([^,]+)") do
    table.insert(out, part)
  end
  return out
end

-- Emits SMW annotations for a multi-image list:
-- [[Has images::File:...]] repeated
function p.annotate(frame)
  local args = frame:getParent().args
  local prop = args.prop or "Has images"
  local list = args.images or ""

  local bits = {}
  for _, raw in ipairs(split_csv(list)) do
    local title = normalize_file_title(raw)
    if title then
      table.insert(bits, string.format("[[%s::%s]]", prop, title))
    end
  end

  return table.concat(bits, "\n")
end

-- Renders a gallery from the list (optional)
function p.gallery(frame)
  local args = frame:getParent().args
  local list = args.images or ""
  local mode = args.mode or "packed"
  local heights = args.heights or "160"

  local lines = {}
  for _, raw in ipairs(split_csv(list)) do
    local title = normalize_file_title(raw)
    if title then
      table.insert(lines, title)
    end
  end

  if #lines == 0 then return "" end

  local wikitext = string.format(
    '<gallery mode="%s" heights="%s">\n%s\n</gallery>',
    mode, heights, table.concat(lines, "\n")
  )

  return frame:preprocess(wikitext)
end

return p
MediaWiki Appliance - Powered by TurnKey Linux