לדלג לתוכן

יחידה:כרונולוגיה

מתוך המכלול, האנציקלופדיה היהודית

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:כרונולוגיה/תיעוד

-- Module:כרונולוגיה
local p = {}

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

local function isYes(v)
	v = type(v) == 'string' and mw.text.trim(v) or v
	return v == 'כן'
end

local function notEmpty(v)
	return type(v) == 'string' and mw.text.trim(v) ~= ''
end

local function expand(frame, title, args)
	return frame:expandTemplate{ title = title, args = args or {} }
end

-- רקע הכותרת לפי סינגל/אלבום/סיבוב הופעות וסוג
local function headerBg(frame, args)
	local kind = args['סוג'] or ''
	if isYes(args['סינגל']) then
		return expand(frame, 'סינגל/רקע', { kind })
	elseif isYes(args['אלבום']) then
		return expand(frame, 'אלבום/רקע', { kind })
	elseif isYes(args['סיבוב הופעות']) then
		return expand(frame, 'סיבוב הופעות/רקע', { kind })
	else
		-- ברירת מחדל: נשתמש ברקע אלבום כדי לשמור תאימות
		return expand(frame, 'אלבום/רקע', { kind })
	end
end

-- כותרת התיבה (כמו בוויקיקוד, עם תמיכה בסיבוב הופעות)
local function boxTitle(frame, args)
	local country = args['מדינה']
	if notEmpty(country) then
		return ('כרונולוגיית שירי %s באירוויזיון'):format(country)
	end

	if isYes(args['כרונולוגיית אלבומים']) then
		local plural = expand(frame, 'כרונולוגיה/סוג ברבים', { args['סוג'] or '' })
		return ('כרונולוגיית %s של %s'):format(plural, args['מאת'] or '')
	end

	-- ברירת מחדל: "כרונולוגיית <סינגלים/אלבומים/סיבובי הופעות> של ..."
	local softSpace = expand(frame, 'רווח קל')
	local left = ''
	if isYes(args['סינגל']) then
		left = softSpace .. 'סינגלים של'
	end
	if isYes(args['אלבום']) then
		left = softSpace .. 'אלבומים של'
	end
	if isYes(args['סיבוב הופעות']) then
		left = softSpace .. 'סיבובי הופעות של'
	end
	return ('כרונולוגיית%s %s'):format(left, args['מאת'] or '')
end

-- "(שנה במוזיקה|...)" אם יש תאריך
local function musicYearParen(frame, dateStr)
	if notEmpty(dateStr) then
		return ' (' .. expand(frame, 'שנה במוזיקה', { dateStr }) .. ')'
	end
	return ''
end

-- חילוץ שנה מספרית גסה
local function extractYearNum(s)
	if notEmpty(s) then
		local y = tonumber((s:match('(%d%d%d%d)')))
		return y
	end
	return nil
end

-- סיומת השנה לפי הלוגיקה של הוויקיקוד (כולל "שיר אירוויזיון" ±1)
local function edgeYearSuffix(frame, args, edge) -- 'קודמת' / 'נוכחית' / 'הבאה'
	if edge == 'נוכחית' then
		return musicYearParen(frame, args['תאריך יצירה'])
	end

	local explicit = args['תאריך יצירה ' .. edge]  -- 'תאריך יצירה קודמת' / '... הבאה'
	if notEmpty(explicit) then
		return musicYearParen(frame, explicit)
	end

	if (args['סוג'] or '') == 'שיר אירוויזיון' then
		local baseY = extractYearNum(args['תאריך יצירה'])
		if baseY then
			local y = edge == 'קודמת' and (baseY - 1) or (baseY + 1)
			return ' (' .. expand(frame, 'שנה במוזיקה', { tostring(y) }) .. ')'
		end
	end
	return ''
end

-- תא כרונולוגיה (קודם/נוכחי/הבא)
local function chronoCell(frame, args, which, emphasized)
	local nameKey = (which == 'נוכחי') and 'שם' or ('יצירה ' .. which)
	local name2Key = (which == 'נוכחי') and 'שם2' or ('יצירה ' .. which .. '2')

	local name = args[nameKey]
	if which == 'נוכחי' and not notEmpty(name) then
		name = expand(frame, 'שם הדף בלי הסוגריים')
	end
	local name2 = args[name2Key]

	local tmplArgs = {
		['סינגל']         = args['סינגל'] or '',
		['אלבום']         = args['אלבום'] or '',
		['הדגשה']         = emphasized and 'כן' or 'לא',
		['שם']            = name or '',
		['שם2']           = name2 or ''
	}

	local core = expand(frame, 'עיצוב יצירות מוזיקליות', tmplArgs)

	local suffix =
		(which == 'נוכחי') and edgeYearSuffix(frame, args, 'נוכחית')
		or (which == 'קודמת' and edgeYearSuffix(frame, args, 'קודמת'))
		or edgeYearSuffix(frame, args, 'הבאה')

	-- שמירה על ירידת שורה בין שם לשנה
	if notEmpty(suffix) then
		return core .. '<br />' .. suffix
	else
		return core
	end
end

-- רוחב תא לפי קיום קודם/הבא
local function cellWidthPerc(hasPrev, hasNext, which)
	if which == 'נוכחי' then
		if hasPrev and hasNext then return '33.33' end
		if hasPrev or hasNext then return '50' end
		return '100'
	end
	if hasPrev and hasNext then return '33.33' end
	return '50'
end

function p.data(frame)
	-- קורא את הפרמטרים שהועברו לתבנית כרונולוגיה (הורה ישיר של ה-#invoke)
	local args = getArgs(frame, { parentOnly = true, frameOnly = false, removeBlanks = false })

	-- מאפייני טבלה לפי הוויקיקוד
	local tableStyle = 'text-align:right; border:0; margin:0; padding:0; display:table;'

	local classes = {}
	if isYes(args['הסתרה']) then
		table.insert(classes, 'mw-collapsible')
		table.insert(classes, 'mw-collapsed')
	end
	-- תמיכה ידנית בפרמטר "טבלה-מחלקה" אם הוזן ישירות (לשמירת תאימות)
	if notEmpty(args['טבלה-מחלקה']) then
		table.insert(classes, mw.text.trim(args['טבלה-מחלקה']))
	end
	local classAttr = next(classes) and table.concat(classes, ' ') or nil

	-- כיתוב הכותרת ורקע
	local captionStyle = 'background: ' .. headerBg(frame, args) .. '; width:100%; font-size:100%; display:table-caption'
	if notEmpty(args['כותרת תבנית-עיצוב']) then
		captionStyle = args['כותרת תבנית-עיצוב']
	end
	local titleText = args['כותרת'] or boxTitle(frame, args)

	-- קיום קודם/הבא
	local hasPrev = notEmpty(args['יצירה קודמת'])
	local hasNext = notEmpty(args['יצירה הבאה'])

	-- בניית ה-HTML
	local root = mw.html.create('table')
		:attr('width', '100%')  -- כמו בוויקיקוד המקורי
		:attr('style', (tableStyle or '') .. '; table-layout:fixed')

	if classAttr then
		root:addClass(classAttr)
	end

	root:tag('caption')
		:attr('style', captionStyle)
		:wikitext(titleText)

	if hasPrev or hasNext then
		local tr = root:tag('tr')

		if hasPrev then
			tr:tag('td')
				:attr('style', 'text-align:center; width:' .. cellWidthPerc(hasPrev, hasNext, 'קודמת') .. '%')
				:wikitext( chronoCell(frame, args, 'קודמת', false) )
		end

		tr:tag('td')
			:attr('style', 'text-align:center; width:' .. cellWidthPerc(hasPrev, hasNext, 'נוכחי') .. '%')
			:wikitext( chronoCell(frame, args, 'נוכחי', true) )

		if hasNext then
			tr:tag('td')
				:attr('style', 'text-align:center; width:' .. cellWidthPerc(hasPrev, hasNext, 'הבאה') .. '%')
				:wikitext( chronoCell(frame, args, 'הבאה', false) )
		end
	end

	local html = tostring(root)

	-- קטגוריית שגיאה (כמו במקור) – רק במרחב הראשי ורק אם לא צוין שום סוג
	local titleObj = mw.title.getCurrentTitle()
	if titleObj.namespace == 0
		and (not isYes(args['אלבום']))
		and (not isYes(args['סינגל']))
		and (not isYes(args['סיבוב הופעות'])) then
		html = html .. '[[קטגוריה:שגיאות פרמטריות בתבנית כרונולוגיה]]'
	end

	return html
end

return p