Moduł:Brudnopis/Paweł Ziemian/Wikidane/formatSnak

Z Wikipedii, wolnej encyklopedii

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Brudnopis/Paweł Ziemian/Wikidane/formatSnak/opis

local moduleData = mw.loadData("Module:Wikidane/data")

local function formatNoValue(snak, options)
	return options.novalue
		or moduleData.noValueDescription[snak.property]
		or moduleData.noValueDescription[1]
end

local function formatSomeValue(snak, options)
	return options.somevalue
		or moduleData.someValueDescription[snak.property]
end

local parser = {
	use = "snaktype",
	choose = {
		["novalue"] = formatNoValue,
		
		["somevalue"] = formatSomeValue,
		
		["value"] = {
			use = "datatype",
			choose = {
				["string"] = {
					enter = "datavalue",
					use = "type",
					choose = {
						["string"] = "Moduł:Brudnopis/Paweł Ziemian/Wikidane/formatString",
					},
				},
			
				["time"] = {
					enter = "datavalue",
					use = "type",
					choose = {
						["time"] = {
							enter = "value",
							use = "calendarmodel",
							choose = {
								["http://www.wikidata.org/entity/Q1985727"] = "Moduł:Brudnopis/Paweł Ziemian/Wikidane/formatProlepticGregorianCalendar",
							},
						},
					},
				},
			
				["wikibase-item"] = {
					enter = "datavalue",
					use = "type",
					choose = {
						["wikibase-entityid"] = {
							enter = "value",
							use = "entity-type",
							choose = {
								["item"] = "Moduł:Brudnopis/Paweł Ziemian/Wikidane/formatWikibaseEntityItem",
							},
						},
					},
				},
			
				["quantity"] = {
					enter = "datavalue",
					use = "type",
					choose = {
						["quantity"] = {
							enter = "value",
							use = "unit",
							choose = "Moduł:Brudnopis/Paweł Ziemian/Wikidane/formatQuantity",
						}
					},
				},
			
				["globe-coordinate"] = {
					enter = "datavalue",
					use = "type",
					choose = {
						["globecoordinate"] = "Moduł:Brudnopis/Paweł Ziemian/Wikidane/formatGlobeCoordinates",
						-- TODO difference by globe EW, W, E
					},
				},
			
				["monolingualtext"] = {
					enter = "datavalue",
					use = "type",
					choose = {
						["monolingualtext"] = "Moduł:Brudnopis/Paweł Ziemian/Wikidane/formatMonolingualText",
					},
				},
			
				-- end of "snak.datatype"
			},
		},
	
		-- end of "snaktype"
	},
}

return {
	
scope = "snak",

format = function(snak, options)
	mw.logObject(parser, "parser")
	mw.logObject(snak, "snak")
	mw.logObject(options, "options")
	local p = parser
	local d = snak
	while true do
		if p.enter then
			mw.log("enter: "..p.enter)
			d = d[p.enter]
		end
		
		mw.log("use: "..p.use)
		local s = d[p.use]
		if not s then
			mw.log("cannot retrive data '"..p.use.."' to determine formatter")
			return nil
		end
		
		mw.log("choose: "..s)
		local c = p.choose[s]
		if not c then
			mw.log("unrecognized choice '"..s.."' from '"..p.use.."' when determining formatter")
			return
		end
		
		if type(c) == "function" then
			return c(snak, options)
		end
		
		if type(c) == "string" then
			return require(c).format(snak, options)
		end
		
		if type(c) ~= "table" then
			mw.log("unexpected decoder type")
			return
		end
		
		p = c
	end
end,

}