پرش به محتوا

پودمان:Running header

از سورسکال

توضیحات این پودمان می‌تواند در پودمان:Running header/توضیحات قرار گیرد.

--[=[
	Implements [[الگو:سرصفحهٔ مستمر]]

	RTL localization of Module:RunningHeader (English Wikisource) for
	Persian Wikisource.

	Supported named parameters (Persian and English both work):
		راست  / right              -> cell shown on the right side of the header
		وسط، میان / center, centre -> single middle cell (classic 3-cell layout)
		راست-میان, چپ-میان         -> the two inner cells of a 4-cell layout,
		                              used instead of وسط/میان (see
		                              get_alias_groups below)
		چپ   / left                -> cell shown on the left side of the header
		کلاس / class               -> extra CSS class for the whole header

	About the RTL layout: cells are still built into the DOM in the same
	order as the English version (cell 1 is still built first). It's the
	accompanying CSS (direction:rtl on .wst-rh) that makes the browser lay
	them out right to left. So cell 1 ends up on the right and the last
	cell ends up on the left -- and left=... always puts content on the
	visual left, right=... always on the visual right, whether you write
	the Persian or the English name.
]=]

require('strict')

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

local p = {}

-- Returns the alias-group table to use for this call. راست/right (position 1)
-- and چپ/left (the last position) are always accepted. The middle is either
-- a single cell (وسط/میان -- the classic 3-cell layout) or two cells
-- (راست-میان + چپ-میان -- a 4-cell layout), whichever the caller actually
-- used; giving راست-میان or چپ-میان switches on the 4-cell scheme.
-- Note: since راست/وسط/میان/چپ etc. aren't ASCII identifiers, they're always
-- accessed with brackets (args['راست']) rather than dot notation
-- (args.راست) -- dot notation isn't reliable for these keys in Lua/Scribunto.
local function get_alias_groups(args)
	if args['راست-میان'] or args['چپ-میان'] then
		return {
			{pos = 1, names = {'راست', 'right'}},
			{pos = 2, names = {'راست-میان', 'وسط', 'میان', 'center', 'centre'}},
			{pos = 3, names = {'چپ-میان'}},
			{pos = 4, names = {'چپ', 'left'}},
		}
	end
	return {
		{pos = 1, names = {'راست', 'right'}},
		{pos = 2, names = {'وسط', 'میان', 'center', 'centre'}},
		{pos = 3, names = {'چپ', 'left'}},
	}
end

function p._running_header(args)
	-- holds tracking categories
	local tracking_cats = {}

	-- was at least one named parameter (Persian or English) used?
	local used_named_alias = false
	-- was more than one source (positional and/or named aliases) given for the same position?
	local has_duplicate = false

	local alias_groups = get_alias_groups(args)

	for _, group in ipairs(alias_groups) do
		local resolved = nil
		local sources_defined = 0

		if args[group.pos] then
			sources_defined = sources_defined + 1
			resolved = args[group.pos]
		end

		for _, name in ipairs(group.names) do
			if args[name] then
				sources_defined = sources_defined + 1
				used_named_alias = true
				resolved = resolved or args[name]
			end
		end

		if sources_defined > 1 then
			has_duplicate = true
		end

		args[group.pos] = resolved
	end

	if used_named_alias then
		-- this is fine but it's worth keeping track of
		table.insert(tracking_cats, '[[رده:' .. 'سرصفحه‌های مستمر با نام پارامترهای صریح' .. ']]')
	end
	if has_duplicate then
		-- this is a real problem worth flagging
		table.insert(tracking_cats, '[[رده:' .. 'صفحه‌های دارای آرگومان تکراری در فراخوانی الگو' .. ']]')
	end

	-- alias for the class parameter
	args.class = args.class or args['کلاس']

	if used_named_alias then
		-- using a named alias at all (even just one) commits to this
		-- scheme's full cell count -- so the sibling positions the caller
		-- never mentioned are blank, not "undefined". Without this,
		-- {{...|میان=X}} alone would look like a 2-cell header missing
		-- its first cell, instead of a 3-cell header with two blank sides.
		local last_pos = alias_groups[#alias_groups].pos
		for i = 1, last_pos do
			if args[i] == nil then
				args[i] = ''
			end
		end
	end

	-- get number of cells (largest-numbered parameter)
	-- can't use #args because that doesn't work consistently on tables that aren't sequences
	-- table.maxn also seems not to work
	local cell_count = 0
	for k, v in pairs(args) do
		local i = tonumber(k)
		if i and i > cell_count then
			cell_count = i
		end
	end

	-- track headers which don't set the contents of every cell
	local undefined_entries = false

	-- track how many cells have content
	local content_entries = 0

	for i = 1, cell_count do
		if not args[i] then
			undefined_entries = true
		elseif args[i] and args[i] ~= '' then
			content_entries = content_entries + 1
		end
	end

	if undefined_entries then
		-- track headers with undefined entries (fine but worth keeping track of)
		table.insert(tracking_cats, '[[رده:' .. 'سرصفحه‌های مستمر با ورودی‌های تعریف‌نشده' .. ']]')
	end
	if content_entries == 1 then
		-- track headers with only one non-blank entry
		table.insert(tracking_cats, '[[رده:' .. 'سرصفحه‌های مستمر با تنها یک ورودی دارای محتوا' .. ']]')
	end

	if cell_count == 0 or content_entries == 0 then
		-- track headers with no entries (pointless)
		table.insert(tracking_cats, '[[رده:' .. 'سرصفحه‌های مستمر خالی' .. ']]')
	elseif cell_count == 1 then
		-- track 1-cell headers (fine but worth keeping track of)
		table.insert(tracking_cats, '[[رده:' .. 'سرصفحه‌های مستمر با یک ورودی' .. ']]')
	elseif cell_count == 2 then
		-- track 2-cell headers (fine but worth keeping track of)
		table.insert(tracking_cats, '[[رده:' .. 'سرصفحه‌های مستمر با دو ورودی' .. ']]')
	elseif cell_count > 4 then
		-- track headers with more than 4 cells (fine but worth keeping track of)
		table.insert(tracking_cats, '[[رده:' .. 'سرصفحه‌های مستمر با بیش از چهار ورودی' .. ']]')
	end

	-- TEMPORARY FOR MIGRATION: enforce 3-cell minimum
	cell_count = math.max(cell_count, 3)

	-- assemble header
	local base_class = 'wst-rh'
	local header_class = table.concat({
		base_class,
		base_class .. '-' .. cell_count,
		args.class or (base_class .. '-default')
	}, ' ')

	local header_div = mw.html.create('div')
		:addClass(header_class)
		:attr('dir', 'rtl')

	-- cell classes
	local cell_class = base_class .. '-cell'
	local extra_cell_classes = {
		[3] = {'wst-rh-right', 'wst-rh-center', 'wst-rh-left'},
		[4] = {'wst-rh-right', 'wst-rh-right-middle', 'wst-rh-left-middle', 'wst-rh-left'},
	}
	extra_cell_classes = extra_cell_classes[cell_count] or {}

	for i = 1, cell_count do
		local cell = header_div:tag('div')
			:addClass(cell_class)
			:wikitext(args[i] or '')

		if extra_cell_classes[i] then
			cell:addClass(extra_cell_classes[i])
		end
	end

	return tostring(header_div) .. table.concat(tracking_cats)
end

function p.running_header(frame)
	local args = getArgs(frame, {trim = true, removeBlanks = false})
	return p._running_header(args)
end

return p