Module:GeneralPortSectorSummary: Difference between revisions

From MicrasWiki
Jump to navigationJump to search
NewZimiaGov (talk | contribs)
No edit summary
NewZimiaGov (talk | contribs)
No edit summary
Line 29: Line 29:
return string.format("%.1f%%", num)
return string.format("%.1f%%", num)
else
else
return "0.0%%"
return "0.0%"
end
end
end
end
Line 61: Line 61:
end
end


local function parseSummaryFromPage(pageName)
local function getRenderedPageText(frame, pageName)
local title = mw.title.new(pageName)
-- Pull rendered/transcluded output instead of raw source.
if not title then return nil end
local ok, rendered = pcall(function()
return frame:preprocess("{{:" .. pageName .. "}}")
end)


local text = title:getContent() or ""
if not ok or not rendered or rendered == "" then
if text == "" then return nil end
return nil
end
 
-- Normalize whitespace a bit.
rendered = rendered:gsub(" ", " ")
rendered = rendered:gsub("%s+", " ")
 
return rendered
end


-- normalize some common source noise
local function parseLastSummaryFromRendered(frame, pageName)
text = text:gsub(" ", " ")
local text = getRenderedPageText(frame, pageName)
text = text:gsub("<.->", " ")
if not text then
text = text:gsub("%[%[", "")
return nil
text = text:gsub("%]%]", "")
end
text = text:gsub("'''", "")
text = text:gsub("''", "")


local matches = {}
local matches = {}


-- broad match: only capture the two numbers
-- Capture the rendered summary line directly.
for adjusted, base in text:gmatch("Average%s*adjusted%s*%(([%d%.]+)%)%s*vs%s*base%s*%(([%d%.]+)%)") do
for adjusted, base, arrow, level in text:gmatch("Average adjusted %(([%d%.]+)%) vs base %(([%d%.]+)%)%:%s*([↑↓•])%s*([A-Za-z%s%(%)%-]+)") do
table.insert(matches, {
table.insert(matches, {
adjusted = tonumber(adjusted),
adjusted = tonumber(adjusted),
base = tonumber(base)
base = tonumber(base),
arrow = arrow,
level = mw.text.trim(level or "")
})
})
end
-- Fallback: just get the two numbers if arrow/level formatting varies.
if #matches == 0 then
for adjusted, base in text:gmatch("Average adjusted %(([%d%.]+)%) vs base %(([%d%.]+)%)") do
table.insert(matches, {
adjusted = tonumber(adjusted),
base = tonumber(base),
arrow = "",
level = ""
})
end
end
end


Line 100: Line 122:
adjusted = m.adjusted,
adjusted = m.adjusted,
base = m.base,
base = m.base,
arrow = m.arrow,
level = m.level,
pct = pct
pct = pct
}
}
Line 111: Line 135:


for _, sector in ipairs(sectors) do
for _, sector in ipairs(sectors) do
local data = parseSummaryFromPage(sector.page)
local data = parseLastSummaryFromRendered(frame, sector.page)


if data then
if data then
local bg, fg = colorForPct(data.pct)
local bg, fg = colorForPct(data.pct)
local summaryText = string.format(
"Average adjusted (%.4f) vs base (%.4f)%s",
data.adjusted,
data.base,
(data.arrow ~= "" and (": " .. data.arrow .. " " .. data.level) or "")
)


table.insert(out, '|-')
table.insert(out, '|-')
Line 123: Line 153:
))
))
table.insert(out, string.format(
table.insert(out, string.format(
'| style="background:%s; color:%s;" | Average adjusted (%.4f) vs base (%.4f)',
'| style="background:%s; color:%s;" | %s',
bg, fg, data.adjusted, data.base
bg, fg, summaryText
))
))
table.insert(out, string.format(
table.insert(out, string.format(
Line 138: Line 168:


table.insert(out, '|}')
table.insert(out, '|}')
table.insert(out, "''Average Daily Change'' = ((adjusted − base) ÷ base) × 100, based on the last summary row found on each sector page.")
table.insert(out, "''Average Daily Change'' = ((adjusted − base) ÷ base) × 100, using the rendered summary line from each sector page.")
return table.concat(out, "\n")
return table.concat(out, "\n")
end
end
Line 144: Line 174:
function p.debug(frame)
function p.debug(frame)
local page = frame.args[1] or "General Port of Lake Morovia - Services"
local page = frame.args[1] or "General Port of Lake Morovia - Services"
local title = mw.title.new(page)
local text = getRenderedPageText(frame, page)
if not title then return "No title" end
if not text then
local text = title:getContent() or ""
return "No rendered text found."
local pos = text:find("Average")
end
if not pos then
 
return "No 'Average' found in raw source."
local startPos = text:find("Average adjusted", 1, true)
if not startPos then
return "Rendered page found, but no 'Average adjusted' string detected."
end
end
local startPos = math.max(1, pos - 200)
 
local endPos = math.min(#text, pos + 400)
local snippet = text:sub(math.max(1, startPos - 120), math.min(#text, startPos + 220))
return "<pre>" .. mw.text.nowiki(text:sub(startPos, endPos)) .. "</pre>"
return "<pre>" .. mw.text.nowiki(snippet) .. "</pre>"
end
end
return p
return p

Revision as of 14:49, 17 March 2026

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

-- Module:GeneralPortSectorSummary
local p = {}

local sectors = {
	{
		label = "Agriculture",
		page = "General Port of Lake Morovia - Agriculture"
	},
	{
		label = "Mining / Manufacturing / Energy",
		page = "General Port of Lake Morovia - Mining/Manufacturing/Energy"
	},
	{
		label = "Services",
		page = "General Port of Lake Morovia - Services"
	}
}

local function round(num, places)
	local mult = 10 ^ (places or 0)
	return math.floor(num * mult + 0.5) / mult
end

local function fmtPct(num)
	num = round(num, 1)
	if num > 0 then
		return string.format("+%.1f%%", num)
	elseif num < 0 then
		return string.format("%.1f%%", num)
	else
		return "0.0%"
	end
end

local function colorForPct(pct)
	if pct >= 5 then
		return "#d8f0d2", "#1f5f1f"
	elseif pct > 0 then
		return "#edf7e8", "#2f6f2f"
	elseif pct <= -5 then
		return "#f8d7d7", "#8b1e1e"
	elseif pct < 0 then
		return "#fbeaea", "#a33a3a"
	else
		return "#f3f3f3", "#555555"
	end
end

local function readingForPct(pct)
	if pct >= 5 then
		return "Rising"
	elseif pct > 0 then
		return "Slightly Rising"
	elseif pct <= -5 then
		return "Falling"
	elseif pct < 0 then
		return "Slightly Falling"
	else
		return "Stable"
	end
end

local function getRenderedPageText(frame, pageName)
	-- Pull rendered/transcluded output instead of raw source.
	local ok, rendered = pcall(function()
		return frame:preprocess("{{:" .. pageName .. "}}")
	end)

	if not ok or not rendered or rendered == "" then
		return nil
	end

	-- Normalize whitespace a bit.
	rendered = rendered:gsub("&nbsp;", " ")
	rendered = rendered:gsub("%s+", " ")

	return rendered
end

local function parseLastSummaryFromRendered(frame, pageName)
	local text = getRenderedPageText(frame, pageName)
	if not text then
		return nil
	end

	local matches = {}

	-- Capture the rendered summary line directly.
	for adjusted, base, arrow, level in text:gmatch("Average adjusted %(([%d%.]+)%) vs base %(([%d%.]+)%)%:%s*([↑↓•])%s*([A-Za-z%s%(%)%-]+)") do
		table.insert(matches, {
			adjusted = tonumber(adjusted),
			base = tonumber(base),
			arrow = arrow,
			level = mw.text.trim(level or "")
		})
	end

	-- Fallback: just get the two numbers if arrow/level formatting varies.
	if #matches == 0 then
		for adjusted, base in text:gmatch("Average adjusted %(([%d%.]+)%) vs base %(([%d%.]+)%)") do
			table.insert(matches, {
				adjusted = tonumber(adjusted),
				base = tonumber(base),
				arrow = "",
				level = ""
			})
		end
	end

	if #matches == 0 then
		return nil
	end

	local m = matches[#matches]
	if not m.base or m.base == 0 then
		return nil
	end

	local pct = ((m.adjusted - m.base) / m.base) * 100

	return {
		adjusted = m.adjusted,
		base = m.base,
		arrow = m.arrow,
		level = m.level,
		pct = pct
	}
end

function p.main(frame)
	local out = {}
	table.insert(out, '{| class="wikitable" style="width:100%; text-align:center;"')
	table.insert(out, '|-')
	table.insert(out, '! Sector !! Average Daily Change !! Summary !! Reading')

	for _, sector in ipairs(sectors) do
		local data = parseLastSummaryFromRendered(frame, sector.page)

		if data then
			local bg, fg = colorForPct(data.pct)
			local summaryText = string.format(
				"Average adjusted (%.4f) vs base (%.4f)%s",
				data.adjusted,
				data.base,
				(data.arrow ~= "" and (": " .. data.arrow .. " " .. data.level) or "")
			)

			table.insert(out, '|-')
			table.insert(out, '| style="text-align:left;" | ' .. sector.label)
			table.insert(out, string.format(
				'| style="background:%s; color:%s; font-weight:bold;" | %s',
				bg, fg, fmtPct(data.pct)
			))
			table.insert(out, string.format(
				'| style="background:%s; color:%s;" | %s',
				bg, fg, summaryText
			))
			table.insert(out, string.format(
				'| style="background:%s; color:%s;" | %s',
				bg, fg, readingForPct(data.pct)
			))
		else
			table.insert(out, '|-')
			table.insert(out, '| style="text-align:left;" | ' .. sector.label)
			table.insert(out, '| colspan="3" style="color:#888;" | No summary found')
		end
	end

	table.insert(out, '|}')
	table.insert(out, "''Average Daily Change'' = ((adjusted − base) ÷ base) × 100, using the rendered summary line from each sector page.")
	return table.concat(out, "\n")
end

function p.debug(frame)
	local page = frame.args[1] or "General Port of Lake Morovia - Services"
	local text = getRenderedPageText(frame, page)
	if not text then
		return "No rendered text found."
	end

	local startPos = text:find("Average adjusted", 1, true)
	if not startPos then
		return "Rendered page found, but no 'Average adjusted' string detected."
	end

	local snippet = text:sub(math.max(1, startPos - 120), math.min(#text, startPos + 220))
	return "<pre>" .. mw.text.nowiki(snippet) .. "</pre>"
end

return p