Moduł:Brudnopis/Paweł Ziemian/Podpis

Z Wikipedii, wolnej encyklopedii

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

local resources = {
	dates = {
		{ pattern="^(%d%d):(%d%d), (%d%d?) (%a+) (%d%d%d%d)$", year=5, month=4, day=3, hour=1, minute=2 },
		{ pattern="^(%d%d):(%d%d), (%a+) (%d%d?), (%d%d%d%d)$", year=5, month=3, day=4, hour=1, minute=2 },
		{ pattern="^(%d%d):(%d%d), (%d%d%d%d) (%a+) (%d%d?)$", year=3, month=4, day=5, hour=1, minute=2 },
		{ pattern="^(%d%d%d%d)%-(%d%d)%-(%d%d)T(%d%d):(%d%d):%d%d$", year=1, month=2, day=3, hour=4, minute=5 },
		{ pattern="^(%d%d):(%d%d), (%d%d?)%. (%a+)%.? (%d%d%d%d)$", year=5, month=4, day=3, hour=1, minute=2 },
	},

	months = {
		["sty"] = { "1", "01", "styczeń",     "stycznia",     "January",   "Jan.", "января",   },
		["lut"] = { "2", "02", "luty",        "lutego",       "February",  "Feb.", "февраля",  },
		["mar"] = { "3", "03", "marzec",      "marca",        "March",     "Mär.", "марта",    },
		["kwi"] = { "4", "04", "kwiecień",    "kwietnia",     "April",     "Apr.", "апреля",   },
		["maj"] = { "5", "05",                "maja",         "May",       "Mai",  "мая",      },
		["cze"] = { "6", "06", "czerwiec",    "czerwca",      "June",      "Jun.", "июня",     },
		["lip"] = { "7", "07", "lipiec",      "lipca",        "July",      "Jul.", "июля",     },
		["sie"] = { "8", "08", "sierpień",    "sierpnia",     "August",    "Aug.", "августа",  },
		["wrz"] = { "9", "09", "wrzesień",    "września",     "September", "Sep.", "сентября", },
		["paź"] = { "10",      "październik", "października", "October",   "Okt.", "октября",  },
		["lis"] = { "11",      "listopad",    "listopada",    "November",  "Nov.", "ноября",   },
		["gru"] = { "12",      "grudzień",    "grudnia",      "December",  "Dez.", "декабря",  },
	},

	ltrs = "\226\128\142 ", -- E2 80 8E 20
	rtls = "\226\128\143 ", -- E2 80 8F 20
	illegalUserNameCharPattern = "[%[%]@|#/\a\b\f\n\r\t\v\127]",
	prefix = "-- niepodpisany komentarz użytkownika ",
	suffix = ". Wstawił ",
	contributionPage = "Specjalna:Wkład/",
	talkName = "dyskusja",
}

local parseMonth = function(month)
	if resources.months[month] then
		return month
	end
	
	for k, v in pairs(resources.months) do
		for _, n in ipairs(v) do
			if month == n then
				return k
			end
		end
	end
	
	return month
end

local killDirection = function(text)
	text = mw.text.trim(text or "")
	text = mw.ustring.gsub(text, mw.text.trim(resources.rtls), "")
	text = mw.ustring.gsub(text, mw.text.trim(resources.ltrs), "")
	return text
end

local parseDate = function(date)
	date = killDirection(date)
	for _, p in ipairs(resources.dates) do
		local params = { mw.ustring.match(date, p.pattern) }
		if #params > 0 then
			local status, result = pcall(string.format, "%s:%s, %s %s %s", params[p.hour], params[p.minute], params[p.day], parseMonth(params[p.month]), params[p.year])
			if status then
				return result
			end
		end
	end
end
	
local parseUser = function(user)
	user = killDirection(user)
	if (#user > 0) and not mw.ustring.match(user, resources.illegalUserNameCharPattern) then
		local userTitle = mw.title.new(user, 2)
		local talkTitle = mw.title.new(user, 3)
		if userTitle then
			return tostring(talkTitle), userTitle.exists
		end
	end
end

local splitDateUser = function(text)
	-- tekst skopiowany z historii edycji ma znak specjalny po dacie
	local rtl = false
	local ofss, ofse = mw.ustring.find(text, resources.ltrs, 1, true)
	if not ofss then
		ofss, ofse = mw.ustring.find(text, resources.rtls, 1, true)
		rtl = true
	end
	if ofss and ofse then
		d = mw.ustring.sub(text, 1, ofss - 1)
		u = mw.ustring.sub(text, ofse + 1)
		return d, u, rtl
	end
	
	-- tekst wpisany ręcznie musi zaczynać się datą, a kończyć nazwą usera
	for _, p in ipairs(resources.dates) do
		local pattern = mw.ustring.gsub(p.pattern, "%$$", " ")
		ofss, ofse = mw.ustring.find(text, pattern)
		if (ofss == 1) and (ofse < #text) then
			d = mw.ustring.sub(text, 1, ofse - 1)
			u = mw.text.trim(mw.ustring.sub(text, ofse + 1))
			if #u > 0 then
				return d, u, false
			end
		end
	end
	
end

local formatSignature = function(user, talk, exists, rtl, date, sig)
	local result = mw.html.create("small")
		:wikitext(resources.prefix)
		:wikitext("[[", exists and tostring(mw.title.new(user, 2)) or resources.contributionPage..user, "|", rtl and resources.rtls or "", user, rtl and resources.ltrs or "", "]] ")
		:wikitext("([[", tostring(mw.title.new(user, 3)), "|", resources.talkName, "]])")
		
	if date then
		result:wikitext(" ", date)
	end
	
	if sig and (#sig > 0) then
		result:wikitext(resources.suffix, sig)
	end
	
	return tostring(result)
end

return {

Podpisz = function (frame)
	local date = false
	local user = false
	local rtl = false
	local talk = false
	local exists = false
	
	local arg1 = frame:getParent().args[1]
	local arg2 = frame:getParent().args[2]
	local sig = frame:getParent().args["wstawił"]
	if not sig and mw.isSubsting() then
		sig = "~~~~"
	end

	if arg1 and not arg2 then
		date, user, rtl = splitDateUser(arg1)
		date = parseDate(date)
		user = user or arg1
	elseif arg1 and arg2 then
		date1 = parseDate(arg1)
		date2 = parseDate(arg2)
		if date1 and not date2 then
			date = date1
			user = arg2
		elseif not date1 and date2 then
			date = date2
			user = arg1
		end
	end

	talk, exists = parseUser(user)

	if not talk then
		mw.log("??? brak usera ???")
		return
	end
	
	if mw.isSubsting() and not date then
		mw.log("??? brak daty ???")
		return
	end
	
	local result = formatSignature(user, talk, exists, rtl, date, sig)
	mw.logObject(result, "result")
	return result
end,

}