模組:沙盒/a2569875/DataReadTest
外观
local p={}
local lib_arg = {}
local yesno = {}
function p.getWikitextTimes(frame)
local args, working_frame
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame, {parentFirst=true})
working_frame = frame
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
working_frame = mw.getCurrentFrame()
if type(args) ~= type({}) then args = {frame} end
end
local input_title = args[1] or args['1']
local input_arg = args[2] or args['2']
local Times = tonumber(args.Times) or 1
local body = ''
for testtime = 1,Times do
body = body .. '\n*{{' .. input_title .. '|' .. input_arg .. '}}'
end
return working_frame:preprocess(body)
end
function p.getJSONTimes(frame)
local args, working_frame
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. The args are passed through to the module
-- from the template page, so use the args that were passed into the template.
if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
args = lib_arg.getArgs(frame, {parentFirst=true})
working_frame = frame
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
args = frame
working_frame = mw.getCurrentFrame()
if type(args) ~= type({}) then args = {frame} end
end
local input_title = args[1] or args['1']
local Times = tonumber(args.Times) or 1
local key_alias = args['key alias'] or args.key_alias
if (key_alias or '') ~= '' then
if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
key_alias = yesno(key_alias or 'no')
end
local default = args["default"] or ''
local fullText = mw.title.new( input_title, "Template" ):getContent()
local jsonText = ''
if fullText then
local checker, begin = mw.ustring.find(fullText, "<%s*source%s*lang%s*=%s*['\"]?[Jj][Ss][Oo][Nn]['\"]?%s*>")
local finish, __noop = mw.ustring.find(fullText, "<%s*/%s*source[^>]*>")
if checker then
jsonText = mw.ustring.sub(fullText,begin+1,finish-1)
end
end
local body = ''
for testtime = 1,Times do
local JSONobj = mw.text.jsonDecode( jsonText )
local iterator = 2
while args[iterator] ~= nil do
if key_alias then
local check_key = JSONobj[args[iterator]]
if type(check_key) == type("string") then
local check_key_str, find_key_str = mw.ustring.gsub(check_key,"<%s*target%s*>(.-)<%s*/%s*target%s*>", '%1')
mw.log(args[iterator] .. ', find:' .. check_key_str)
while find_key_str > 0 and mw.text.trim(check_key_str) ~= '' and JSONobj[check_key_str] ~= nil do
check_key = JSONobj[check_key_str]
if type(check_key) == type("string") then
check_key_str, find_key_str = mw.ustring.gsub(check_key,"<%starget%s>(.-)<%s/%starget%s>", '%1')
mw.log(' =>\"' .. check_key_str .. '\"')
else break end
end
end
JSONobj = check_key
else JSONobj = JSONobj[args[iterator]] end
if JSONobj == nil and default ~= nil then
body = body .. '\n*' .. default
end
iterator = iterator + 1
end
if type(JSONobj) == type({}) then
body = body .. '\n*' .. default
end
body = body .. '\n*' .. JSONobj
end
return body
end
return p