Difference between revisions of "Module:DefInfo"
Jump to navigation
Jump to search
m |
m |
||
Line 1: | Line 1: | ||
− | + | --------------- | |
+ | -- load data -- | ||
+ | --------------- | ||
+ | local Biomes = mw.loadData('Module:Test/data/biomes') | ||
+ | --local Buildings = mw.loadData('Module:Test/data/buildings') | ||
local Races = mw.loadData('Module:Test/data/races') | local Races = mw.loadData('Module:Test/data/races') | ||
− | + | ||
+ | ----------------------- | ||
+ | -- private functions -- | ||
+ | ----------------------- | ||
local function find_key_in_table(key, table) | local function find_key_in_table(key, table) | ||
Line 13: | Line 20: | ||
end | end | ||
end | end | ||
+ | |||
local function parent_defName(def, category) | local function parent_defName(def, category) | ||
local parent_name = category[def]["ParentName"] | local parent_name = category[def]["ParentName"] | ||
− | if type(category[parent_name]) == "table" then return parent_name end | + | if type(category[parent_name]) == "table" then |
+ | return parent_name | ||
+ | end | ||
end | end | ||
+ | |||
local function find_in_parents(tag, def, category) | local function find_in_parents(tag, def, category) | ||
− | if not category[def] then return def .. " not found in category" end | + | if not category[def] then |
+ | return def .. " not found in category" | ||
+ | end | ||
+ | |||
local parent_def = parent_defName(def, category) | local parent_def = parent_defName(def, category) | ||
− | if not parent_def then return tag .." not found in parent defs" end | + | if not parent_def then |
+ | return tag .." not found in parent defs" | ||
+ | end | ||
+ | |||
local found = find_key_in_table(tag, category[parent_def]) | local found = find_key_in_table(tag, category[parent_def]) | ||
if found then return found | if found then return found | ||
else | else | ||
found = find_in_parents(tag, parent_def, category) | found = find_in_parents(tag, parent_def, category) | ||
− | if found then return found end | + | if found then |
+ | return found | ||
+ | end | ||
end | end | ||
end | end | ||
+ | |||
local function query(tag, def, category) | local function query(tag, def, category) | ||
− | if not category[def] then return def .. " not found in category" end | + | if not category[def] then |
+ | return def .. " not found in category" | ||
+ | end | ||
+ | |||
local found = find_key_in_table(tag, category[def]) | local found = find_key_in_table(tag, category[def]) | ||
if found then return found end | if found then return found end | ||
+ | |||
found = find_in_parents(tag, def, category) | found = find_in_parents(tag, def, category) | ||
+ | |||
+ | -- check if found is a table with <li> subelements and notify | ||
+ | -- the user if such is the case | ||
+ | |||
if found then return found end | if found then return found end | ||
+ | |||
return tag .. " not found" | return tag .. " not found" | ||
end | end | ||
+ | |||
+ | -------------------------------- | ||
+ | -- publicly exposed functions -- | ||
+ | -------------------------------- | ||
+ | |||
+ | local p = {} | ||
function p.query(frame) | function p.query(frame) | ||
Line 58: | Line 93: | ||
return tag .. " is not a table (not countable)" | return tag .. " is not a table (not countable)" | ||
end | end | ||
− | elseif sublist_query | + | elseif sublist_query then |
− | return queried[sublist_query] | + | return queried[tonumber(sublist_query)] |
end | end | ||
− | return queried | + | return queried |
end | end | ||
+ | -- os.clock() may be logging time that is not the time I want | ||
mw.log("Module:DefInfo:os.clock() " .. os.clock()*1000 .. " ms") | mw.log("Module:DefInfo:os.clock() " .. os.clock()*1000 .. " ms") | ||
return p | return p |
Revision as of 15:14, 30 March 2021
Dev version at Module:Test
--------------- -- load data -- --------------- local Biomes = mw.loadData('Module:Test/data/biomes') --local Buildings = mw.loadData('Module:Test/data/buildings') local Races = mw.loadData('Module:Test/data/races') ----------------------- -- private functions -- ----------------------- local function find_key_in_table(key, table) for k, v in pairs(table) do if k == key then return v elseif type(v) == "table" then local found = find_key_in_table(key, v) if found then return found end end end end local function parent_defName(def, category) local parent_name = category[def]["ParentName"] if type(category[parent_name]) == "table" then return parent_name end end local function find_in_parents(tag, def, category) if not category[def] then return def .. " not found in category" end local parent_def = parent_defName(def, category) if not parent_def then return tag .." not found in parent defs" end local found = find_key_in_table(tag, category[parent_def]) if found then return found else found = find_in_parents(tag, parent_def, category) if found then return found end end end local function query(tag, def, category) if not category[def] then return def .. " not found in category" end local found = find_key_in_table(tag, category[def]) if found then return found end found = find_in_parents(tag, def, category) -- check if found is a table with <li> subelements and notify -- the user if such is the case if found then return found end return tag .. " not found" end -------------------------------- -- publicly exposed functions -- -------------------------------- local p = {} function p.query(frame) local category = frame.args[1] local def = frame.args[2] local tag = frame.args[3] local sublist_query = frame.args[4] if category == "Races" then category = Races elseif category == "Biomes" then category = Biomes else return "undefined category" end local queried = query(tag, def, category) if sublist_query == "count" then if type(queried) == "table" then return table.getn(queried) else return tag .. " is not a table (not countable)" end elseif sublist_query then return queried[tonumber(sublist_query)] end return queried end -- os.clock() may be logging time that is not the time I want mw.log("Module:DefInfo:os.clock() " .. os.clock()*1000 .. " ms") return p