Editing Module:Test
Jump to navigation
Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
DefInfo = {} | DefInfo = {} | ||
local wiki = {} | local wiki = {} | ||
− | |||
− | |||
− | |||
− | |||
------------------------------------------------------------------ | ------------------------------------------------------------------ | ||
Line 11: | Line 7: | ||
if mw then | if mw then | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Util = require("Module:Test/lib/util") | Util = require("Module:Test/lib/util") | ||
Search = require("Module:Test/lib/search") | Search = require("Module:Test/lib/search") | ||
− | + | Data = mw.loadData('Module:Test/data') | |
− | + | frame = mw and mw.getCurrentFrame() | |
+ | log = mw.log | ||
+ | logObject = mw.logObject | ||
else | else | ||
+ | logStore = {} | ||
+ | |||
+ | Util = require "lib/util" | ||
+ | Search = require "lib/search" | ||
+ | Data = require "data/data" | ||
+ | Inspect = require 'lib/inspect' | ||
+ | VF = require "data/virtual" | ||
− | + | function pp(tbl, title) -- pretty print tables | |
+ | Util.hl(title) | ||
+ | print(Inspect(tbl)) | ||
+ | end | ||
log = function(str) | log = function(str) | ||
− | table.insert( | + | table.insert(logStore, str) |
end | end | ||
Line 39: | Line 36: | ||
if prefix then | if prefix then | ||
assert(type(prefix) == "string") | assert(type(prefix) == "string") | ||
− | table.insert( | + | table.insert(logStore, prefix .. " = " .. Inspect(obj)) |
else | else | ||
− | table.insert( | + | table.insert(logStore, Inspect(obj)) |
end | end | ||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
Line 70: | Line 47: | ||
----------------------- | ----------------------- | ||
− | function DefInfo.vardefine(name, value | + | local function runTime() |
+ | local clock = string.format("%i", os.clock() * 1000) | ||
+ | return clock | ||
+ | end | ||
+ | |||
+ | function DefInfo.vardefine(name, value) | ||
assert(name, "vardefine: missing argument #1 (variable to definePrefix)") | assert(name, "vardefine: missing argument #1 (variable to definePrefix)") | ||
assert(type(name) == "string", string.format("vardefine: bad argument #1 (string expected, got %s)", type(name))) | assert(type(name) == "string", string.format("vardefine: bad argument #1 (string expected, got %s)", type(name))) | ||
assert(value, "vardefine: missing argument #2 (value to assign)") | assert(value, "vardefine: missing argument #2 (value to assign)") | ||
assert(type(value) == "string" or type(value) == "number" or type(value) =="boolean", string.format("vardefine: bad argument #2 (string, number or boolean expected, got %s)", type(value))) | assert(type(value) == "string" or type(value) == "number" or type(value) =="boolean", string.format("vardefine: bad argument #2 (string, number or boolean expected, got %s)", type(value))) | ||
− | |||
frame:callParserFunction('#vardefine', name, value) | frame:callParserFunction('#vardefine', name, value) | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
Line 110: | Line 81: | ||
end | end | ||
− | function DefInfo.getDef(defID | + | function DefInfo.getDef(defID) |
− | |||
− | |||
local ignoreKeys = {"Abstract", "Name", "ParentName"} | local ignoreKeys = {"Abstract", "Name", "ParentName"} | ||
local baseDef | local baseDef | ||
Line 133: | Line 102: | ||
def = DefInfo.mergeParents(baseDef, ignoreKeys) | def = DefInfo.mergeParents(baseDef, ignoreKeys) | ||
− | + | VF.expand(def) | |
return def | return def | ||
Line 153: | Line 122: | ||
end | end | ||
− | local function definePrefixed(tbl | + | local function definePrefixed(tbl) |
for k,v in pairs(tbl) do | for k,v in pairs(tbl) do | ||
if type(v) ~= 'table' then | if type(v) ~= 'table' then | ||
local mt = getmetatable(tbl) | local mt = getmetatable(tbl) | ||
log(string.format('%s = %s', mt[k], tostring(v))) | log(string.format('%s = %s', mt[k], tostring(v))) | ||
− | if mw then DefInfo.vardefine(mt[k], v | + | if mw then DefInfo.vardefine(mt[k], v) end |
else | else | ||
− | definePrefixed(v | + | definePrefixed(v) |
end | end | ||
end | end | ||
Line 168: | Line 137: | ||
-- public interface -- | -- public interface -- | ||
---------------------- | ---------------------- | ||
+ | |||
+ | function wiki.getDefName(frame) | ||
+ | local defName | ||
+ | local label = frame.args[1] | ||
+ | |||
+ | if not label then | ||
+ | log(runTime()) | ||
+ | log("getDefName: missing argument #1 (label)") | ||
+ | logObject(frame.args, "frame.args") | ||
+ | return nil | ||
+ | end | ||
+ | |||
+ | for defID,def in pairs(Data) do | ||
+ | if string.upper(def.label or "") == string.upper(label) then | ||
+ | defName = def.defName | ||
+ | end | ||
+ | end | ||
+ | |||
+ | if not defName then | ||
+ | log(runTime()) | ||
+ | log(string.format("getDefName: '%s' not found", label)) | ||
+ | logObject(frame.args, "frame.args") | ||
+ | return nil | ||
+ | end | ||
+ | |||
+ | log(runTime()) | ||
+ | return defName | ||
+ | end | ||
function wiki.count(frame) | function wiki.count(frame) | ||
local query = wiki.query(frame) | local query = wiki.query(frame) | ||
− | if | + | if queried then |
− | return | + | return #queried |
end | end | ||
end | end | ||
Line 197: | Line 194: | ||
end | end | ||
− | local | + | local prune = def |
for i,arg in ipairs(frame.args) do -- arguments | for i,arg in ipairs(frame.args) do -- arguments | ||
Line 203: | Line 200: | ||
if i == argLen and frame.args["sibling"] then | if i == argLen and frame.args["sibling"] then | ||
− | + | prune = Search.find({nil, frame.args["sibling"]} , prune) | |
− | if not | + | if not prune then |
logObject(frame.args, string.format("query @ %ims: bad argument 'sibling' ('%s' not found')\nframe.args", runTime(), frame.args["sibling"])) | logObject(frame.args, string.format("query @ %ims: bad argument 'sibling' ('%s' not found')\nframe.args", runTime(), frame.args["sibling"])) | ||
return nil | return nil | ||
else | else | ||
− | + | prune = Search.meta.parent.table[arg] | |
− | if not | + | if not prune then |
logObject(frame.args, string.format("query @ %ims: bad argument #%i ('%s' is not a sibling of '%s')", runTime(), i, arg, frame.args["sibling"])) | logObject(frame.args, string.format("query @ %ims: bad argument #%i ('%s' is not a sibling of '%s')", runTime(), i, arg, frame.args["sibling"])) | ||
return nil | return nil | ||
Line 217: | Line 214: | ||
if i < argLen or i == argLen and not frame.args["sibling"] then | if i < argLen or i == argLen and not frame.args["sibling"] then | ||
− | + | prune = Search.find(arg, prune) | |
− | if not | + | if not prune then |
logObject(frame.args, string.format("query @ %ims: bad argument #%i ('%s' not found)\nframe.args", runTime(), i, frame.args[i])) | logObject(frame.args, string.format("query @ %ims: bad argument #%i ('%s' not found)\nframe.args", runTime(), i, frame.args[i])) | ||
return nil | return nil | ||
else | else | ||
− | if type( | + | if type(prune) ~= 'table' and i < argLen then |
log(string.format("query @ %ims: warning Def ['%s'] argument #%i ('%s' returns a value, all extra arguments ignored)", runTime(), def['label'], i, frame.args[i])) | log(string.format("query @ %ims: warning Def ['%s'] argument #%i ('%s' returns a value, all extra arguments ignored)", runTime(), def['label'], i, frame.args[i])) | ||
− | return | + | return prune |
end | end | ||
end | end | ||
Line 231: | Line 228: | ||
end -- for arguments | end -- for arguments | ||
− | if type( | + | if type(prune) == "table" then |
− | log(string.format("@%ims, | + | log(string.format("@%ims, table vardefined", runTime())) |
− | setPrefix( | + | setPrefix(prune, frame.args[argLen]) |
− | definePrefixed( | + | definePrefixed(prune) |
− | + | queried = prune | |
return nil | return nil | ||
end | end | ||
− | log(string.format("@%ims, | + | log(string.format("@%ims, %s printed", runTime(), type(prune))) |
− | return | + | return prune |
+ | |||
end | end | ||
− | + | --------------------------------- | |
− | -- simulate | + | -- simulate module invocation -- |
− | + | --------------------------------- | |
if not mw then | if not mw then | ||
local simframe = { ["args"] = {} } | local simframe = { ["args"] = {} } | ||
− | simframe.args['label'] = ' | + | simframe.args['label'] = 'fennec fox' |
− | + | simframe.args[1] = 'livesIn' | |
− | -- | + | -- simframe.args[2] = 'label' |
− | wiki.query(simframe) | + | local query = wiki.query(simframe) |
+ | if query then print(query) end | ||
end | end | ||
+ | |||
+ | ---------------------------------------- | ||
+ | -- simulate wiki log while developing -- | ||
+ | ---------------------------------------- | ||
if not mw then | if not mw then | ||
Util.hl("DefInfo log") | Util.hl("DefInfo log") | ||
− | for _,v in ipairs( | + | for _,v in ipairs(logStore) do |
print(v) | print(v) | ||
end | end | ||
end | end | ||
− | |||
− | |||
− | |||
− | |||
if mw then | if mw then |