Module:LoU

-- LoU - List of Untergangers lua module     to improve parsing speed of the List of Untergangers page     by mfaizsyahmi, 2017

local libCtry = require("Dev:Country") local libFlag = require("Module:iconflags") local lang = mw.language.new("en") local p = {}

-- constants local ytPlFmt = 'pl' local ytUserFmt = 'yt' local ytUcFmt = 'yt' local NaN = 0/0

-- implementing or assignments -- basically go through given list, iterate until non-nil value found local function eqOr(list) for _,v in pairs(list) do       if v~=nil then return v end end end

-- come on, lua doesn't have this built in? local function trim(str) if type(str)=="string" then local s = string.gsub(str, "^%s+", "") s = string.gsub(s, "%s+$", "") return s   else return str end end --function p.trim(s) return trim(s) end

local function markupRow(attribs) local attrTbl = {} for k,v in pairs(attribs) do       table.insert(attrTbl, k..'="'..v..'" ') end return '|- '..table.concat(attrTbl) end

local function markupCell(text,attribs) local attrTbl = {} for k,v in pairs(attribs) do       table.insert(attrTbl, k..'="'..v..'" ') end return '| '..table.concat(attrTbl)..'|'..text end

local statusTbl = { ['a'] = ' Active ', ['h'] = ' Hiatus ', ['d'] = ' Dormant ', ['r'] = ' Retired ', ['s'] = ' Suicide ', ['t'] = ' Terminated ', ['x'] = ' Resurrected ', ['na']= ' Not Unterganger? ',   ['a?']= ' Active (?) ', ['h?']= ' Hiatus (?) ', ['d?']= ' Dormant (?) ', ['r?']= ' Retired (?) ', ['s?']= ' Suicide (?) ', ['t?']= ' Terminated (?) ', ['x?']= ' Resurrected (?) ', }

function p.entry(frame) -- retrieve arguments local fArgs = frame.args--.getParent local noAjax = (fArgs.noajax~=nil) local name = trim(eqOr{fArgs.name, fArgs[1]}) local reg = trim(eqOr{fArgs.region, fArgs[2], ""}) local ctry = trim(eqOr{fArgs.country, fArgs[3], ""}) local vids = trim(eqOr{fArgs.vids, fArgs[4], ""}) local views= trim(eqOr{fArgs.views, fArgs[5], ""}) local subs = trim(eqOr{fArgs.subs, fArgs[6], ""}) local status = trim(eqOr{fArgs.status, fArgs[7], ""}) local yt = trim(eqOr{fArgs.yt, fArgs[8], ""}) local pl = trim(fArgs.pl) -- parse select arguments -- country and region local countryCode = mw.ustring.lower(ctry) -- HPW accepts uk as synonym for gb    if countryCode == 'uk' then countryCode = 'gb' end local countryName, region, subregion, flag if countryCode ~= nil and countryCode ~= "" then countryName = libCtry.main{countryCode,'name'} region = libCtry.main{countryCode,'region'} subregion = libCtry.main{countryCode,'sub-region'} flag = libFlag.flag{countryCode, useemoji='om'} if reg == "" then reg = ".." end end

-- numbers --mw.log(vids.." - vids: "..tostring(vids ~= nil and vids ~= "")) if vids ~= nil and vids ~= "" then local n = tonumber(vids,10) if n ~= nil then vids = lang:formatNum(n) end end if views ~= nil and views ~= "" then local n = tonumber(views,10) if n ~= nil then views = lang:formatNum(n) end end if subs ~= nil and subs ~= "" then local n = tonumber(subs,10) if n ~= nil then subs = lang:formatNum(n) end end -- status status = statusTbl[status] or status -- yt link local ytlink = '' -- tilde means take the name argument if yt == '~' then _, _, yt = mw.ustring.find(name,'%[?%[?([^%]]*)%]?%]?') end if pl ~= nil then -- playlist defined ytlink = mw.ustring.format(ytPlFmt, pl) elseif mw.ustring.len(yt)==24 and mw.ustring.sub(yt,1,2)=='UC' then -- yt matches channel ID (string of 24 char length begin w/ UC) ytlink = mw.ustring.format(ytUcFmt, yt) else -- old username ytlink = mw.ustring.format(ytUserFmt, yt) end -- prepare markup local markupTbl = {} if noAjax then table.insert(markupTbl, '|- class="noAJAX"') else table.insert(markupTbl, '|-') end table.insert(markupTbl, '| class="index"  | ') table.insert(markupTbl, '| class="name"   | '..name) if countryCode ~= nil and countryCode ~= "" then table.insert(markupTbl, markupCell(reg, { class = "region", title = region.."–"..subregion }))       table.insert(markupTbl, markupCell(ctry .. flag, { class = "country", title = countryName }))   else table.insert(markupTbl, '| class="region" | '..reg) table.insert(markupTbl, '| class="country" | '..ctry) end table.insert(markupTbl, '| class="vids"   | '..vids) table.insert(markupTbl, '| class="views"  | '..views) table.insert(markupTbl, '| class="subs"   | '..subs) table.insert(markupTbl, '| class="status" | '..status) table.insert(markupTbl, '| class="yt"     | '..ytlink) return table.concat(markupTbl,'\n') end

return p