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 142: | Line 142: | ||
end | end | ||
function p.debug(frame) | |||
local page = frame.args[1] or "General Port of Lake Morovia - Services" | |||
local title = mw.title.new(page) | |||
if not title then return "No title" end | |||
local text = title:getContent() or "" | |||
local pos = text:find("Average") | |||
if not pos then | |||
return "No 'Average' found in raw source." | |||
end | |||
local startPos = math.max(1, pos - 200) | |||
local endPos = math.min(#text, pos + 400) | |||
return "<pre>" .. mw.text.nowiki(text:sub(startPos, endPos)) .. "</pre>" | |||
end | |||
return p | return p | ||
Revision as of 14:45, 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 parseSummaryFromPage(pageName)
local title = mw.title.new(pageName)
if not title then return nil end
local text = title:getContent() or ""
if text == "" then return nil end
-- normalize some common source noise
text = text:gsub(" ", " ")
text = text:gsub("<.->", " ")
text = text:gsub("%[%[", "")
text = text:gsub("%]%]", "")
text = text:gsub("'''", "")
text = text:gsub("''", "")
local matches = {}
-- broad match: only capture the two numbers
for adjusted, base in text:gmatch("Average%s*adjusted%s*%(([%d%.]+)%)%s*vs%s*base%s*%(([%d%.]+)%)") do
table.insert(matches, {
adjusted = tonumber(adjusted),
base = tonumber(base)
})
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,
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 = parseSummaryFromPage(sector.page)
if data then
local bg, fg = colorForPct(data.pct)
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;" | Average adjusted (%.4f) vs base (%.4f)',
bg, fg, data.adjusted, data.base
))
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, based on the last summary row found on 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 title = mw.title.new(page)
if not title then return "No title" end
local text = title:getContent() or ""
local pos = text:find("Average")
if not pos then
return "No 'Average' found in raw source."
end
local startPos = math.max(1, pos - 200)
local endPos = math.min(#text, pos + 400)
return "<pre>" .. mw.text.nowiki(text:sub(startPos, endPos)) .. "</pre>"
end
return p