Module:SMW JSON: Difference between revisions

From RuneRealm Wiki
Jump to navigation Jump to search
Content added Content deleted
(Created page with "local p = {} function p.main(frame) local field = frame.args.field local parent_args = frame:getParent().args p._main(field, parent_args) end function p._main(field, parent_args) if field ~= nil then local args = {} for k, v in pairs(parent_args) do if v == '' then args[k] = nil else args[k] = mw.text.unstrip(v) end end mw.logObject(args) local json = mw.text.jsonEncode(args) mw.smw.set({[field] = json}) end end function p.parse(data,...")
 
(No difference)

Latest revision as of 00:13, 17 October 2024

Documentation for this module may be created at Module:SMW JSON/doc

local p = {}

function p.main(frame)
	local field = frame.args.field
	local parent_args = frame:getParent().args
	p._main(field, parent_args)
end

function p._main(field, parent_args)
	if field ~= nil then
		local args = {}
		for k, v in pairs(parent_args) do
			if v == '' then
				args[k] = nil
			else
				args[k] = mw.text.unstrip(v)
			end
		end
		mw.logObject(args)
		local json = mw.text.jsonEncode(args)
		mw.smw.set({[field] = json})
	end
end

function p.parse(data, field)
	if data == nil then
		return {}
	end
	
	local out = {}
	for _,task in ipairs(data) do
		local raw = task[field]
		if type(raw) == "string" then
			local json = mw.text.jsonDecode(mw.text.decode(raw))
			json['_page'] = task[1]
			table.insert(out, json)
		elseif type(raw) == "table" then
			for _,v in ipairs(raw) do
				local json = mw.text.jsonDecode(mw.text.decode(v))
				json['_page'] = task[1]
				table.insert(out, json)
			end
		end
	end
	return out
end

return p