Moduł:Wikidane/format/string

Z Wikipodróży

Dedykowana uniwersalna wtyczka formatująca wartość cechy z wartością łańcuchową.

Wtyczka obsługuje parametr format.

brak
oryginalna wartość lub link do zewnętrznego serwisu w postaci [url wartość].
nie
zawsze oryginalna wartość
url
wygenerowany adres URL do serwisu zewnętrznego jeśli odpowiedni wzór jest dostępny, w przeciwnym razie oryginalna wartość
inny
tytuł w linku zewnętrznym lub łańcuch formatujący

Łańcuch formatujący tym odróżnia się od tytułu, że zawiera sekwencje ((...)). Niektóre z nich są zamieniane przez odpowiednio sformatowaną wartość cechy. Lista obsługiwanych sekwencji obejmuje następujące transformacje:

((value))
oryginalna wartość
((lc))
konwersja do małych liter
((lcfirst))
konwersja pierwszej litery do małej
((uc))
konwersja do wielkich liter
((ucfirst))
konwersja pierwszej litery do wielkiej
((nospaces))
usunięcie wszelkich odstępów
((0-9))
pozostawienie tylko cyfr
((0-9X))
pozostawienie cyfr i znaku X
((QUERY))
konwersja URI w stylu QUERY
((PATH))
konwersja URI w stylu PATH
((WIKI))
konwersja URI w stylu WIKI
((ANCHOR))
konwersja URI w stylu ANCHOR
((encode))
zamienia wybrane znaki na encje HTML
((nowiki))
zamienia wybrane znaki na encje HTML aby zapobiec ich interpretowaniu jaki wikikod
((trim))
obcina skrajne znaki odstępu
((Moduł:nazwa modułu|nazwa funkcji))
wskazana dedykowana funkcja formatująca z podanego modułu, do której wartość jest przekazana jako 1 element w tablicy
((Szablon:nazwa szablonu))
wskazany dedykowany szablon formatujący wyniki, do którego wartość jest przekazana jako {{{1}}}

Uwaga! Jeśli łańcuch formatujący ma wygenerować link to należy jawnie go zdefiniować w postaci wikikodu na przykład format=[http://jakiś.adres.url/((PATH)) Zobacz ((value)) w jakiejś bazie].

Zobacz też[edytuj]



Na stronie Moduł:Wikidane/format/string wykorzystano treści ze strony Moduł:Wikidane/format/string w polskiej Wikipedii (link do wersji).
Jej autorzy wymienieni są w historii edycji. Prawa autorskie: na licencji Creative Commons CC-BY-SA.



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)
		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 select = require("Module:Wikidane/select").selectProperty
	local pid, qid, prop = select("P1630", {}, property)
	if not qid then
		pid, qid, prop = select("P1921", {}, property)
	end

	if prop 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,

}