모듈:CountryData

포장 (토론 | 기여)님의 2019년 12월 5일 (목) 19:47 판 (새 문서: local p = {} local function getcontents(frame,country,params) return frame:expandTemplate({title="Country data "..country;args=params}) end function p.gettable(frame,country,param...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

local p = {}

local function getcontents(frame,country,params) return frame:expandTemplate({title="Country data "..country;args=params}) end

function p.gettable(frame,country,params) --Returns the parameters of a country data template as a Lua table --If not a valid data template, return empty tablelocal bool, s = pcall(getcontents,frame,country,params or {})if bool and string.find(s,"^%{%{ *%{%{%{1")then--Replace parameter delimiters with arbitrary control characters--to avoid clashes if param values contain equals/pipe signss = string.gsub(s,"|([^|=]-)=","\1\1%1\2")s = string.gsub(s,"}}%s*$","\1")--Loop over string and add params to tablelocal part = {}for par in string.gmatch(s,"\1[^\1\2]-\2[^\1\2]-\1") dolocal k = string.match(par,"\1%s*(.-)%s*\2")local v = string.match(par,"\2%s*(.-)%s*\1")if v and not (v=="" and string.find(k,"^flag alias")) thenpart[k] = v end endreturn part else return {}end end

function p.getalias(frame) --Returns a single parameter value from a data template local part = p.gettable(frame,frame.args[1])if frame.args.variantthen return tostring(part[frame.args[2].."-"..frame.args.variant]or part[frame.args[2]] or frame.args.def) else return tostring(part[frame.args[2]] or frame.args.def) end end

function p.gettemplate(frame) --For testing, recreates the country data from the created Lua table --Get data tablelocal data = p.gettable(frame,frame.args[1])--Concatenate fields into a template-like stringlocal out = "{{ 1"for k,v in pairs(data) doout = out.."\n| "..k.." = "..v endreturn out.."\n}}" end

return p