Module:Cite tweet: Difference between revisions
Jump to navigation
Jump to search
(Created page with "local TwitterSnowflake = require('Module:TwitterSnowflake') local err_msgs_t = { -- a sequence of snowflake date error messages; all must be terminated with a semicolon (;) ' <kbd>|date=</kbd> / <kbd>|number=</kbd> mismatch;', -- [1] ' <kbd>|date=</kbd> required;', -- [2] ' Invalid <kbd>|number=</kbd>;', -- [3] ' Missing or empty <kbd>|number=</kbd>;', -- [4] ' Missing or empty <kbd>|user=</kbd...") |
No edit summary |
||
| Line 1: | Line 1: | ||
local TwitterSnowflake = require('Module:TwitterSnowflake') | local TwitterSnowflake = require('Module:TwitterSnowflake') | ||
local err_msgs_t = { | local err_msgs_t = { | ||
' <kbd>|date=</kbd> / <kbd>|number=</kbd> mismatch;', | ' <kbd>|date=</kbd> / <kbd>|number=</kbd> mismatch;', | ||
' <kbd>|date=</kbd> required;', | ' <kbd>|date=</kbd> required;', | ||
' Invalid <kbd>|number=</kbd>;', | ' Invalid <kbd>|number=</kbd>;', | ||
' Missing or empty <kbd>|number=</kbd>;', | ' Missing or empty <kbd>|number=</kbd>;', | ||
' Missing or empty <kbd>|user=</kbd>;' | ' Missing or empty <kbd>|user=</kbd>;' | ||
} | |||
local function suppress_url_in_title (frame, title) | local function suppress_url_in_title(frame, title) | ||
local schemes = { | local schemes = { | ||
'https://', | 'https://', | ||
'http://', | 'http://', | ||
'ftp://', | 'ftp://', | ||
} | |||
if title then | if title then | ||
for _, scheme in ipairs (schemes) do | for _, scheme in ipairs(schemes) do | ||
title = title:gsub (scheme, frame:callParserFunction ('#tag', {'nowiki', scheme}) | title = title:gsub( | ||
scheme, | |||
frame:callParserFunction('#tag', {'nowiki', scheme}) | |||
) | |||
end | end | ||
end | end | ||
return title | return title | ||
end | end | ||
local function date_number_url_get(args_t, cite_args_t, errors_t) | |||
local err_msg_index | |||
cite_args_t.url = 'https://x.com/' | |||
if not args_t.user then | |||
table.insert(errors_t, err_msgs_t[5]) | |||
end | |||
if not args_t.date and not args_t.number then | |||
err_msg_index = 4 | |||
elseif args_t.number then | |||
if tonumber(args_t.number) then | |||
cite_args_t.date = | |||
args_t.date or | |||
TwitterSnowflake.snowflakeToDate{ | |||
args = { id_str = args_t.number } | |||
} | |||
cite_args_t.number = args_t.number | |||
if args_t.user then | |||
cite_args_t.url = | |||
cite_args_t.url .. args_t.user .. '/status/' .. args_t.number | |||
end | |||
else | else | ||
err_msg_index = 3 | |||
end | end | ||
elseif args_t.date then | |||
cite_args_t.date = args_t.date | |||
err_msg_index = 4 | |||
elseif args_t. | |||
cite_args_t.date = args_t.date | |||
err_msg_index = 4 | |||
end | end | ||
if err_msg_index then | if err_msg_index then | ||
table.insert (errors_t, err_msgs_t[err_msg_index]) | table.insert(errors_t, err_msgs_t[err_msg_index]) | ||
return | return | ||
end | end | ||
if | err_msg_index = TwitterSnowflake.datecheck({ | ||
cite_args_t.date = nil | args = { | ||
id_str = args_t.number or '', | |||
date = args_t.date or '', | |||
error1 = 1, | |||
error2 = 2, | |||
error3 = 3 | |||
} | |||
}) | |||
if err_msg_index == 2 then | |||
cite_args_t.date = nil | |||
end | |||
if err_msg_index then | |||
table.insert(errors_t, err_msgs_t[err_msg_index]) | |||
end | end | ||
end | end | ||
local function main(frame) | |||
local args_t = require('Module:Arguments').getArgs(frame) | |||
local args_t = require ('Module:Arguments').getArgs (frame) | |||
local cite_args_t = { | local cite_args_t = { | ||
title = suppress_url_in_title (frame, args_t.title), | title = suppress_url_in_title(frame, args_t.title), | ||
['script-title'] = suppress_url_in_title (frame, args_t['script-title']), | ['script-title'] = suppress_url_in_title(frame, args_t['script-title']), | ||
['trans-title'] = suppress_url_in_title (frame, args_t['trans-title']), | ['trans-title'] = suppress_url_in_title(frame, args_t['trans-title']), | ||
language = args_t.language, | language = args_t.language, | ||
last1 = args_t.last1 or args_t.last, | last1 = args_t.last1 or args_t.last, | ||
| Line 116: | Line 112: | ||
via = args_t.link == 'no' and 'Twitter' or '[[Twitter]]', | via = args_t.link == 'no' and 'Twitter' or '[[Twitter]]', | ||
type = args_t.link == 'no' and 'Tweet' or '[[Tweet (social media)|Tweet]]', | type = args_t.link == 'no' and 'Tweet' or '[[Tweet (social media)|Tweet]]', | ||
location = args_t.location, | location = args_t.location, | ||
['access-date'] = args_t['access-date'] or args_t.accessdate, | ['access-date'] = args_t['access-date'] or args_t.accessdate, | ||
['archive-date'] = args_t['archive-date'] or args_t.archivedate, | ['archive-date'] = args_t['archive-date'] or args_t.archivedate, | ||
| Line 126: | Line 122: | ||
df = args_t.df, | df = args_t.df, | ||
mode = args_t.mode | mode = args_t.mode | ||
} | } | ||
local errors_t = { | |||
'<span class="cs1-visible-error citation-comment"> <kbd>{{[[Template:Cite tweet|Cite tweet]]}}</kbd>:' | |||
} | |||
date_number_url_get(args_t, cite_args_t, errors_t) | |||
local | local author = | ||
((cite_args_t.last1 and cite_args_t.first1) and cite_args_t.last1 .. ', ' .. cite_args_t.first1) | |||
or (cite_args_t.last1 and cite_args_t.last1) | |||
or (cite_args_t.author1 and cite_args_t.author1:gsub('^%(%((.+)%)%)$', '%1')) | |||
if author and args_t.user then | if author and args_t.user then | ||
cite_args_t['author-mask'] = author .. ' [@' .. | cite_args_t['author-mask'] = author .. ' [@' .. args_t.user .. ']' | ||
elseif args_t.user then | elseif args_t.user then | ||
cite_args_t.author1 = '((' .. args_t.user .. '))' | cite_args_t.author1 = '((' .. args_t.user .. '))' | ||
cite_args_t['author-mask'] = '@' .. args_t.user | cite_args_t['author-mask'] = '@' .. args_t.user | ||
else | |||
cite_args_t.author1 = nil | else | ||
cite_args_t.author1 = nil | |||
end | end | ||
local rendering = require('Module:Citation/CS1')._citation(nil, cite_args_t, {CitationClass = 'web'}) | |||
if errors_t[2] then | |||
if rendering:find ('cs1-visible-error', 1, true) then | |||
errors_t[1] = errors_t[1]:gsub ('> <', '>; <') | if errors_t[2] then | ||
if rendering:find('cs1-visible-error', 1, true) then | |||
errors_t[1] = errors_t[1]:gsub('> <', '>; <') | |||
end | end | ||
errors_t[#errors_t] = errors_t[#errors_t]:gsub (';$',' ([[Template:Cite_tweet#Error_detection|help]])') | errors_t[#errors_t] = | ||
table.insert (errors_t, '</span>') | errors_t[#errors_t]:gsub( | ||
if mw.title.getCurrentTitle():inNamespace (0) then | ';$', | ||
table.insert (errors_t, '[[Category:Cite tweet templates with errors]]') | ' ([[Template:Cite_tweet#Error_detection|help]])' | ||
) | |||
table.insert(errors_t, '</span>') | |||
if mw.title.getCurrentTitle():inNamespace(0) then | |||
table.insert(errors_t, '[[Category:Cite tweet templates with errors]]') | |||
end | end | ||
rendering = rendering .. table.concat (errors_t) | |||
rendering = rendering .. table.concat(errors_t) | |||
end | end | ||
return rendering | |||
return rendering | |||
end | end | ||
return { | return { | ||
main = main, | main = main, | ||
[''] = main | [''] = main | ||
} | |||
Latest revision as of 04:10, 6 March 2026
Documentation for this module may be created at Module:Cite tweet/doc
local TwitterSnowflake = require('Module:TwitterSnowflake')
local err_msgs_t = {
' <kbd>|date=</kbd> / <kbd>|number=</kbd> mismatch;',
' <kbd>|date=</kbd> required;',
' Invalid <kbd>|number=</kbd>;',
' Missing or empty <kbd>|number=</kbd>;',
' Missing or empty <kbd>|user=</kbd>;'
}
local function suppress_url_in_title(frame, title)
local schemes = {
'https://',
'http://',
'ftp://',
}
if title then
for _, scheme in ipairs(schemes) do
title = title:gsub(
scheme,
frame:callParserFunction('#tag', {'nowiki', scheme})
)
end
end
return title
end
local function date_number_url_get(args_t, cite_args_t, errors_t)
local err_msg_index
cite_args_t.url = 'https://x.com/'
if not args_t.user then
table.insert(errors_t, err_msgs_t[5])
end
if not args_t.date and not args_t.number then
err_msg_index = 4
elseif args_t.number then
if tonumber(args_t.number) then
cite_args_t.date =
args_t.date or
TwitterSnowflake.snowflakeToDate{
args = { id_str = args_t.number }
}
cite_args_t.number = args_t.number
if args_t.user then
cite_args_t.url =
cite_args_t.url .. args_t.user .. '/status/' .. args_t.number
end
else
err_msg_index = 3
end
elseif args_t.date then
cite_args_t.date = args_t.date
err_msg_index = 4
end
if err_msg_index then
table.insert(errors_t, err_msgs_t[err_msg_index])
return
end
err_msg_index = TwitterSnowflake.datecheck({
args = {
id_str = args_t.number or '',
date = args_t.date or '',
error1 = 1,
error2 = 2,
error3 = 3
}
})
if err_msg_index == 2 then
cite_args_t.date = nil
end
if err_msg_index then
table.insert(errors_t, err_msgs_t[err_msg_index])
end
end
local function main(frame)
local args_t = require('Module: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 ('Retweeted by ' .. args_t.retweet),
via = args_t.link == 'no' and 'Twitter' or '[[Twitter]]',
type = args_t.link == 'no' and 'Tweet' or '[[Tweet (social media)|Tweet]]',
location = args_t.location,
['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 = args_t.df,
mode = args_t.mode
}
local errors_t = {
'<span class="cs1-visible-error citation-comment"> <kbd>{{[[Template:Cite tweet|Cite tweet]]}}</kbd>:'
}
date_number_url_get(args_t, cite_args_t, errors_t)
local author =
((cite_args_t.last1 and cite_args_t.first1) and cite_args_t.last1 .. ', ' .. cite_args_t.first1)
or (cite_args_t.last1 and cite_args_t.last1)
or (cite_args_t.author1 and cite_args_t.author1:gsub('^%(%((.+)%)%)$', '%1'))
if author and args_t.user then
cite_args_t['author-mask'] = author .. ' [@' .. args_t.user .. ']'
elseif args_t.user then
cite_args_t.author1 = '((' .. args_t.user .. '))'
cite_args_t['author-mask'] = '@' .. args_t.user
else
cite_args_t.author1 = nil
end
local rendering = require('Module:Citation/CS1')._citation(nil, cite_args_t, {CitationClass = 'web'})
if errors_t[2] then
if rendering:find('cs1-visible-error', 1, true) then
errors_t[1] = errors_t[1]:gsub('> <', '>; <')
end
errors_t[#errors_t] =
errors_t[#errors_t]:gsub(
';$',
' ([[Template:Cite_tweet#Error_detection|help]])'
)
table.insert(errors_t, '</span>')
if mw.title.getCurrentTitle():inNamespace(0) then
table.insert(errors_t, '[[Category:Cite tweet templates with errors]]')
end
rendering = rendering .. table.concat(errors_t)
end
return rendering
end
return {
main = main,
[''] = main
}