--build a table of Olymipc medals and country ranks for an individual country. This example supports Mexico and Serbia. Needs better data organization but serves as proof of concept
require ('strict')-- local data_t = mw.loadData ('Module:Sandbox/Kawesa01/nocMedals/data');local format_strings_t = local messages_t =
--
local function games_template_make (games, year) if tonumber (year) then -- if
--main| country_data_t =
local function html_table_build (frame) local args = require ('Module:Arguments').getArgs (frame); local country_data_t = mw.text.jsonEncode(args[3])
if not args[1] or
local games = args[2]:upper; -- SOG or WOG if 'SOG' ~= games and 'WOG' ~= games then return '
unknown games: ' .. games .. ''; end--games='WOG'local html_tbl = mw.html.create ('table'); -- create the wikitable html_tbl :attr ('class', 'wikitable'):attr ('style', 'text-align:center') -- add table attributes :tag ('caption'):wikitext (string.format (format_strings_t.caption, country_data_t[tag].country, ('SOG'
local row_count = 0; -- used as flag when rowspan used for future and no_participate messaging for _, row_t in ipairs (country_data_t[tag][games]) do -- for each row sequence in the data table local games_template = games_template_make (games, row_t.year); if row_t.athletes then -- only when there are athletes local athletes = string.format (format_strings_t.athletes, country_data_t[tag].country, row_t.year, ('SOG'
games) and 'Summer' or 'Winter', row_t.rank); local tr = html_tbl:tag('tr'):attr('style', ('yes'
1 then td:attr('style', 'background-color:#F7F6A8;'):wikitext(rank):done elseif row_t.rank
3 then td:attr('style', 'background-color:#FFDAB9;'):wikitext(rank):done else td:wikitext(rank):done end
gold_total = gold_total + row_t.gold; -- update totals silver_total = silver_total + row_t.silver; bronze_total = bronze_total + row_t.bronze; total_total = total_total + row_t.gold + row_t.silver + row_t.bronze -- update total of totals elseif row_t.message then if row_t.rowspan then row_count = row_t.rowspan - 1; html_tbl:tag ('tr') :tag ('td'):attr ('style', 'text-align:left'):wikitext (games_template):done -- game name :tag ('td'):attr ('colspan', 6):attr ('rowspan', row_t.rowspan):wikitext (messages_t[row_t.message] or row_t.message):done else -- here when single-row message (no rowspan) html_tbl:tag ('tr') :tag ('td'):attr ('style', 'text-align:left'):wikitext (games_template):done -- game name :tag ('td'):attr ('colspan', 6):attr ('rowspan', row_t.rowspan):wikitext (messages_t[row_t.message] or row_t.message):done end -- here when no
html_tbl :tag ('tr') -- and create the bottom header :tag ('th'):attr ('colspan', '2'):wikitext ('Totals') -- with all of the totals :tag ('th'):attr ('class', 'combined_medals'):wikitext (gold_total) :tag ('th'):wikitext (silver_total) :tag ('th'):wikitext (bronze_total) :tag ('th'):wikitext (total_total) :tag ('th'):wikitext ('' .. country_data_t[tag].all_time_rank .. '')mw.log (tostring (html_tbl)) return frame:preprocess (tostring (html_tbl)); -- make a big string, preprocess the game templates, and doneend
--total_medals_get|
local function total_medals_get (frame) local args = require ('Module:Arguments').getArgs (frame);
local country_data_t = mw.text.jsonEncode(args[3]) local country = args[1]:upper; local award = args[2]:lower;
if not country_data_t[country] then return '
unknown country: ' .. args[1] .. ''; end if not [award] then return 'unknown award: ' .. args[2] .. ''; endlocal total = 0;
for _, games in ipairs do for _, v_t in ipairs (country_data_t[country][games]) do if v_t[award] then total = total + v_t[award] end end end return total;end
local function get_combined_SOG_WOG(frame) local args = require('Module:Arguments').getArgs(frame) local award = args[1]:lower; local fullpage = mw.title.getCurrentTitle.prefixedText local text = mw.title.new(fullpage):getContent;-- Get the wikitext as a parameter
-- Use the mw.text.unstrip function to process the wikitext text = mw.text.unstrip(text)
-- Use mw.text.split to split the wikitext into lines local lines = mw.text.split(text, '\n')
local inSOGTable = false -- Flag to track if we are inside the SOG table local goldValues =
for _, line in ipairs(lines) do -- Check if we've entered the SOG table if line:match('') then break end end end
-- Calculate the sum of gold values local goldSum = 0 for _, value in ipairs(goldValues) do goldSum = goldSum + value end
return goldSumend
----------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------
return