local p =
function p.siteinfo(frame) return mw.site.siteName .. '--' .. '(' .. mw.site.server .. ')'end
local serialize_replacements =
local dump_table_count = 0;local function dump_table(o) if dump_table_count
'table' then local s = ' ' else st = tostring(o) rst = st:gsub('[\r\n\\ ]', serialize_replacements) return rst endend
local diagnostic_preprocess_nested_data = function mw.diagnostic_preprocess_nested(o) if o
'exec' then diagnostic_preprocess_nested_data = diagnostic_preprocess_nested_data .. 'once'; end return diagnostic_preprocess_nested_dataend
function p.info(frame) mw.logObject(frame) return dump_table(frame)endlocal htmlencodeMap =
function p.ta(frame) s = 'abcdef&' s = a = mw.ustring.gsub(s, '', htmlencodeMap) --a = mw.ustring.gsub("abcd", "b(.)", "%1-%2") return s .. "|" .. aendfunction p.tb mw.ustring.gsub("abcd", "a(bc)d", function(arg1) mw.log('basic', arg1); -- arg1 is the matched string or "bc" end)
mw.ustring.gsub("abcd", "bcd", function(arg1) mw.log('empty', arg1); -- arg1 is the position of the empty capture or "2" end)endfunction p.tccandidateString = 'Geography'page, candidateArgs = mw.ustring.match(candidateString, "^%s*(%[%b[]%])%s*|?(.*)")mw.log(page, candidateArgs)page, candidateArgs = mw.ustring.match(candidateString, "%s*([^|]*[^|%s])%s*|?(.*)")mw.log(page, candidateArgs)c = '#d8d8d8'c = mw.ustring.match(c, '^[%s#]*([a-f0-9]*)[%s]*$')return cend
function p.td return mw.language.isValidCode('mdy') endfunction p.te --mw.logObject(mw.ext.data.get('DateI18n.tab', 'sr')) local langObj = mw.language.new('en')mw.logObject(langObj)end
local mYesno = require('Module:Yesno')-- a global per #invoke local linked_write_in = false
function formatnum(num) -- simple wrapper local lang = mw.getContentLanguage return lang:formatNum(num)end
function percent(part, total) if total >= 1000000 then -- if > 1 million votes, then round to 2 decimals round_to = 2 else round_to = 1 end local ret = mw.ext.ParserFunctions.expr("" .. 100 * part / total .. " round " .. round_to) if not string.find(ret, ".", 1, true) then -- add the decimals that expr doesn't ret = ret .. "." .. string.rep("0", round_to) end return retend
function p.make(invoke) frame = invoke:getParent local state, year, contest, type = parse_args(frame.args) local ret = "" local no_headings = mYesno(frame.args["no headings"]) if string.find(year, ",", 1, true) then -- multi mode for i,v in pairs(mw.text.split(year, ",", true)) do if not no_headings then ret = ret .. "\n
" end ret = ret .. make(state, v, contest, type, year_args(v, frame.args)) end else ret = ret .. make(state, year, contest, type, frame.args) end return invoke:preprocess(ret)end
function fmt_candidate(v, winner, total_votes,args, usestateparties) local temp = "" return tempend
function parse_args(args) local state = args[1] if not state then error("State is missing") end local year = args[2] if not year then error("Year is missing") end local contest = args[3] if not contest then error("Contest is missing") end local type = "General" if args.type then if args.type
function year_args(year, args) -- we want to turn year args like "|2018 foo=" into just foo -- drop any other year args like "|2016 foo=" -- and have year args override general args -- finally have general args local new = for k,v in pairs(args) do local k_year = mw.ustring.match(k, "^%d%d%d%d ") if k_year then k_year = mw.text.trim(k_year) end if k_year and k_year
function make(state, year, contest, type, args) function load_tabular(state, year, type) local tab_name = state .. " Elections/" .. year .. "/" .. type .. "/Candidates.tab" local tabular = mw.ext.data.get(tab_name) if tabular then return tabular else return end end local tabular = load_tabular(state, year, type) if tabular.error then error(tabular.error) end function find_candidates(data, contest) local candidates = for k,v in pairs(data) do if v[1]
v[2] then incumb_party = v[3] end end return total_votes, winner, incumb_party end local total_votes, winner, incumb_party = sum_totals(candidates) local usestateparties = nil if mw.ustring.find(contest, "United States Representative", 1, true) then title = "United States House of Representatives elections, " .. year .. "" elseif mw.ustring.find(contest, "State Assembly Member", 1, true) then title = "" .. state .. " State Assembly election, " .. year .. "" usestateparties = state elseif contest
for k,v in pairs(candidates) do ret = ret .. fmt_candidate(v, winner,total_votes,args,usestateparties) end ret = ret .. total_box(total_votes) local hold = args.hold local gain = false if hold
winner[2] then ret = ret .. "" elseif hold
"Democratic" then lose_party = normalize_parties("Republican",usestateparties) else lose_party = normalize_parties("Democratic",usestateparties) end win_party = normalize_parties(win_party) gain = true elseif args.gain then win_party = normalize_parties(args.gain,usestateparties) lose_party = normalize_parties(args.loser,usestateparties) gain = true elseif incumb_party and incumb_party ~= winner[3] then win_party = normalize_parties(winner[3],usestateparties) lose_party = normalize_parties(incumb_party,usestateparties) gain = true end if gain then ret = ret .. "" end
ret = ret .. "" return retend
function normalize_parties(party, state) -- Drop all parties after the first one? party = mw.text.split(party, ",", true)[1] local specials = specials["No Party Preference"] = "No party preference" if specials[party] then return specials[party] end if state then -- ex "California Democratic Party" return state .. " " .. party .. " Party" end return party .. " Party (US)"end
local function ss(width)-- local val = -- local t = mw.text.trim(val)-- return val,type(val), t, type(t) local fw = ('%.2f'):format(width) local cwidth = tonumber(fw) --cwidth = tonumber(' NaN ') local res if cwidth > 0 then res = 'A' else res = 'Z' end return width, type(width), fw, cwidth, type(cwidth), res, type(res)endfunction p.s mw.log(ss(1/0)) mw.log(ss(0/0))-- mw.log(ss('abc')) mw.log(ss(10.345))endlocal mostUsed = mw.loadData('Module:CountryData/summary')local function getcontents(frame,country,params) return frame:expandTemplateendfunction p.s(frame)local country = 'United States'local params = return getcontents(frame,country,params)end
function p.r local part = local s = "" s = string.gsub(s,"|([^|=]-)=","\1\1%1\2") s = string.gsub(s,"}}%s*$","\1") --Loop over string and add params to table local k, v local part = for k, v in string.gmatch(s,"\1%s*([^\1\2]-)%s*\2%s*([^\1\2]-)%s*\1") do mw.log(k, v) --local k = string.match(par,"\1%s*(.-)%s*\2") --local v = string.match(par,"\2%s*(.-)%s*\1") if v and not (v
function dotcheck(Title, sepc) local pos = string.find (Title, '%.$') -- does this end in a dot? and not mw.ustring.find (Title, '%.%s*%a%.$') and not mw.ustring.find (Title, '%s+%a%.$') -- ...and not a 'space-letter-dot' initial (Allium canadense L.) mw.log(pos) if pos or sepc ~= '.' then Title = mw.ustring.gsub(Title, '%' .. sepc .. '$', ); -- remove any trailing separator character; sepc and ms.ustring here for languages that use multibyte separator characters mw.log(Title) endend
function p.x dotcheck ('abc..b.c.d.xpdf?dajshdkjahsd', '.') dotcheck ('abc..b.c.d.xpdf?dajshdkjahsd.', '.') dotcheck ('abc..b.c.d.xpdf?dajshdkjahsd L.', '.')endfunction p.xx(frame)mw.logObject(frame)return mw.text.nowiki('[]fredRFC 123\n*bill\n----\r\n*://a')endreturn p