Module:Yeararticle

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:Yeararticle/doc

Code

local p = {}

require('strict')
local getArgs = require('Module:Arguments').getArgs

local function link(year, bc, nolink)
	local bc_text = (bc and ' BC') or ''
	local format = (nolink and '* %d%s') or '* [[%d%s]]'
	return string.format(format, year, bc_text)
end

function p.main(frame)
	local args = getArgs(frame)
	local title = mw.title.getCurrentTitle().text
	local year, bc = nil, false
	year = tonumber(title)
	if not year then
		year = tonumber((title:gsub(' BC$', '')))
		bc = true
	end
	if not year and args.demo then
		year = tonumber(os.date('%Y'))
		bc = false
	end
	if not year then
		return nil
	end
	local ret = { mw.getCurrentFrame():extensionTag('templatestyles', '', {src='Yeararticle/styles.css'}) }
	table.insert(ret, '<div class="yearnavbox hlist">')
	for i = year-3, year+3 do
		table.insert(ret, link(i, bc, i==year))
	end
	table.insert(ret, '</div>')
	table.insert(ret, mw.getCurrentFrame():expandTemplate{title='Yeararticle/Languages', args={year, (bc and ' BC' or nil)}})
	return table.concat(ret, '\n')
end

return p