Module:NarratorListModule

From The Seven Sages of Rome
Revision as of 19:30, 27 February 2025 by Noeth (talk | contribs)

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

local p = {}

local function getSubobjects(shortTitle)
  local query = "[[Has subobject::+]][[Has Short Title::" .. shortTitle .. "]]|?Has Narrator"
  local results = mw.smw.ask(query)
  return results
end

function p.getNarratorList(frame)
  local shortTitle = frame.args.shortTitle
  if not shortTitle then
    return "Error: shortTitle parameter is missing."
  end

  local results = getSubobjects(shortTitle)

  if not results then
    return "* No matching subobjects found."
  end

  local groupedResults = {}

  for _, result in ipairs(results) do
    local narrator = result["Has Narrator"] and result["Has Narrator"][1]
    if narrator then
      local pageName = result.fulltext:match("\[\[(.-)\]\]") -- Extract page name from fulltext
      if pageName then
        if not groupedResults[narrator] then
          groupedResults[narrator] = {}
        end
        groupedResults[narrator][#groupedResults[narrator] + 1] = "[[" .. pageName .. "]]"
      end
    end
  end

  local output = {}
  for narrator, pages in pairs(groupedResults) do
    output[#output + 1] = "* " .. narrator .. ": " .. table.concat(pages, ", ")
  end

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

return p