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 16:15, 16 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 and args_t.user 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
	
	-- Error checking
	local errors_t = {'<span class="cs1-visible-error citation-comment"> <kbd>{{[[Template:Cite tweet|Cite tweet]]}}</kbd>:'};		-- initialize sequence of error messages with style tag
	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] = errors_t[1]:gsub ('> <', '>; <');							-- insert semicolon to terminate cs1|2 error message
	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_msgs = {												-- a sequence of snowflake error messages
		' <kbd>&#124;date=</kbd> mismatches calculated date from <kbd>&#124;number=</kbd> by two or more days;',
		' Missing or empty <kbd>&#124;date=</kbd>, and posted before November 4, 2010;',
		' Invalid <kbd>&#124;number=</kbd> parameter;'
		}
	
	local date_check_err_msg = TwitterSnowflake.datecheck ({ args = {			-- returns error number on error; nil else
		id_str	= args_t.number or '',
		date	= args_t.date or '',
		error1	= 1,															-- done this way to avoid long string comparison looking for
		error2  = 2,															-- the undated-pre-twitter-epoch-post message
		error3	= 3
		}});

	if	2 == date_check_err_msg then											-- when no date and posted before twitter epoch
		cite_args_t.date = nil;													-- suppress default date because {{cite tweet}} should not claim in its own voice that the undated post was posted 2010-11-04
	end

	table.insert (errors_t, date_check_err_msgs[date_check_err_msg]);

	frame.args = cite_args_t;
	local rendering = CiteWeb (frame);											-- render the template

	if errors_t[2] then															-- errors_t[2] nil when no errors
		errors_t[#errors_t] = errors_t[#errors_t]:gsub (';$',' ([[Template:Cite_tweet#Error_detection|help]])');	-- replace trailing semicolon with help link
		table.insert (errors_t, '</span>');										-- close style span tag
		if mw.title.getCurrentTitle():inNamespace (0) then						-- mainspace only
			table.insert (errors_t, '[[Category:Cite tweet templates with errors]]');	-- add error category
		end

		rendering = rendering .. table.concat (errors_t);						-- append error messaging, help links and catagories
	end
	return rendering;
end

return p