وحدة:Get year

من أرابيكا، الموسوعة الحرة
اذهب إلى التنقل اذهب إلى البحث

local p = {}
-- {{#invoke:Get year|year|+1|y=2016}}
-- {{#invoke:Get year|year|+1|y=2016–17}}
-- {{#invoke:Get year|year|+1|y=2016–17}}

function trim(s)
	-- from PiL2 20.4
	return (s:gsub("^%s*(.-)%s*$", "%1"))
end

function get_year(y, Type)
	if mw.ustring.match(y, "^%d%d%d%d$") then
		return tonumber(y) + (Type == "+1" and 1 or -1)
	end

	local hyph, finish = mw.ustring.match(y, "%d([%–-])(%d+)")
	finish = finish or ""
	if not string.match(finish, "^%d+$") then
		local finish_fixed = mw.ustring.match(finish, "^%s*(%d%d?%d?%d?)%D")
		if finish_fixed then
			finish = finish_fixed
		else
			return ''
		end
	elseif string.len(finish) >= 5 then
		return ''
	end

	local start = string.match(y, "^%d+") or ""
	if not string.match(start, "^%d%d?%d?%d?$") then
		local start_fixed = mw.ustring.match(start, "^%s*(%d%d?%d?%d?)%D")
		if start_fixed then
			start = start_fixed
		end
	end

	local nstart = tonumber(start)
	local nfinish = tonumber(finish)
	local t = 1
	while t <= 10 do
		local nish = nstart + t
		if nish == nfinish or string.match(nish, "%d?%d$") == finish then
			break
		end
		if t == 10 then
			errors =
				p.errorclass(
				'Function nav_hyphen can\'t determine a reasonable term length for "' .. start .. hyph .. finish .. '".'
			)
			return p.failedcat(errors, "K")
		end
		t = t + 1
	end

	local i = Type == "+1" and 1 or -1
	local hgap = 0
	local from = nstart + i * (t + hgap)
	local from2 = string.match(from, "%d?%d$")
	local to = tostring(from + t)
	local to2 = string.match(to, "%d?%d$")
	local tofinal = (t > 1 or (from2 - to2) > 0) and to or to2

	local full = from .. hyph .. tofinal
	local abbr = from .. hyph .. to2

	return full
end

function p.year(frame)
	local args = frame.args
	local y = args.y or args.year or ""
	local Type = args["1"] or args.ty or args.type or ""

	if y == "" or Type == "" then
		return ""
	end

	return get_year(y, Type)
end

function p.get(frame)
    local args = frame.args

    local title = args["1"] or args.pa or args.page or args.title
    local Type = args["2"] or args.ty or args.type

    if title == "" or Type == "" then
        return ""
    end

    title = trim(title)
    Type = trim(Type)

    local year =
        mw.ustring.match(title, "%s%d%d%d%d[%–-]%d%d%d%d$", 1) or mw.ustring.match(title, "%s%d%d[%–-]%d%d%d%d$", 1) or
        mw.ustring.match(title, "%s%d%d%d%d[%–-]%d%d$", 1) or
        mw.ustring.match(title, "%s%d%d%d%d$", 1) or
        mw.ustring.match(title, "^%d%d%d%d%s", 1) or
        mw.ustring.match(title, "%s%d%d%d%d%s", 1) or
        ""

    year = trim(year)
    
    if year == "" then
        mw.log("no year")
        return ""
    end
    
    mw.log("year:" .. year .. ";Type:" .. Type .. ";title:" .. title)
    local y2 = year:gsub("%-", "%%-")
    if Type == "year" or Type == "y" then
        return year
        
    elseif Type == "noyear" or Type == "noy" then
		local t3 = title:gsub("^(.-)%s" .. y2, "%1")
		return t3
    elseif Type == "+1" or Type == "-1" then
        return get_year(year, Type)
        
    elseif Type == "title+1" or Type == "title-1" then
        local tyt = "+1" and Type == "title+1" or "-1"
        year_2 = get_year(year, tyt)
        title3 = title:gsub("^(.-)%s" .. y2, "%1") .. " " .. year_2
        return title3
    end
end

return p