Dokumentacja modułu [zobacz] [edytuj] [historia] [odśwież]

Moduł techniczny do obsługi dokumentacji map lokalizacyjnych.

opis edytuj

Funkcja do generowania opisu modułu z definicją mapy.

Błędy edytuj

Błędy należy zgłaszać na stronie Wikibooks:Kwestie techniczne.
local function Infobox(builder, standardMap, frame)
	local width = 240
	
	local function map(name, mandatory)
		local result = standardMap[name]
		if not result and mandatory then
			result = "{{{"..name.."}}}"
		end
		
		return result
	end
	
	builder:wikitext(
		'{| class="infobox" cellpadding="0"\n',
		"|-\n",
		'|  colspan="2" style="font-weight:bold; text-align:center; color:grey;" | Mapa lokalizacyjna ', map("dopełniacz", true)
	):tag("br"):done():wikitext(
		"[[Plik:", map("mapa", true), "|", tostring(width), "px|Mapa lokalizacyjna ", map("dopełniacz", true), "]]\n"
	)
	
	if not map("bez mapy świata") then
		local worldMap = mw.loadData("Module:Mapa/dane/świat")
		local reliefInfo = worldMap[1]["dopełniacz"] or "świata"
		local reliefImage = worldMap[1]["mapa"] or "World location map.svg"
		local relief = builder:wikitext(
			"|-\n",
			'| colspan="2" style="font-weight:bold; text-align:center; color:grey; border-top:1px solid #aaa;" | Położenie na mapie ', reliefInfo
		):tag("br"):done()
		:tag("div")
			:addClass("mapa-lokalizacyjna")
			:css({ margin ="0 auto", width = tostring(width + 2).."px", padding = "3px" })
			:tag("div")
				:css({ position = "relative", border = "0 solid #aaa", padding = "0", width = tostring(width).."px" })
				:wikitext("[[Plik:", reliefImage, "|", width, "px|link=|Mapa lokalizacyjna ", reliefInfo, "]]")
		local border = "1px solid "..(map("x") and "green" or "red")
		local top = tostring(map("top")/-1.8+50).."%"
		local bottom = tostring(map("bottom")/1.8+50).."%"
		local left = tostring(map("left")/3.6+50).."%"
		local right = tostring(map("right")/-3.6+50).."%"
		relief:tag("div")
			:css({ border = border, position = "absolute", top = top, bottom = bottom, left = left, right = map("left") >= map("right") and "-1px" or right, })
		if map("left") >= map("right") then
			relief:tag("div")
				:css({ border = border, position = "absolute", top = top, bottom = bottom, left = "-1px", right = right, })
		end
		if map("x") then
			builder:tag("span")
				:css({ ["font-style"] = "italic", ["font-weight"] = "normal", })
				:wikitext("Zaznaczone granice mają charakter jedynie orientacyjny.")
		end
		builder:wikitext("\n")
	end
	
	local templateName = map("szablon") or map("nazwa alias") or map("link alias")
	local templateNameVariant = templateName
	if templateName == "Jugosławia" then
		templateNameVariant = "złożona"
	end
	if templateName then
		local template = mw.title.new("Szablon:państwo dane "..templateName)
		if template and template.exists then
			builder
				:wikitext(
					"|-\n",
					'! colspan="2" | ', "''Szablon odpowiada jednostce administracyjnej zdefiniowanej w szablonie''"
				)
				:tag("br"):done()
				:wikitext(frame:expandTemplate{ title = "s", args = { template.text }}, "\n")
				:wikitext(frame:expandTemplate{ title = "infobox państwo dodaj", args = { templateName, wariant=templateNameVariant }}, "\n")
		end
	end

	if map("globe") ~= nil then
		builder:wikitext(
			"|-\n",
			"! Opcja „globe” dla geohack\n",
			"| ", map("globe") or "''nie linkuje''", "\n"
		)
	end

	if map("dokładność") then
		builder:wikitext(
			"|-\n",
			"! Opcja „dokładność” dla ", frame:expandTemplate{ title = "s", args = { "współrzędne" }}, "\n",
			"| ", map("dokładność"), "\n"
		)
	end

	if map("ikona") then
		builder:wikitext(
			"|-\n",
			"! Opcja „ikona” dla współrzędnych\n",
			"| ", map("ikona"), "\n"
		)
	end

	local parent = map("nadrzędna")
	local parentHeader = "Mapą nadrzędną dla tej mapy"
	while parent do
		local c = mw.title.getCurrentTitle()
		local parentTitle = mw.title.new(c.baseText.."/"..parent, c.nsText)
		local parentMap = false
		if parentTitle.exists then
			local s, m = pcall(mw.loadData, parentTitle.fullText)
			if s and (type(m[1]) == "table") then
				parentMap = m
			end
		end
	
		builder:wikitext(
			"|-\n",
			'| colspan="2" style="font-weight:bold; text-align:center; color:grey; border-top:1px solid #aaa;" | '
		)
		if parentMap then
			local d = parentMap[1]["dopełniacz"] or "{{{dopełniacz}}"
			local p = parentMap[1]["mapa"] or "{{{mapa}}}"
			builder:wikitext(
				parentHeader
			):tag("br"):done():wikitext(
				"jest mapa ", d
			)
			
			if map("x") or parentMap[1].x then
				builder:tag("br"):done():wikitext("[[Plik:", p, "|", tostring(width), "px|Mapa lokalizacyjna ", d, "]]\n")
			else
				-- mapa z ramką
				local function getX(longitude, left, right)
					if longitude == left then
						return 0
					elseif longitude == right then
						return 100
					end
					
					local width = (right - left) % 360
					if width == 0 then
						width = 360
					end
					local distanceFromLeft = (longitude - left) % 360
					-- the distance needed past the map to the right equals distanceFromLeft - width. the distance needed past the map to the left equals 360 - distanceFromLeft. to minimize page stretching, go whichever way is shorter
					if distanceFromLeft - width / 2 >= 180 then
						distanceFromLeft = distanceFromLeft - 360
					end
					return 100 * distanceFromLeft / width
				end
				
				local function getY(latitude, top, bottom)
					return 100 * (top - latitude) / (top - bottom)
				end
				
				local r = builder:tag("div")
					:addClass("mapa-lokalizacyjna")
					:css({ margin ="0 auto", width = tostring(width + 2).."px", padding = "3px" })
					:tag("div")
						:css({ position = "relative", border = "0 solid #aaa", padding = "0", width = tostring(width).."px" })
						:wikitext("[[Plik:", p, "|", width, "px|link=|Mapa lokalizacyjna ", d, "]]")
				local border = "1px solid "..(map("x") and "green" or "red")
				local top = tostring(getY(map("top"), parentMap[1].top, parentMap[1].bottom)).."%"
				local bottom = tostring(100-getY(map("bottom"), parentMap[1].top, parentMap[1].bottom)).."%"
				local left = tostring(getX(map("left"), parentMap[1].left, parentMap[1].right)).."%"
				local right = tostring(100-getX(map("right"), parentMap[1].left, parentMap[1].right)).."%"
				r:tag("div")
					:css({ border = border, position = "absolute", top = top, bottom = bottom, left = left, right = right, })
				builder:wikitext("\n")
				
			end
			parent = parentMap[1]["nadrzędna"]
			parentHeader = "Kolejną nadrzędną"
		else
			builder	:wikitext(
				"Mapa nadrzędna nie istnieje[[Kategoria:Szablony lokalizacyjne - brak mapy nadrzędnej]]\n"
			)
			parent = false
		end
	end

	local linkAlias = map("link alias")
	if linkAlias and (#linkAlias > 0) then
		builder:wikitext(
			"|-\n",
			'! colspan="2" | Wykorzystanie\n',
			"|-\n",
			'| colspan="2" style="vertical-align:top; text-align:center;" | [[Specjalna:Linkujące/Moduł:Mapa/linkujące/', linkAlias, "/primary", "|Infoboksy]]<br />[[Specjalna:Linkujące/Moduł:Mapa/linkujące/", linkAlias, "/standalone", "|Mapy lokalizacyjne]]\n"
		)
	end

	builder:wikitext(
		"|-\n",
		"|}\n"
	)
end

local function StandardParameters(builder, standardMap)
	builder:wikitext("== Parametry standardowe ==\n")
	local wikitable = builder:tag("table")
		:addClass("wikitable")
		:tag("tr")
			:tag("th"):wikitext("nazwa parametru"):done()
			:tag("th"):wikitext("wartość"):done()
			:tag("th"):wikitext("wynik"):done()
		:done()
	
	local function appendRow(paramName, mandatory, description)
		local value = standardMap[paramName]
		if value or mandatory then
			local tr = wikitable:tag("tr")
			tr:tag("td"):tag("tt"):wikitext(paramName)
			if value then
				tr:tag("td"):tag("tt"):wikitext(value)
			else
				tr:tag("td"):tag("span"):css("color", "red"):wikitext("'''niezdefiniowany!'''")
				value = "{{{"..paramName.."}}}"
			end
			tr:tag("td"):wikitext(mw.ustring.format(description, value,value))
		end
	end
	
	appendRow("link alias", true, "Główny artykuł ([[w:%s|%s]])")
	appendRow("nazwa alias", false, "Skrócona nazwa (do wyświetlania) o ile różna od nazwy artykułu")
	appendRow("dopełniacz", true, "Dopełniacz nazwy, używany np. do generowania różnych nazw tj. ''Mapa %s''.")
	appendRow("skrót", false, "Skrót [[w:ISO 3166-1 alfa-3|ISO 3166-1 alfa-3]] lub [[w:ISO 3166-2|ISO 3166-2]].")
	appendRow("nadrzędna", false, "Nazwa mapy nadrzędnej, umieszczonej w module [[Moduł:Mapa/dane/%s]].")
	
	builder:wikitext("\n")
end

local function makeMap(map, default, source)
	return function(name)
		return name == nil and source or (map[name] or default[name])
	end
end

local function Map(builder, map, variant, example)
	builder:wikitext('{| class="wikitable" style="text-align:center; width:700px;"\n')
	if variant then
		builder:wikitext('! colspan="4" style="color:blue; font-weight:bold;" | <tt>wariant=', variant, '</tt>\n')
	end
	builder:wikitext(
		"|-\n",
		'| colspan="4" | [[Plik:', map("mapa"), "|240px|Mapa lokalizacyjna ", map("dopełniacz"), "]]\n",
		"|-\n",
		'| colspan="4" | '
	):tag("code"):wikitext("[[:Plik:", map("mapa"), "|", map("mapa"), "]]"):done():wikitext(
		"\n",
		"|-\n"
	)
	if map("x") then
		builder:wikitext(
		'! colspan="4" | Współrzędne nieliniowe\n',
		"|-\n",
		'| colspan="4" style="text-align:left;" | x = ', map("x"), "\n",
		"|-\n",
		'| colspan="4" style="text-align:left;" | y = ', map("y"), "\n"
		)
	else
		builder:wikitext(
		'! colspan="4" | Współrzędne graniczne\n',
		"|-\n",
		"|\n",
		'| colspan="2" | ', map("top"), "\n",
		"|\n",
		"|-\n",
		'| style="width:33%;" | ', map("left"), "\n",
		'| style="width:34%;" colspan="2" | ←↕→\n',
		'| style="width:33%;" | ', map("right"), "\n",
		"|-\n",
		"|\n",
		'| colspan="2" | ', map("bottom"), "\n",
		"|\n"
		)
	end
	
	if map("right") and map("left") and (map("right") < map("left")) then
		builder:wikitext(
		"|-\n",
		'| colspan="4" | ', "'''UWAGA!''' Szablon przekracza południk 180°\n"
		)
	end

	if example then
		builder:wikitext(
			"|-\n",
			'! colspan="4" | Przykład\n',
			"|-\n",
			'| colspan="4" style="vertical-align:top; text-align:left;" |\n',
			example
		)
	end

	local linkAlias = map("link alias")
	if linkAlias and (#linkAlias > 0) then
		builder:wikitext(
			"\n|-\n",
			'! colspan="4" | Zobacz też\n',
			"|-\n",
			'| colspan="4" style="vertical-align:top; text-align:left;" |\n',
			"* [[Specjalna:Linkujące/Moduł:Mapa/linkujące/", linkAlias, "/primary/", variant or 1, "|Podstawowe wykorzystanie w infoboksach]]\n",
			"* [[Specjalna:Linkujące/Moduł:Mapa/linkujące/", linkAlias, "/standalone/", variant or 1, "|Wykorzystanie w mapach lokalizacyjnych]]"
		)
	end

	builder:wikitext(
		"\n|}\n"
	)
end

local function Example(frame, map, variant)
	local example = map("przykład")
	local draw = false
	local text = mw.html.create():wikitext(
		"{{mapa lokalizacyjna\n",
		" |mapa        = ",  mw.title.new(map()).subpageText, "\n",
		variant and (" |wariant     = "..variant.."\n") or ""
	):done()

	if example and example[1] then
		local points = {}
		text:wikitext(" |punkty mapy = \n")
		for _, v in ipairs(example) do
			table.insert(points, require("Module:Współrzędne").punkt({ v[1], opis=v[2] }))
			text:wikitext("{{mapa lokalizacyjna/punkt|", v[1], v[2] and "|opis=" or "", v[2], "}}\n")
		end
		draw = require("Module:Mapa").Thumb({ map(), wariant=variant, ["punkty mapy"] = table.concat(points)})
	else
		draw = require("Module:Mapa").Thumb({ map(), wariant=variant })
	end
	
	text:wikitext("}}")
	local example = tostring(text)
	local maxWidth = 0
	for v in mw.text.gsplit(example, "\n") do
		local w = mw.ustring.len(v)
		maxWidth = w > maxWidth and w or maxWidth
	end
	
	local attributes = nil
	if maxWidth > 44 then
		attributes = { style="white-space: pre; clear:right;" }
	end
	
	local result = tostring(draw)..frame:extensionTag("pre", example, attributes)
	return result
end

local function Footer(builder, standardMap)
	local title = mw.title.getCurrentTitle()
	local content = title:getContent()
	local index = mw.ustring.match(title.text, "^Mapa/dane/(.*)$") or title.text
	local directCategories = false
	for category in mw.ustring.gmatch(content, "%[%[[Kk][Aa][Tt][Ee][Gg][Oo][Rr][Ii][Aa]:([^\n\t|%[%]]+)%]%]") do
		local c = mw.text.trim(category)
		if #c > 0 then
			builder:wikitext("[[Kategoria:", c, "|", index, "]]")
			directCategories = true
		end
	end
	if directCategories then
		return
	end
	
	local link = standardMap["link alias"]
	local name = standardMap["nazwa alias"]
	local abbr = standardMap["skrót"]
	local template = standardMap.szablon
	
	if name or link then
		builder:wikitext(mw.getCurrentFrame():callParserFunction("DEFAULTSORT", name or link), "\n")
	end
	
	builder:wikitext("[[Kategoria:Szablony lokalizacyjne - szablony ", standardMap.kategoria or "państw", "]]")
	if abbr then
		if (#abbr == 0) or (link == abbr) then
			builder:wikitext("[[Kategoria:Szablony lokalizacyjne - błędny skrót]]")
		end
		
		if (#abbr > 0) and not mw.title.new("Module:Mapa/dane/"..abbr).exists then
			builder:wikitext("[[Kategoria:Szablony lokalizacyjne - nieistniejący skrót]]")
		end
	end
	
	if name then
		if (#name == 0) or (name == link) then
			builder:wikitext("[[Kategoria:Szablony lokalizacyjne - błędna nazwa]]")
		end
	end

	if title.subpageText == abbr then
		local content = title:getContent()
		local redirTo = mw.ustring.match(content, '^%s*return%s+require%s*%(%s*"(.-)"%s*%)%s*$')
		if not redirTo then
			builder:wikitext("[[Kategoria:Szablony lokalizacyjne - brak określonej poprawnie nazwy]]")
		end
	elseif (title.subpageText ~= (template or name or link)) or (template == (name or link)) then
		builder:wikitext("[[Kategoria:Szablony lokalizacyjne - brak określonej poprawnie nazwy]]")
	end
	
	if not standardMap.top and not standardMap["bez mapy świata"] then
		builder:wikitext("[[Kategoria:Szablony lokalizacyjne - brak oznaczenia na mapie świata]]")
	end
	
	if standardMap["rozmiar"] then
		local width = tonumber(standardMap["rozmiar"]) or 238
		if width > 238 then
			builder:wikitext("[[Kategoria:Szablony lokalizacyjne - za duży rozmiar w infoboksach]]")
		end
	end
end

local function SelectVariants(mapData)
	local index = {
		["fizyczna"] = -3,
		["kolej"] = -2,
		["hydro"] = -1,
	}
	if mapData[1].par then
		for i, v in ipairs(mapData[1].par) do
			if not index[v] then
				index[v] = i
			end
		end
	end
	
	local variants = {}
	for k, v in pairs(mapData) do
		if (type(k) == "string") and (#k > 0) and (type(v) == "table") then
			table.insert(variants, k)
			if not index[k] then
				local y = tonumber(k)
				if y and (y >= 1000) and (y <= 9999) then
					y = 11000 - y
				else
					local fold = mw.getContentLanguage():caseFold(k)
					y = 11000 + mw.ustring.codepoint(fold, 1, 1) + mw.ustring.len(fold)
				end
				
				index[k] = y
			end
		end
	end

	table.sort(variants, function(a, b) return index[a] < index[b] end)

	return variants
end

return {
	['opis'] = function(frame)
		local moduleName = mw.title.getCurrentTitle().fullText
		local status, mapData = pcall(mw.loadData, moduleName)
		if not status then
			-- logowanie komunikatu błędu może być przydatne
			mw.log(mapData)
			return
		end
		if type(mapData[1]) ~= "table" then
			-- nieprawidłowa definicja mapy
			mw.log("brak danych")
			return
		end
		
		local builder = mw.html.create()
		builder
			:tag("tt"):wikitext(mw.title.getCurrentTitle().fullText):done()
			:wikitext(" zawiera dane, które są używane przez szablony map lokalizacyjnych. ''Zobacz więcej w [[w:Wikiprojekt:Szablony lokalizacyjne|Wikiprojekcie Szablony lokalizacyjne]].''\n")
		Infobox(builder, mapData[1], frame)
		StandardParameters(builder, mapData[1])
		builder:wikitext("== Mapa standardowa ==\n")
		local mainMap = makeMap(mapData[1], {}, moduleName)
		local mainExample = Example(frame, mainMap, false)
		Map(builder, mainMap, false, mainExample )
		local variants = SelectVariants(mapData)
		if #variants > 0 then
			builder:wikitext("== Inne mapy ==\n")
			for _, v in ipairs(SelectVariants(mapData)) do
				local map = makeMap(mapData[v], mapData[1], moduleName)
				local example = Example(frame, map, v)
				local title = mapData[v].opis or v
				builder:wikitext("=== ", title, " ===\n")
				Map(builder, map, v, example)
			end
		end
	
		Footer(builder, mapData[1])
		
		return builder:allDone()
	end,
}