پرش به محتوا

پودمان:Citation/CS1/fa/dispatch

نگاه به جهان از دریچه فیلم

توضیحات این پودمان می‌تواند در پودمان:Citation/CS1/fa/dispatch/توضیحات قرار گیرد.

--[[
پودمان تقسیم‌کننده یادکرد — CS1/CS2 citation dispatcher (post-expand fix).

Purpose: let the یادکرد X citation templates be a BARE {{#invoke}} — counted once
in the 2 MB post-expand budget, like en's {{cite web}} — instead of routing
through {{#invoke:Template wrapper}} → {{یادکرد X/هسته}} → {{#invoke}} (counted ~2×).

It reproduces exactly what Template wrapper + the /هسته layer did per type:
  * language fork: reuse پودمان:Citation/lang is_rtl VERBATIM to pick the fa
    module (RTL / Persian sources) or the en module (LTR-language sources); both
    modules accept Persian parameter names natively (verified byte-identical).
  * per-type special-param preprocessing (see each entry point below).
All inside one #invoke, so the citation output is counted once.

Verified byte-identical vs the live templates across real + edge citations.
]]

local p = {}
local citlang = require ('Module:Citation/lang')

local function set (v) return v ~= nil and mw.text.trim (v) ~= ''; end

local function pick_module (args, cite_type)
	local L = args['زبان'] or args['کد زبان'] or args['language'] or args['lang'] or '';
	local tpl = citlang.is_rtl ({args = {type = cite_type, lang = L}});		-- returns a TEMPLATE name; fa names start with «یادکرد»
	if mw.ustring.find (tpl, 'یادکرد', 1, true) then
		return 'Module:citation/CS1/fa';
	end
	return 'Module:Citation/CS1/en';
end

local function copy (args)													-- getParent args are read-only → mutable copy
	local m = {};
	for k, v in pairs (args) do m[k] = v; end
	return m;
end

-- کوشش folded into دیگران as «به کوشش X. <existing others>» (web/book/journal/podcast/magazine/encyclopedia)
local function fold_others (m)
	if set (m['کوشش']) then
		local others = m['دیگران'] or m['others'] or m['نویسندگان سایر بخش‌ها'] or '';
		m['دیگران'] = 'به کوشش ' .. m['کوشش'] .. '. ' .. others;
	end
	m['کوشش'] = nil; m['others'] = nil; m['نویسندگان سایر بخش‌ها'] = nil;
end

-- news: کوشش folded into خبرگزاری (agency)
local function fold_agency (m)
	m['خبرگزاری'] = (set (m['کوشش']) and m['کوشش']) or m['خبرگزاری'] or m['agency'];
	m['کوشش'] = nil; m['agency'] = nil;
end

-- magazine/encyclopedia: ref aliases رف/شناسه (module supplies the harv default)
local function fold_ref (m)
	local r = m['ref'] or m['رف'] or m['شناسه'];
	if r ~= nil then m['ref'] = r; end
	m['رف'] = nil; m['شناسه'] = nil;
end

local function fold_jeldha (m)												-- جلدها → «X جلد»
	if set (m['جلدها']) then m['جلدها'] = m['جلدها'] .. ' جلد'; end
end

-- CS2 (الگو:یادکرد): url-status switch — many Persian/en aliases → dead/live/unfit/usurped
local URL_STATUS_MAP = {
	['yes'] = 'dead', ['y'] = 'dead', ['true'] = 'dead', ['بله'] = 'dead', ['آره'] = 'dead',
	['آری'] = 'dead', ['مرده'] = 'dead', ['dead'] = 'dead', ['deviated'] = 'dead',
	['no'] = 'live', ['n'] = 'live', ['false'] = 'live', ['نه'] = 'live', ['خیر'] = 'live',
	['زنده'] = 'live', ['live'] = 'live', ['unfit'] = 'unfit', ['usurped'] = 'usurped',
};
local AFFIRM = {['yes'] = true, ['y'] = true, ['true'] = true, ['بله'] = true, ['آره'] = true, ['آری'] = true};

local function fold_url_status (m)
	local raw = m['وضعیت پیوند'] or m['وضعیت-پیوند'] or m['url-status'] or m['پیوند مرده'] or m['dead-url'] or m['deadurl'];
	local key = raw and mw.ustring.lower (mw.text.trim (raw)) or '';
	m['url-status'] = URL_STATUS_MAP[key];									-- nil when #default (empty)
	m['وضعیت پیوند'] = nil; m['وضعیت-پیوند'] = nil;
	m['پیوند مرده'] = nil; m['dead-url'] = nil; m['deadurl'] = nil;
end

local function fold_url_access (m)
	local mem = m['عضویت'] and mw.ustring.lower (mw.text.trim (m['عضویت'])) or '';
	local val;
	if AFFIRM[mem] then
		val = 'registration';
	else
		local sub = m['اشتراک'] or m['آبونمان'];
		sub = sub and mw.ustring.lower (mw.text.trim (sub)) or '';
		if AFFIRM[sub] then
			val = 'subscription';
		else
			val = m['دسترسی-پیوند'] or m['دسترسی پیوند'] or m['url-access'];
		end
	end
	m['url-access'] = val;
	m['عضویت'] = nil; m['اشتراک'] = nil; m['آبونمان'] = nil;
	m['دسترسی-پیوند'] = nil; m['دسترسی پیوند'] = nil;
end

-- url-status tracking category (only الگو:یادکرد وب emitted this at template level)
local function url_status_cat_web (m)
	local us = m['url-status'];
	if not set (us) then return ''; end
	if mw.text.trim (us) == 'bot: unknown' then
		return '[[رده:صفحه‌هایی که از یادکرد وب با وضعیت پیوند نامعلوم استفاده می‌کنند]]';
	end
	return '[[رده:صفحه‌هایی که از یادکرد وب با پارامتر وضعیت پیوند استفاده می‌کنند]]';
end

local function emit (frame, class, cite_type, m, trailer)
	local mod = pick_module (m, cite_type);
	return require (mod)._citation (frame, m, {CitationClass = class}) .. (trailer or '');
end

--=== entry points (type -> CitationClass mapping matches the live templates) ===

function p.web (frame)
	local m = copy (frame:getParent().args);
	fold_others (m); fold_jeldha (m);
	return emit (frame, 'web', 'web', m, url_status_cat_web (m));
end

function p.news (frame)
	local m = copy (frame:getParent().args);
	fold_agency (m); fold_jeldha (m);
	return emit (frame, 'news', 'news', m);
end

function p.book (frame)
	local m = copy (frame:getParent().args);
	fold_others (m); fold_jeldha (m);
	return emit (frame, 'book', 'book', m);
end

function p.journal (frame)
	local m = copy (frame:getParent().args);
	fold_others (m); fold_jeldha (m);
	return emit (frame, 'journal', 'journal', m);
end

function p.magazine (frame)
	local m = copy (frame:getParent().args);
	fold_others (m); fold_ref (m); fold_jeldha (m);
	return emit (frame, 'magazine', 'magazine', m);
end

function p.encyclopedia (frame)
	local m = copy (frame:getParent().args);
	fold_others (m); fold_ref (m); fold_jeldha (m);
	return emit (frame, 'encyclopaedia', 'encyclopedia', m);
end

function p.podcast (frame)
	local m = copy (frame:getParent().args);
	fold_others (m); fold_jeldha (m);
	return emit (frame, 'podcast', 'podcast', m);
end

function p.cite (frame)														-- CS2 الگو:یادکرد
	local m = copy (frame:getParent().args);
	fold_others (m); fold_jeldha (m); fold_url_status (m); fold_url_access (m);
	return emit (frame, 'citation', 'cite', m);
end

return p