Spiderjjr45 (talk | contribs) No edit summary |
Spiderjjr45 (talk | contribs) No edit summary |
||
Line 17: | Line 17: | ||
Online = {} | Online = {} | ||
} | } | ||
local function processRow(row) | local function processRow(row) | ||
-- Extract the first | -- Extract everything up to the first "]]" | ||
local campaignWithLink = row:match("^ | local campaignWithLink = row:match("^.-]]") | ||
local location = row:match("[^\n]+ | -- Extract the location, which is the last element before the end of the row | ||
local location = row:match("[^\n]+$") | |||
if campaignWithLink | if campaignWithLink then | ||
-- | -- Clean up the campaign string by removing leading pipes, quotes, and any trailing whitespace | ||
campaignWithLink = | campaignWithLink = campaignWithLink:gsub("^|%s*'''", ""):gsub("'''%s*$", ""):gsub("%[%[", ""):gsub("%]%]", "") | ||
-- Trim the location string | |||
location = mw.text.trim(location) | location = mw.text.trim(location) | ||
-- | -- Add the campaign to the relevant location list | ||
if location:find("PA") then | if location:find("PA") then | ||
table.insert(campaigns.PA, campaignWithLink) | table.insert(campaigns.PA, campaignWithLink) | ||
elseif location:find("MD") then | |||
table.insert(campaigns.MD, campaignWithLink) | table.insert(campaigns.MD, campaignWithLink) | ||
elseif location:find("Online") then | |||
table.insert(campaigns.Online, campaignWithLink) | table.insert(campaigns.Online, campaignWithLink) | ||
end | end |
Revision as of 16:04, 23 January 2024
Documentation for this module may be created at Module:CampaignNavbox/doc
local p = {} function p.getCampaigns(frame) -- Fetch the content of the "Campaign" page local pageTitle = "Campaign" local page = mw.title.new(pageTitle) local wikitext = page:getContent() -- Check if the page content is available if not wikitext then return "Error: Unable to fetch content for page '" .. pageTitle .. "'." end local campaigns = { PA = {}, MD = {}, Online = {} } local function processRow(row) -- Extract everything up to the first "]]" local campaignWithLink = row:match("^.-]]") -- Extract the location, which is the last element before the end of the row local location = row:match("[^\n]+$") if campaignWithLink then -- Clean up the campaign string by removing leading pipes, quotes, and any trailing whitespace campaignWithLink = campaignWithLink:gsub("^|%s*'''", ""):gsub("'''%s*$", ""):gsub("%[%[", ""):gsub("%]%]", "") -- Trim the location string location = mw.text.trim(location) -- Add the campaign to the relevant location list if location:find("PA") then table.insert(campaigns.PA, campaignWithLink) elseif location:find("MD") then table.insert(campaigns.MD, campaignWithLink) elseif location:find("Online") then table.insert(campaigns.Online, campaignWithLink) end end end -- Split the wikitext into rows and process each local rowPattern = "|%-(.-)\n|%-" wikitext = wikitext .. '|-' -- Append delimiter to capture the last row for row in wikitext:gmatch(rowPattern) do processRow(row) end -- Build the navbox local navbox = '{| class="wikitable"\n!Location\n!Campaigns\n' for loc, camps in pairs(campaigns) do navbox = navbox .. '|-\n|' .. loc .. '\n|' .. table.concat(camps, ', ') .. '\n' end navbox = navbox .. '|}' return navbox end return p