Module:Sandbox/User:Ahecht/Inflation

local p={}

local function split(text, pattern, plain)
    local ret = {}
    local s, l = 1, string.len( text )
    while s do
    	local e, n = string.find( text, pattern, s, plain )
    	if not e then
    		table.insert(ret, string.sub ( text, s ))
    		s = nil
    	elseif n < e then
    		-- Empty separator!
    		table.insert(ret, string.sub ( text, s, e ))
    		if e < l then
    			s = e + 1
    		else
    			s = nil
    		end
    	else
    		table.insert(ret, e > s and string.sub( text, s, e - 1 ) or '')
    		s = n + 1
    	end
    end
    return ret
end

function p._parse(index, template, frame)
	if not frame then frame = mw.getCurrentFrame() end
	local error = ' when using {{tl|Inflation/' .. template .. '}}.{{main other|[[Category:Pages with errors in inflation template]]}}</span>'
	if index and index ~= 'ERR' then
		local tempTitle = "Inflation/" .. index .. "/dataset"
		local dataset = mw.title.new(tempTitle, "Template"):getContent()
		if dataset then
			local redirect = mw.ustring.match(dataset, "#REDIRECT%s*%[%[Template:Inflation/(.*)/dataset%]%]")
			if redirect then return p._parse(redirect, template, frame)	end
			dataset = mw.ustring.gsub(mw.ustring.gsub(dataset, "<!%-%-.-%-%->", ""), "\n?%s*|%s*#default%s*", "")
			local datatable = {}
			local data = split(dataset, "\n", true)
			for _, row in ipairs(data) do
				local year, value = mw.ustring.match(row, "%s*|%s*(%d%d%d%d)%s*=%s*([^=]+)%s*")
				if year and value and tonumber(year) then
					year = tonumber(year)
					if tonumber(value) then
						datatable[year] = tonumber(value)
					elseif mw.ustring.sub(value,1,8) == '{{#expr:' and mw.ustring.sub(value,-2) == '}}' then
						value = mw.ustring.sub(value, 3, -3)
						datatable[year] = tonumber(frame:callParserFunction( value ))
					end
				end
			end
			if table.maxn(datatable) > 0 then
				return datatable
			else
				error = '<span class="error">Error: unable to parse "Template:' .. tempTitle .. '"' .. error
			end
		else
			error = '<span class="error">Error: undefined index "' .. index .. '"' .. error
		end
	else
		error = '<span class="error">Error: no index specified' .. error
	end
	return frame:preprocess(error)
end

function p.year(frame)
	local index = frame.args.index or frame.args[1] or frame:getParent().args.index or frame:getParent().args[1]
	local result = p._parse(index, 'year', frame)
	if type(result) == "table" then
		return table.maxn(result)
	else
		return result
	end
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.