Module:Highlighted date list

-- This module takes a list of dates and adds a marker highlighting the closest
-- date in the future.

local p = {}
local lang = mw.language.getContentLanguage()

local function normalizeDate(timestamp)
	local success, date = pcall(lang.formatDate, lang, 'Y-m-d', timestamp)
	return success and date
end

local function makeData(args)
	local data, unsorted = {}, {}
	for k, v in pairs(args) do
		if type(k) == 'string' then
			local datakey, num = k:match('^(.-)([1-9][0-9]*)$')
			if datakey == 'item' or datakey == 'date' then
				num = tonumber(num)
				unsorted[num] = unsorted[num] or {}
				unsorted[num][datakey] = v
			elseif datakey then
				error(string.format("invalid parameter '%s' detected", k), 3)
			end
		end
	end
	for num, t in pairs(unsorted) do
		if t.item and t.date then
			local date = normalizeDate(t.date)
			if not date then
				error(string.format(
					"invalid date '%s' in parameter 'date%d'",
					t.date, num
				), 3)
			end
			t.date = date
			table.insert(data, t)
		elseif t.item then
			error(string.format(
				"parameter 'item%d' was specified but parameter 'date%d' is missing",
				num, num
			), 3)
		else
			error(string.format(
				"parameter 'date%d' was specified but parameter 'item%d' is missing",
				num, num
			), 3)
		end
	end
	table.sort(data, function (t1, t2)
		return t1.date < t2.date
	end)
	return data
end

local function makeComparisonDate(gracePeriod)
	local timestamp
	if gracePeriod then
		timestamp = 'now - ' .. gracePeriod
	else
		timestamp = 'now'
	end
	local date = normalizeDate(timestamp)
	if date then
		return date
	else
		error(string.format("invalid grace period '%s'", gracePeriod), 3)
	end
end

local function makeHighlighter(color)
	color = color or '#FC6'
	local highlighter = mw.html.create('span')
	highlighter
		:css('background-color', color)
		:wikitext(lang:getArrow('forwards'))
	return tostring(highlighter)
end

function p._main(args)
	local data = makeData(args)
	local comparisonDate = makeComparisonDate(args.graceperiod)
	local highlighter = args.highlighter or makeHighlighter(args.highlightercolor)
	local root = mw.html.create('ul')
	root
		:addClass(args.class)
		:cssText(args.style)
	local doneHighlight = false
	for i, t in ipairs(data) do
		local item
		if not doneHighlight and t.date >= comparisonDate then
			doneHighlight = true
			item = highlighter .. ' ' .. t.item
		else
			item = t.item
		end
		root
			:newline()
			:tag('li')
				:wikitext(item)
	end
	root:newline()
	return tostring(root)
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:Highlighted date list'
	})
	return p._main(args)
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.