پودمان:Cite tweet

از ویکی‌پدیا، دانشنامهٔ آزاد
توضیحات پودمان[ایجاد] [پاکسازی]
local TwitterSnowflake = require('Module:TwitterSnowflake')
local CiteWeb = require('Module:Cite web')['']
local CiteWebFa = require('Module:Cite web/fa')['']
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local numConv = require('Module:Numeral converter').convert

local p = {}

local function _if(arg)
	return arg and arg ~= '' or nil
end

p.main = function(frame)
	local args = getArgs(frame)
	return p._main(frame, args)
end

local function covertToFa(args)
	local convert = false
	local faArgs = {
		'ارجاع', 'پیوند', 'پیوند مرده', 'پیوند نویسنده', 'تاریخ',
		'تاریخ بازبینی', 'تاریخ بایگانی', 'ترجمه عنوان', 'عنوان خط', 'ریتوییت',
		'زبان', 'شماره', 'عنوان', 'کاربر', 'گفتاورد',
		'مکان', 'نام', 'نام خانوادگی', 'نشانی بایگانی', 'نویسنده'
	}
	
	-- Check if any of the Persian arguments are provided
	for k, v in pairs(args) do
		if faArgs[k] then
			convert = true
			break
		end
	end
	
	return convert
end

-- Local function to convert citation language to Persian if any of the Persian arguments are provided
local function localize(args, arg, val, default)

	local convert = covertToFa(args)
	local returnVal = default

	if convert then
		if arg == 'پیوند' then
			if val == 'no' then
				returnVal = 'توییتر'
			else
				returnVal = '[[توییتر]]'
			end
		elseif arg == 'ریتوییت' then
			returnVal = 'ریتوییت‌شده توسط ' .. val
		end
	end
	
	return returnVal

end

p._main = function(frame, args)
	-- Localized arguments
	if args['ارجاع'] then args.ref = args['ارجاع'] end
	if args['پیوند'] then args.link = args['پیوند'] end
	if args['پیوند مرده'] then args.deadurl = args['پیوند مرده'] end
	if args['پیوند نویسنده'] then args.authorlink = args['پیوند نویسنده'] end
	if args['تاریخ'] then args.date = args['تاریخ'] end
	if args['تاریخ بازبینی'] then args.accessdate = args['تاریخ بازبینی'] end
	if args['تاریخ بایگانی'] then args.archivedate = args['تاریخ بایگانی'] end
	if args['ترجمه عنوان'] then args['trans-title'] = args['ترجمه عنوان'] end
	if args['عنوان خط'] then args['script-title'] = args['عنوان خط'] end
	if args['ریتوییت'] then args.retweet = args['ریتوییت'] end
	if args['زبان'] then args.language = args['زبان'] end
	if args['شماره'] then args.number = args['شماره'] end
	if args['عنوان'] then args.title = args['عنوان'] end
	if args['کاربر'] then args.user = args['کاربر'] end
	if args['گفتاورد'] then args.quote = args['گفتاورد'] end
	if args['مکان'] then args.location = args['مکان'] end
	if args['نام'] or args['نام۱'] then args.first = args['نام'] or args['نام۱'] end
	if args['نام خانوادگی'] then args.last = args['نام خانوادگی'] end
	if args['نشانی بایگانی'] then args.archiveurl = args['نشانی بایگانی'] end
	if args['نویسنده'] then args.author = args['نویسنده'] end
	
	
	local cite_args = {
		url 		= 'https://twitter.com/' .. ((args.user and args.number) and (args.user .. '/status/' .. args.number) or ''),
		title		= mw.ustring.gsub((args.title or ''), 'https*://', ''),
		['script-title'] = args['script-title'],
		['trans-title'] = args['trans-title'],
		language	= args.language,
		['author-link'] = args['author-link'] or args.authorlink,
		others		= _if(args.retweet) and localize(args, 'ریتوییت', args.retweet, 'Retweeted by ' .. args.retweet),
		via 		= localize(args, 'پیوند', yesno(args.link), yesno(args.link) == 'no' and 'Twitter' or '[[Twitter]]'),
		type		= 'Tweet',
		location	= args.location,
		['access-date'] = args['access-date'] or args.accessdate,
		['archive-date'] = args['archive-date'] or args.archivedate,
		['archive-url'] = args['archive-url'] or args.archiveurl,
		['dead-url'] = args['url-status'] or args['dead-url'] or args.deadurl,
		quote	= args.quote,
		ref			= args.ref,
		df			= args.df,
	}
	if _if(args.last1 or args.last) then
		cite_args.author = (args.last1 or args.last) ..
			(_if(args.first1 or args.first) and ('، ' .. (args.first1 or args.first)) or '') ..
			' [@' .. (args.user or '') .. ']'
	elseif _if(args.author1 or args.author) then
		cite_args.author = (args.author1 or args.author) .. ' [@' .. (args.user or '') .. ']'
	elseif _if(args['author-link']) then
		cite_args.author = args['author-link'] .. ' [@' .. (args.user or '') .. ']'
	else
		cite_args.author = '@' .. (args.user or '')
	end
	if mw.ustring.find(cite_args.author, '[Tt]witter') or mw.ustring.find(cite_args.author, 'توییتر') then
		cite_args.author = '((' .. cite_args.author .. '))'
	end
	if _if(tonumber(args.number)) or _if(tonumber(numConv('en', args.number))) then
		cite_args.date = args.date or (_if(args.number) and TwitterSnowflake.snowflakeToDate{ args = {id_str = args.number} })
	else
		cite_args.date = args.date
	end
	
	frame.args = cite_args
	local output = covertToFa(args) and CiteWeb(frame) or CiteWebFa(frame)
	
	-- Error checking
	local error_template = '<span class="cs1-visible-error citation-comment">%s</span>'
	local errors = {}
	if not (_if(args.title) or _if(args['script-title']) or args.user or args.number or args.date) then
		-- No title; error message is provided by CS1 module.
		errors[1] = '؛'
	end
	if not _if(args.user) then
		errors[1 + #errors] = ' پارامتر <kbd>&#124;user=</kbd> یا <kbd>&#124;کاربر=</kbd> ناموجود یا خالی؛'
	end
	if not _if(args.number) then
		errors[1 + #errors] = ' پارامتر <kbd>&#124;number=</kbd> یا <kbd>&#124;شماره=</kbd> ناموجود یا خالی؛'
	end
	errors[1 + #errors] = TwitterSnowflake.datecheck{ args = {
		id_str	= args.number or '',
		date	= args.date or '',
		error1	= 'مقدار <kbd>&#124;date=</kbd> یا <kbd>&#124;تاریخ=</kbd> دو یا چند روز با تاریخ محاسبه‌شده از <kbd>&#124;number=</kbd> تفاوت دارد؛',
		error2  = ' پارامتر <kbd>&#124;date=</kbd> یا <kbd>&#124;تاریخ=</kbd> ناموجود یا خالی، و ارسال‌شده پیش از ۴ نوامبر ۲۰۱۰;',
		error3	= ' پارامتر <kbd>&#124;number=</kbd> یا <kbd>&#124;شماره=</kbd> نامعتبر؛'
	}}
	if errors[1] then
		local last = errors[#errors]
		errors[#errors] = mw.ustring.sub(last, 1, #last - 1) .. ' ([[الگو:Cite_tweet#شناسایی خطا|راهنما]])'
		local error_out = mw.ustring.format(mw.ustring.rep(error_template, #errors), unpack(errors)) 
		if mw.title.getCurrentTitle():inNamespace(0) then
			error_out = error_out .. '[[رده:الگو:یادکرد توئیت حاوی خطاها]]'
		end
		output = output .. error_out
	end
	return output
end

return p