Fix: read images from frame.args (direct #invoke params) not frame:getParent().args; same for recallGallery mode/heights (via update-page on MediaWiki MCP Server)
Update comments to reference {{EveryCRT gallery}} instead of {{CRT gallery}}/{{CRT model}}; change fallback default mode to nolines (via update-page on MediaWiki MCP Server)
 
(One intermediate revision by the same user not shown)
Line 40: Line 40:
end
end


-- Module-level storage: persists across all #invoke calls within one page parse
-- Extract the |images= value from the current page's wikitext.
local storedImages = nil
-- Reads the raw source via mw.title:getContent() and parses out
-- the images parameter from the page's content template invocation
-- (works with {{CRT model}}, {{AV device}}, or any template with an images= field).
local function getImagesFromPageSource()
  local title = mw.title.getCurrentTitle()
  local content = title:getContent()
  if not content then return nil end


-- Store the images list for later retrieval by recallGallery.
  -- Match |images= value: capture everything after |images= up to the next
-- Called by {{CRT model}} when suppress_gallery is set.
  -- pipe-prefixed parameter (|paramname=) or closing braces (}})
-- Outputs nothing.
   local images = content:match("|images%s*=%s*([^|}]+)")
function p.storeImages(frame)
  if images then
   storedImages = getArg(frame, "images", "")
    return trim(images)
   return ""
  end
   return nil
end
end


-- Render a gallery using the images previously stored by storeImages.
-- Auto-gallery: reads the current page's wikitext to find the images= field
-- Called by {{CRT gallery}}.
-- from the page's content template, then renders a gallery. No parameters required.
function p.recallGallery(frame)
-- Called by {{EveryCRT gallery}}.
   if not storedImages or storedImages == "" then
function p.autoGallery(frame)
     return ""
  -- Allow explicit override via images= parameter
  local imageList = getArg(frame, "images", "")
 
  -- If no explicit images, read from page source
   if imageList == "" then
     imageList = getImagesFromPageSource() or ""
   end
   end


   local mode = getArg(frame, "mode", "packed")
  if imageList == "" then return "" end
 
   local mode = getArg(frame, "mode", "nolines")
   local heights = getArg(frame, "heights", "160")
   local heights = getArg(frame, "heights", "160")


   local lines = {}
   local lines = {}
   for _, raw in ipairs(split_csv(storedImages)) do
   for _, raw in ipairs(split_csv(imageList)) do
     local title = normalize_file_title(raw)
     local title = normalize_file_title(raw)
     if title then
     if title then
Line 81: Line 95:
-- Emits SMW annotations for a multi-image list:
-- Emits SMW annotations for a multi-image list:
-- [[Has images::File:...]] repeated
-- [[Has images::File:...]] repeated
-- Called by {{CRT model}} and {{AV device}}.
function p.annotate(frame)
function p.annotate(frame)
   local args = frame:getParent().args
   local args = frame:getParent().args
Line 97: Line 112:
end
end


-- Renders a gallery from the list (optional)
-- Renders a gallery from a comma-separated image list.
-- Called by {{CRT model}} and {{AV device}} for their default inline gallery.
function p.gallery(frame)
function p.gallery(frame)
   local args = frame:getParent().args
   local args = frame:getParent().args
   local list = args.images or ""
   local list = args.images or ""
   local mode = args.mode or "packed"
   local mode = args.mode or "nolines"
   local heights = args.heights or "160"
   local heights = args.heights or "160"



Latest revision as of 19:30, 10 February 2026

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

-- Helper: get a named arg from frame.args first, then frame:getParent().args
local function getArg(frame, name, default)
  local val = frame.args[name]
  if val and val ~= "" then return val end
  local parent = frame:getParent()
  if parent then
    val = parent.args[name]
    if val and val ~= "" then return val end
  end
  return default or ""
end

-- Extract the |images= value from the current page's wikitext.
-- Reads the raw source via mw.title:getContent() and parses out
-- the images parameter from the page's content template invocation
-- (works with {{CRT model}}, {{AV device}}, or any template with an images= field).
local function getImagesFromPageSource()
  local title = mw.title.getCurrentTitle()
  local content = title:getContent()
  if not content then return nil end

  -- Match |images= value: capture everything after |images= up to the next
  -- pipe-prefixed parameter (|paramname=) or closing braces (}})
  local images = content:match("|images%s*=%s*([^|}]+)")
  if images then
    return trim(images)
  end
  return nil
end

-- Auto-gallery: reads the current page's wikitext to find the images= field
-- from the page's content template, then renders a gallery. No parameters required.
-- Called by {{EveryCRT gallery}}.
function p.autoGallery(frame)
  -- Allow explicit override via images= parameter
  local imageList = getArg(frame, "images", "")

  -- If no explicit images, read from page source
  if imageList == "" then
    imageList = getImagesFromPageSource() or ""
  end

  if imageList == "" then return "" end

  local mode = getArg(frame, "mode", "nolines")
  local heights = getArg(frame, "heights", "160")

  local lines = {}
  for _, raw in ipairs(split_csv(imageList)) 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

-- Emits SMW annotations for a multi-image list:
-- [[Has images::File:...]] repeated
-- Called by {{CRT model}} and {{AV device}}.
function p.annotate(frame)
  local args = frame:getParent().args
  local prop = args.prop or "Has images"
  local list = args.images or ""

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

  return frame:preprocess(table.concat(out, "\n"))
end

-- Renders a gallery from a comma-separated image list.
-- Called by {{CRT model}} and {{AV device}} for their default inline gallery.
function p.gallery(frame)
  local args = frame:getParent().args
  local list = args.images or ""
  local mode = args.mode or "nolines"
  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