Moduł:Wikidane/format/string

Z Polska Encyklopedia Numizmatyczna
Przejdź do nawigacji Przejdź do wyszukiwania

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Wikidane/format/string/opis

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

local transformations = {
	["lc"] = function(s)
		return mw.getContentLanguage():lc(s)
	end,
	
	["lcfirst"] = function(s)
		return mw.getContentLanguage():lcfirst(s)
	end,
	
	["uc"] = function(s)
		return mw.getContentLanguage():uc(s)
	end,
	
	["ucfirst"] = function(s)
		return mw.getContentLanguage():ucfirst(s)
	end,
	
	["nospaces"] = function(s)
		return mw.ustring.gsub(s, "%s", "")
	end,
	
	["0-9"] = function(s)
		return mw.ustring.gsub(s, "[^0-9]", "")
	end,
	
	["0-9X"] = function(s)
		return mw.ustring.gsub(s, "[^0-9X]", "")
	end,
	
	["query"] = function(s)
		local t = mw.uri.encode(s, "QUERY")
		local q, c = string.gsub(t, "%%2F", '/' )
		return q
	end,
	
	["QUERY"] = function(s)
		return mw.uri.encode(s, "QUERY")
	end,
	
	["PATH"] = function(s)
		return mw.uri.encode(s, "PATH")
	end,
	
	["WIKI"] = function(s)
		return mw.uri.encode(s, "WIKI")
	end,
	
	["ANCHOR"] = function(s)
		return mw.uri.anchorEncode(s)
	end,
	
	["encode"] = function(s)
		return mw.text.encode(s)
	end,
	
	["nowiki"] = function(s)
		return mw.text.nowiki(s)
	end,
	
	["trim"] = function(s)
		return mw.text.trim(s)
	end,
}

local function propertyLinkFormat(property)
	local prop = mw.wikibase.getBestStatements(property, "P1630")
	if not prop or (#prop == 0) then
		prop = mw.wikibase.getBestStatements(property, "P1921")
	end
	
	if prop and (#prop > 0) then
		local snak = prop[1].mainsnak
		if (snak.snaktype == "value") and (snak.datatype == "string") and (snak.datavalue.type == "string") then
			return snak.datavalue.value
		end
	end
end

local function loadFormat(format, formatLink)
	if format == moduleData.boolNo then
		return false
	end
	
	local url = false
	if formatLink then
		local count
		url, count = string.gsub(formatLink, "$1", "((query))")
		if count <= 0 then
			url = false
		end
	end

	if format == "url" then
		return url
	end
	
	if url and not format then
		return "["..url.." ((nowiki))]"
	end
	
	if url and format and not string.match(format, "%(%([^%(%)]+%)%)") then
		return "["..url.." "..format.."]"
	end
	
	return format
end

local function findComplexTransformation(capture)
	-- próba wywołania funkcji z modułu
	local m, f = mw.ustring.match(capture, "^([^%|\n]+)%|([^%|\n]+)$")
	if m and f then
		local title = mw.title.new(m)
		if title and (title.namespace == 828) and title.exists then
			local transformationFunction = require(m)[f]
			if type(transformationFunction) == "function" then
				local succeeded, text = pcall(transformationFunction, {""}) -- próba wywołania z pustym argumentem
				if succeeded then
					return function(value)
						local t = transformationFunction({value}) or ""
						return tostring(t)
					end
				end
			end
		end
		
		return false
	end
	
	-- próba wywołania szablonu
	local s = mw.ustring.match(capture, "^[^%|\n]+$")
	if s then
		local title = mw.title.new(s)
		if title and (title.namespace == 10) and title.exists then
			local frame = mw.getCurrentFrame()
			if frame then
				return function(value)
					return frame:expandTemplate{ title=title.text, args={value} }
				end
			end
		end
		
		return false
	end
	
	return false
end

return {
	
scope = "snak",

format = function(snak, options)
	local format = loadFormat(options.format, propertyLinkFormat(snak.property))
	local value = snak.datavalue.value
	if not format or not value then
		return value
	end
	
	-- prepare cache with "precomputed" direct value
	-- the cache allows to compute value once, which is used mostly twice
	local cache = { value = value }
	
	local repl = function(capture)
		local result = cache[capture]
		if not result then
			local transformation = transformations[capture]
				or findComplexTransformation(capture)
			result = transformation and transformation(value) or value
			cache[capture] = result
		end
		
		return result
	end
	
	return mw.ustring.gsub(format, "%(%(([^%(%)]+)%)%)", repl)
end,

}