Spiderjjr45 (talk | contribs) No edit summary |
Spiderjjr45 (talk | contribs) No edit summary |
||
| Line 19: | Line 19: | ||
local function processRow(row) |
local function processRow(row) |
||
-- Extract the campaign name |
-- Extract the first line (campaign name with wiki link) and last line (location) |
||
local |
local campaignWithLink = row:match("^(|%[%[.-%]%])") |
||
| ⚫ | |||
if not campaign then |
|||
campaign = row:match("|%[%[(.-)|'''") -- Matches cases like |[[Ancients Alive|'''Ancients Alive''']] |
|||
if campaignWithLink and location then |
|||
-- Trim both campaign name and location |
|||
campaignWithLink = mw.text.trim(campaignWithLink) |
|||
end |
|||
| ⚫ | |||
end |
|||
-- In case campaign name is still not found, use alternative pattern |
|||
if not campaign then |
|||
campaign = row:match("|'''(.-)'''") -- Matches cases without wiki link but with bold formatting |
|||
end |
|||
-- Check and add to the relevant location list |
|||
-- local location = row:match("|([^|]-)\n?$") -- The '$' ensures that we are at the end of the row |
|||
| ⚫ | |||
if campaign and location then |
|||
| ⚫ | |||
-- Check and add to the relevant location |
|||
if location:find("PA") then |
if location:find("PA") then |
||
table.insert(campaigns.PA, |
table.insert(campaigns.PA, campaignWithLink) |
||
end |
end |
||
if location:find("MD") then |
if location:find("MD") then |
||
table.insert(campaigns.MD, |
table.insert(campaigns.MD, campaignWithLink) |
||
end |
end |
||
if location:find("Online") then |
if location:find("Online") then |
||
table.insert(campaigns.Online, |
table.insert(campaigns.Online, campaignWithLink) |
||
end |
end |
||
end |
end |
||
Revision as of 15:53, 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 the first line (campaign name with wiki link) and last line (location)
local campaignWithLink = row:match("^(|%[%[.-%]%])")
local location = row:match("[^\n]+%s*$")
if campaignWithLink and location then
-- Trim both campaign name and location
campaignWithLink = mw.text.trim(campaignWithLink)
location = mw.text.trim(location)
-- Check and add to the relevant location list
if location:find("PA") then
table.insert(campaigns.PA, campaignWithLink)
end
if location:find("MD") then
table.insert(campaigns.MD, campaignWithLink)
end
if 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
