Jump to content

Module:Cite tweet/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Trappist the monk (talk | contribs) at 15:45, 15 December 2023. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}
local TwitterSnowflake = require('Module:TwitterSnowflake')
local CiteWeb = require('Module:Cite web')['']

p.main = function(frame)
	return p[''](frame)
end

p[''] = function(frame)
	local args_t = require ('Module:Arguments').getArgs (frame);
	local cite_args_t = {
		url = 'https://twitter.com/' .. ((args_t.user and args_t.number) and (args_t.user .. '/status/' .. args_t.number) or ''),
		title = (args_t.title or ''):gsub('https*://', ''),
		['script-title'] = args_t['script-title'],
		['trans-title'] = args_t['trans-title'],
		language = args_t.language,
		last1 = args_t.last1 or args_t.last,
		first1 = args_t.first1 or args_t.first,
		author1 = args_t.author1 or args_t.author,
		['author-link'] = args_t['author-link'] or args_t.authorlink,
		others = args_t.retweet and ('Retweeted by ' .. args_t.retweet),
		via = args_t.link == 'no' and 'Twitter' or '[[Twitter]]',
		type = 'Tweet',
		location = args_t.location,												-- why |location=?  tweets are online; there is no publication place
		['access-date'] = args_t['access-date'] or args_t.accessdate,
		['archive-date'] = args_t['archive-date'] or args_t.archivedate,
		['archive-url'] = args_t['archive-url'] or args_t.archiveurl,
		['url-status'] = args_t['url-status'],
		quote = args_t.quote,
		ref = args_t.ref,
		df = args_t.df,
		mode = args_t.mode
		}

	local author = ((cite_args_t.last1 and cite_args_t.first1) and cite_args_t.last1 .. ', ' .. cite_args_t.first1) or	-- concatenate |last= with |first= for |author-mask=
		(cite_args_t.last1 and cite_args_t.last1) or							-- only |last= for |author-mask=
		(cite_args_t.author1 and cite_args_t.author1:gsub('^%(%((.+)%)%)$', '%1'));	-- |author= or |author1= stripped of accept-as-written markup for |author-mask=

	if author then
		cite_args_t['author-mask'] = author .. ' [@' .. (args_t.user or '') .. ']'	-- concatenate <author> and |user= into |author-mask=
	elseif args_t.user then
		cite_args_t.author1 = '((' .. args_t.user .. '))';						-- just the user name for cs1|2 metadata
		cite_args_t['author-mask'] = '@' .. args_t.user;						-- make a mask for display
	else																		-- here when neither <author> nor |user=
		cite_args_t.author1 = nil;												-- so unset TODO: add error message here instead of reevaluating and adding later?
	end

	if tonumber(args_t.number) then
		cite_args_t.date = args_t.date or 
			(args_t.number and TwitterSnowflake.snowflakeToDate ({ args = {id_str = args_t.number} }))	-- sets cite_args_t.date to 'November 4, 2010' if tweet posted before that date; should it?
	else
		cite_args_t.date = args_t.date
	end
	
	frame.args = cite_args_t;
	local output = CiteWeb (frame);												-- render the template

	-- Error checking
	local error_msg_format_str = '<span class="cs1-visible-error citation-comment">%s</span>'
	local errors_t = {}
	if not (args_t.title or args_t['script-title'] or args_t.user or args_t.number or args_t.date) then
		-- No title; error message is provided by CS1 module.
		errors_t[1] = ';'
	end
	if not args_t.user then
		table.insert (errors_t, ' Missing or empty <kbd>&#124;user=</kbd>;');
	end
	if not args_t.number then
		table.insert (errors_t, ' Missing or empty <kbd>&#124;number=</kbd>;');
	end

	local date_check_err_msg = TwitterSnowflake.datecheck ({ args = {
		id_str	= args_t.number or '',
		date	= args_t.date or '',
		error1	= ' <kbd>&#124;date=</kbd> mismatches calculated date from <kbd>&#124;number=</kbd> by two or more days;',
		error2  = ' Missing or empty <kbd>&#124;date=</kbd>, and posted before November 4, 2010;',
		error3	= ' Invalid <kbd>&#124;number=</kbd> parameter;'
		}});
	
	table.insert (errors_t, date_check_err_msg);

	if errors_t[1] then
		local last = errors_t[#errors_t]
		errors_t[#errors_t] = last:sub(1, #last - 1) .. ' ([[Template:Cite_tweet#Error_detection|help]])';	-- remove trailing semicolon and append help link
		local error_out = error_msg_format_str:rep(#errors_t):format(unpack(errors_t))	-- why wrap each error message in individual <span> ... </span> tags? 
		if mw.title.getCurrentTitle():inNamespace(0) then
			error_out = error_out .. '[[Category:Cite tweet templates with errors]]'
		end
		output = output .. error_out
	end
	return output
end

return p