Lompat ke isi

Modul:Cite tweet

Dari Wikipedia bahasa Indonesia, ensiklopedia bebas

local TwitterSnowflake = require('Modul:TwitterSnowflake')

local err_msgs_t = {															-- deretan pesan galat tanggal snowflake; semuanya harus diakhiri dengan tanda titik koma (;)
	' nilai <kbd>&#124;date=</kbd> / <kbd>&#124;number=</kbd> tidak cocok;',	-- [1]
	' parameter <kbd>&#124;date=</kbd> diperlukan;',							-- [2]
	' nilai <kbd>&#124;number=</kbd> tidak valid;',								-- [3]
	' nilai <kbd>&#124;number=</kbd> tidak ada atau kosong;',					-- [4]
	' nilai <kbd>&#124;user=</kbd> tidak ada atau kosong;'						-- [5]
	}


--[[--------------------------< S U P P R E S S _ U R L _ I N _ T I T L E >------------------------------------

Fungsi berikut untuk mencari dan menekan URL pada parameter |title=, |script-title=, dan |trans-title= sehingga
{{cite web}} tidak menghasilkan pesan galat 'Pranala luar pada |title=' saat proses rendering, pranala dibentuk
dengan benar seperti pada tweet aslinya. Fungsi ini mencari skema yang valid dan kemudian membungkusnya dalam tag
<nowiki>..</nowiki>.

]]

local function suppress_url_in_title (frame, title)
	local schemes = {															-- skema yang umum ditemukan
		'https://',
		'http://',
		'ftp://',
		}

	if title then																-- when there is a title, suppress any urls with known schemes; abandon else
		for _, scheme in ipairs (schemes) do									-- spin through the list of schemes looking for a match
			title = title:gsub (scheme, frame:callParserFunction ('#tag', {'nowiki', scheme}));	-- replace the scheme with its nowiki'd form (a strip marker)
		end
	end

	return title;																-- done; return <title> modified or not
end


--[[--------------------------< D A T E _ N U M B E R _ U R L _ G E T >----------------------------------------

Ekstarasi nilai parameter |date= dan |number= jika tersedia. Ekstrasi tanggal dari parameter |number= kemudian dibandingkan nilainya dengan |date=.

pembentukan pranala dari |url= untuk templat {{cite web}} bersama dengan nilai dari |number= dan |user=

returns nothing; penambahan tanggal, nomor tweet, pranala ke <cite_args_t>; penambahan pesan galat ke <errors_t>.

]]

local function date_number_url_get (args_t, cite_args_t, errors_t)
	local err_msg_index;

	cite_args_t.url = 'https://x.com/';									        -- inisialisasi dengan pranala dasar karena templat {{cite web}} butuh |url=
	if not args_t.user then
		table.insert (errors_t, err_msgs_t[5]);									-- galat: |user= tidak ada atau kosong
	end

	if not args_t.date and not args_t.number then
		err_msg_index = 4;														-- galat: |number= tidak ada atau kosong
	elseif tonumber (args_t.number) then										-- |number= tanpa |date=? pakai nomor tweet
		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} });	-- parameter |date= lebih disukai; atau ekstarasi tanggal dari parameter |number=
		else
			cite_args_t.date = args_t.date;										-- |number= tampaknya tidak valid; gunakan |date= jika tersedia
		end

		cite_args_t.number = args_t.number;										-- gunakan |number= valid ataupun tidak 

		if args_t.user then														-- |number= tampaknya tidak valid; gunakan |user= jika tersedia
			local lang_obj = mw.getContentLanguage();
			local good, test_date = pcall (lang_obj.formatDate, lang_obj, 'Ymd', cite_args_t.date);
			if good then
				if 20240517 > tonumber (test_date) then							-- resmi berganti menjadi x.com pada 2024-05-17 (17 Mei 2024)
					cite_args_t.url = 'https://twitter.com/';
				end
				if 20230723 > tonumber (test_date) then							-- berganti nama menjadi x pada 2023-07-23 (23 Juli 2023)
					cite_args_t.via = args_t.link == 'no' and 'X' or '[[Twitter]]';
				else
					cite_args_t.via = args_t.link == 'no' and 'X (sebelumnya Twitter)' or '[[Twitter|X (sebelumnya Twitter)]]';
				end
					
			end
			cite_args_t.url = cite_args_t.url .. args_t.user .. '/status/' .. args_t.number;	-- pembuatan |url= untuk templat {{cite web}}
		end

	elseif args_t.number then													-- nilai |number= tidak dapat dikonversi menjadi nomor tweet; tidak valid
		err_msg_index = 3;														-- galat: nomor tidak valid (tidak dapat dikonversi menjadi nomor tweet)
	elseif not args_t.number then												-- |date= tanpa |number= use date
		cite_args_t.date = args_t.date;											-- nilai |date= ada, pakai ini
		err_msg_index = 4;														-- galat: |number= tidak ada tau kosong
	end

	if err_msg_index then
		table.insert (errors_t, err_msgs_t[err_msg_index]);						-- nomor tidak valid atau parameter yang diperlukan tidak ada, jadi batalkan
		return;
	end

	err_msg_index = TwitterSnowflake.datecheck ({ args = {						-- mengembalikan nomor indeks pesan galat jika terjadi galat; nil jika tidak
		id_str	= args_t.number or '',
		date	= args_t.date or '',
		error1	= 1,															-- angka-angka ini merupakan indeks ke dalam <err_msgs_t> untuk mengganti pesan galat bawaan snowflake
		error2  = 2,															-- Hal ini dilakukan untuk menghindari perbandingan string yang panjang dalam mencari 
		error3	= 3																-- pesan yang diposting sebelum era Twitter
		}});

	if	2 == err_msg_index then													-- jika tidak ada tanggal dan diunggah sebelum era Twitter,
		cite_args_t.date = nil;													-- sembunyikan tanggal baku karena {{cite tweet}} tidak boleh menyatakan seolah-olah postingan tanpa tanggal tersebut diunggah pada 2010-11-04
	end
	
	table.insert (errors_t, err_msgs_t[err_msg_index]);							-- penambahan pesat galat
end


--[[--------------------------<F U N G S I  U T A M A >----------------------------------------------------------------------

pembentukan parameter untuk templat {{cite web}} dari parameter templat {{cite tweet}};  serta pemeriksaaan galat

]]

local function main (frame)
	local args_t = require ('Modul:Arguments').getArgs (frame);

	local cite_args_t = {
		title = suppress_url_in_title (frame, args_t.title),
		['script-title'] = suppress_url_in_title (frame, args_t['script-title']),
		['trans-title'] = suppress_url_in_title (frame, 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 ('Di-retweet oleh ' .. args_t.retweet),
		type = args_t.link == 'no' and 'Kicauan' or '[[Twitter#Kicauan|Kicauan]]',
		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'],
		['url-access'] = args_t['url-access'],
		quote = args_t.quote,
		ref = args_t.ref,
		df = 'dmy-all',                   										-- ubah semua tampilan format tanggal menjadi "dmy"
		mode = args_t.mode
		}

	local errors_t = {'<span class="cs1-visible-error citation-comment"> Templat <kbd>{{[[Templat:Cite tweet|Cite tweet]]}}</kbd>:'};		-- inisialisasi urutan pesan galat dengan tag style
	date_number_url_get (args_t, cite_args_t, errors_t);						-- add |date=, |number=, |url= ke <cite_args_t>

	local author = ((cite_args_t.last1 and cite_args_t.first1) and cite_args_t.last1 .. ', ' .. cite_args_t.first1) or	-- penggabungan |last= dan |first= untuk |author-mask=
		(cite_args_t.last1 and cite_args_t.last1) or							-- atau hanya |last= untuk |author-mask=
		(cite_args_t.author1 and cite_args_t.author1:gsub('^%(%((.+)%)%)$', '%1'));	-- |author= atau |author1= dicabut haknya untuk menandai "diterima sebagaimana adanya" pada |author-mask=

	if author and args_t.user then
		cite_args_t['author-mask'] = author .. ' [@' .. (args_t.user or '') .. ']'	-- penggabungan <author> dan |user= ke |author-mask=
	elseif args_t.user then
		cite_args_t.author1 = '((' .. args_t.user .. '))';						-- gunakan user name untuk metada cs1|2
		cite_args_t['author-mask'] = '@' .. args_t.user;						-- membuat tabir untuk ditampilkan
	else																		-- jika <author> atau |user= tidak ada
		cite_args_t.author1 = nil;												-- unset
	end

	local rendering = require ('Modul:Citation/CS1')._citation (nil, cite_args_t, {CitationClass = 'web'});

---------- pesan galat ----------
	if errors_t[2] then															-- nilai errors_t[2] nol saat tidak ada galat
		if rendering:find ('cs1-visible-error', 1, true) then					-- pesan galat templat {{cite web}} dengan string ini
			errors_t[1] = errors_t[1]:gsub ('> <', '>; <');						-- penambahan titik-koma untuk terminasi pesan galat cs1|2
		end

		errors_t[#errors_t] = errors_t[#errors_t]:gsub (';$',' ([[Templat:Cite_tweet#Error_detection|help]])');	-- ganti titik-koma menjadi tautan bantuan
		table.insert (errors_t, '</span>');										-- penutup tag style span
		if mw.title.getCurrentTitle():inNamespace (0) then						-- hanya ruang nama utama
			table.insert (errors_t, '[[Kategori:Templat cite tweet dengan galat]]');	-- kategori pesan galat
		end

		rendering = rendering .. table.concat (errors_t);						-- tambahkan pesan galat, tautan bantuan, dan kategori
	end
	return rendering;
end

--[[--------------------------< E X P O R T S >----------------------------------------------------------------
]]

return {
	main = main,																-- titik awal; {{#invoke:cite tweet|main|...}} lebih disukai
	[''] = main,																-- {{#invoke:cite tweet||...}} juga boleh
	}