Module:CountryData: Difference between revisions
From Zoophilia Wiki
Jump to navigationJump to search
meta>SiBr4 No edit summary |
meta>SiBr4 mNo edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function gettable(frame,country,params) | function p.gettable(frame,country,params) | ||
--Returns the parameters of a country data template as a Lua table | --Returns the parameters of a country data template as a Lua table | ||
--If not a valid data template, return empty table | --If not a valid data template, return empty table | ||
Line 29: | Line 29: | ||
function p.getalias(frame) | function p.getalias(frame) | ||
--Returns a single parameter value from a data template | --Returns a single parameter value from a data template | ||
local part = gettable(frame,frame.args[1]) | local part = p.gettable(frame,frame.args[1]) | ||
return tostring(part[frame.args[2]]) | return tostring(part[frame.args[2]]) | ||
end | end | ||
Line 36: | Line 36: | ||
--For testing, recreates the country data from the created Lua table | --For testing, recreates the country data from the created Lua table | ||
--Get data table | --Get data table | ||
local data = gettable(frame,frame.args[1]) | local data = p.gettable(frame,frame.args[1]) | ||
--Concatenate fields into a template-like string | --Concatenate fields into a template-like string | ||
local out = "{{ {{{1}}}" | local out = "{{ {{{1}}}" |
Revision as of 10:02, 21 August 2015
Documentation for this module may be created at Module:CountryData/doc
local p = {}
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 table
if string.find(frame:preprocess("{{Country data "..country.."}}"),"^%{%{ *%{%{%{1")
then
--Params to be passed into data template
params = params or {}
--Transclude template without first parameter for string list of params
local s = frame:expandTemplate({title="Country data "..country;args=params})
--Replace parameter delimiters with arbitrary control characters
--to avoid clashes if param values contain equals/pipe signs
s = string.gsub(s,"|([^|=]-)=","\1\1%1\2")
s = string.gsub(s,"}}%s*$","\1")
--Loop over string and add params to table
local part = {}
for par in string.gmatch(s,"\1[^\1\2]-\2[^\1\2]-\1") do
local k = string.match(par,"\1%s*(.-)%s*\2")
local v = string.match(par,"\2%s*(.-)%s*\1")
part[k] = v
end
return 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])
return tostring(part[frame.args[2]])
end
function p.gettemplate(frame)
--For testing, recreates the country data from the created Lua table
--Get data table
local data = p.gettable(frame,frame.args[1])
--Concatenate fields into a template-like string
local out = "{{ {{{1}}}"
for k,v in pairs(data) do
out = out.."\n| "..k.." = "..v
end
return out.."\n}}"
end
return p