Module:Citation/CS1/Utilities: Difference between revisions
Created page with "local z = { error_cats_t = {}; -- for categorizing citations that contain errors error_ids_t = {}; -- list of error identifiers; used to prevent duplication of certain errors; local to this module error_msgs_t = {}; -- sequence table of error messages maint_cats_t = {}; -- for categorizing citations that aren't erroneous per se, but could use a little work prop_cats_t = {}; -- for categorizing cit..." |
No edit summary |
||
| Line 1: | Line 1: | ||
local z = { | local z = { | ||
error_cats_t = {}; | error_cats_t = {}; | ||
error_ids_t = {}; | error_ids_t = {}; | ||
error_msgs_t = {}; | error_msgs_t = {}; | ||
maint_cats_t = {}; | maint_cats_t = {}; | ||
prop_cats_t = {}; | prop_cats_t = {}; | ||
prop_keys_t = {}; | prop_keys_t = {}; | ||
}; | }; | ||
local cfg; -- Module:Citation/CS1/Configuration'dan yüklenecek | |||
--[[--------------------------< I S _ S E T >----------------------------------]] | |||
--[[--------------------------< I S _ S E T >---------------------------------- | |||
]] | |||
local function is_set (var) | local function is_set (var) | ||
return not (var == nil or var == ''); | return not (var == nil or var == ''); | ||
end | end | ||
--[[--------------------------< I N _ A R R A Y >------------------------------]] | |||
--[[--------------------------< I N _ A R R A Y >-------------------------------- | -- Hata aldığı yer burasıydı: haystack nil gelirse ipairs çöküyordu. | ||
local function in_array (needle, haystack) | local function in_array (needle, haystack) | ||
if needle == nil then | if needle == nil or haystack == nil then | ||
return false; | return false; | ||
end | end | ||
| Line 44: | Line 29: | ||
end | end | ||
--[[--------------------------< H A S _ A C C E P T _ A S _ W R I T T E N >----]] | |||
--[[--------------------------< H A S _ A C C E P T _ A S _ W R I T T E N >---- | |||
]] | |||
local function has_accept_as_written (str, allow_empty) | local function has_accept_as_written (str, allow_empty) | ||
if not is_set (str) then | if not is_set (str) then | ||
return str, false; | return str, false; | ||
end | end | ||
local count; | local count; | ||
if true == allow_empty then | if true == allow_empty then | ||
str, count = str:gsub ('^%(%((.*)%)%)$', '%1'); | str, count = str:gsub ('^%(%((.*)%)%)$', '%1'); | ||
else | else | ||
str, count = str:gsub ('^%(%((.+)%)%)$', '%1'); | str, count = str:gsub ('^%(%((.+)%)%)$', '%1'); | ||
| Line 71: | Line 43: | ||
end | end | ||
--[[--------------------------< S U B S T I T U T E >--------------------------]] | |||
--[[--------------------------< S U B S T I T U T E >-------------------------- | |||
]] | |||
local function substitute (msg, args) | local function substitute (msg, args) | ||
return args and mw.message.newRawMessage (msg, args):plain() or msg; | return args and mw.message.newRawMessage (msg, args):plain() or msg; | ||
end | end | ||
--[[--------------------------< E R R O R _ C O M M E N T >--------------------]] | |||
--[[--------------------------< E R R O R _ C O M M E N T >-------------------- | |||
]] | |||
local function error_comment (content, hidden) | local function error_comment (content, hidden) | ||
if not cfg or not cfg.presentation then return content; end | |||
return substitute (hidden and cfg.presentation['hidden-error'] or cfg.presentation['visible-error'], content); | return substitute (hidden and cfg.presentation['hidden-error'] or cfg.presentation['visible-error'], content); | ||
end | end | ||
--[[--------------------------< H Y P H E N _ T O _ D A S H >------------------]] | |||
--[[--------------------------< H Y P H E N _ T O _ D A S H >------------------ | |||
]] | |||
local function hyphen_to_dash (str) | local function hyphen_to_dash (str) | ||
if not is_set (str) then | if not is_set (str) then return str; end | ||
str = str:gsub ("(%(%(.-%)%))", function(m) return m:gsub(",", ","):gsub(";", ";") end) | |||
str = str:gsub ('&[nm]dash;', {['–'] = '–', ['—'] = '—'}); | |||
str = str:gsub ('-', '-'); | |||
str = str:gsub ("(%(%(.-%)%))", function(m) return m:gsub(",", ","):gsub(";", ";") end) | str = str:gsub (' ', ' '); | ||
str = str:gsub ('&[nm]dash;', {['–'] = '–', ['—'] = '—'}); | |||
str = str:gsub ('-', '-'); | |||
str = str:gsub (' ', ' '); | |||
local out = {}; | local out = {}; | ||
local list = mw.text.split (str, '%s*[,;]%s*'); | local list = mw.text.split (str, '%s*[,;]%s*'); | ||
local accept; | |||
local accept; | |||
for _, item in ipairs (list) do | |||
item, accept = has_accept_as_written (item); | |||
mw.ustring.match (item, '^%d+%s*[—–-]%s*%d+$') or | if not accept and mw.ustring.match (item, '^%w*[%.%-]?%w+%s*[—–-]%s*%w*[%.%-]?%w+$') then | ||
mw.ustring.match (item, '^% | if mw.ustring.match (item, '^%a+[%.%-]%d+%s*[—–-]%s*%a+[%.%-]%d+$') or | ||
item = mw.ustring.gsub (item, '(%w+)%s*[—–-]%s*(%w+)', '<span class="nowrap">% | mw.ustring.match (item, '^%d+[%.%-]%a+%s*[—–-]%s*%d+[%.%-]%a+$') or | ||
mw.ustring.match (item, '^%d+[%.%-]%d+%s*[—–-]%s*%d+[%.%-]%d+$') then | |||
item = mw.ustring.gsub (item, '(%w*[%.%-]%w+)%s*[—–-]%s*(%w*[%.%-]%w+)', '<span class="nowrap">%1 –</span> <span class="nowrap">%2</span>'); | |||
elseif mw.ustring.match (item, '^%d+%a+%s*[—–-]%s*%d+%a+$') or | |||
mw.ustring.match (item, '^%a+%d+%s*[—–-]%s*%a+%d+$') or | |||
mw.ustring.match (item, '^%d+%s*[—–-]%s*%d+$') or | |||
mw.ustring.match (item, '^%a+%s*[—–-]%s*%a+$') then | |||
item = mw.ustring.gsub (item, '(%w+)%s*[—–-]%s*(%w+)', '<span class="nowrap">%1–</span>%2'); | |||
end | end | ||
end | end | ||
table.insert (out, item); | table.insert (out, item); | ||
end | end | ||
local temp_str = | local temp_str = table.concat (out, ', '); | ||
return temp_str:gsub(",", ","):gsub(";", ";"); | |||
end | end | ||
--[[-------------------------< M A K E _ W I K I L I N K >---------------------]] | |||
--[ | |||
] | |||
local function make_wikilink (link, display) | local function make_wikilink (link, display) | ||
if not is_set (link) then return '' end | if not is_set (link) then return '' end | ||
if is_set (display) and link ~= display then | if is_set (display) and link ~= display then | ||
return table.concat ({'[[', link, '|', display, ']]'}); | return table.concat ({'[[', link, '|', display, ']]'}); | ||
| Line 179: | Line 97: | ||
end | end | ||
--[[--------------------------< S E T _ M E S S A G E >------------------------]] | |||
--[[--------------------------< S E T _ M E S S A G E >------------------------ | local added_maint_cats = {} | ||
]] | |||
local added_maint_cats = {} | |||
local function set_message (error_id, arguments, raw, prefix, suffix) | local function set_message (error_id, arguments, raw, prefix, suffix) | ||
if not cfg or not cfg.error_conditions then return ''; end | |||
local error_state = cfg.error_conditions[error_id]; | local error_state = cfg.error_conditions[error_id]; | ||
| Line 209: | Line 107: | ||
if error_state == nil then | if error_state == nil then | ||
error (cfg.messages['undefined_error'] .. ': ' .. error_id); | error (cfg.messages['undefined_error'] .. ': ' .. error_id); | ||
elseif is_set (error_state.category) then | elseif is_set (error_state.category) then | ||
if error_state.message then | if error_state.message then | ||
table.insert (z.error_cats_t, error_state.category); | table.insert (z.error_cats_t, error_state.category); | ||
else | else | ||
if not added_maint_cats[error_id] then | if not added_maint_cats[error_id] then | ||
added_maint_cats[error_id] = true; | added_maint_cats[error_id] = true; | ||
table.insert (z.maint_cats_t, substitute (error_state.category, arguments)); | table.insert (z.maint_cats_t, substitute (error_state.category, arguments)); | ||
end | end | ||
return; | return; | ||
end | end | ||
end | end | ||
local message = substitute (error_state.message, arguments); | local message = substitute (error_state.message, arguments); | ||
message = table.concat ({message, ' (', make_wikilink (table.concat ({cfg.messages['help page link'], '#', error_state.anchor}), cfg.messages['help page label']), ')'}); | |||
message = table.concat ( | |||
z.error_ids_t[error_id] = true; | z.error_ids_t[error_id] = true; | ||
if z.error_ids_t['err_citation_missing_title'] and | if z.error_ids_t['err_citation_missing_title'] and in_array (error_id, {'err_bare_url_missing_title', 'err_trans_missing_title'}) then | ||
return '', false; | |||
return '', false; | |||
end | end | ||
message = table.concat ({prefix, message, suffix}); | message = table.concat ({prefix, message, suffix}); | ||
if true == raw then return message, error_state.hidden; end | |||
message = error_comment (message, error_state.hidden); | |||
table.insert (z.error_msgs_t, message); | |||
return message; | |||
message = error_comment (message, error_state.hidden); | |||
table.insert (z.error_msgs_t, message); | |||
return message; | |||
end | end | ||
--[[-------------------------< I S _ A L I A S _ U S E D >---------------------]] | |||
--[[-------------------------< I S _ A L I A S _ U S E D >--------------------- | |||
]] | |||
local function is_alias_used (args, alias, index, enumerated, value, selected, error_list) | local function is_alias_used (args, alias, index, enumerated, value, selected, error_list) | ||
if enumerated then | if enumerated then | ||
alias = alias:gsub ('#', index); | alias = alias:gsub ('#', index); | ||
else | else | ||
alias = alias:gsub ('#', ''); | alias = alias:gsub ('#', ''); | ||
end | end | ||
if is_set (args[alias]) then | if is_set (args[alias]) then | ||
if value ~= nil and selected ~= alias then | if value ~= nil and selected ~= alias then | ||
local skip; | local skip; | ||
for _, v in ipairs (error_list) do | -- Buradaki ipairs(error_list) kısmına koruma eklendi | ||
if v == alias then | for _, v in ipairs (error_list or {}) do | ||
if v == alias then skip = true; break; end | |||
end | end | ||
if not skip then table.insert (error_list, alias); end | |||
else | else | ||
value = args[alias]; | value = args[alias]; | ||
selected = alias; | selected = alias; | ||
end | end | ||
end | end | ||
return value, selected; | return value, selected; | ||
end | end | ||
--[[--------------------------< A D D _ M A I N T _ C A T >--------------------]] | |||
--[[--------------------------< A D D _ M A I N T _ C A T >-------------------- | |||
]] | |||
local function add_maint_cat (key, arguments) | local function add_maint_cat (key, arguments) | ||
if not cfg or not cfg.maint_cats then return; end | |||
if not added_maint_cats [key] then | if not added_maint_cats [key] then | ||
added_maint_cats [key] = true; | added_maint_cats [key] = true; | ||
table.insert (z.maint_cats_t, substitute (cfg.maint_cats [key], arguments)); | table.insert (z.maint_cats_t, substitute (cfg.maint_cats [key], arguments)); | ||
end | end | ||
end | end | ||
--[[--------------------------< A D D _ P R O P _ C A T >----------------------]] | |||
--[[--------------------------< A D D _ P R O P _ C A T >---------------------- | local added_prop_cats = {}; | ||
]] | |||
local added_prop_cats = {}; | |||
local function add_prop_cat (key, arguments, key_modifier) | local function add_prop_cat (key, arguments, key_modifier) | ||
local key_modified = key .. ((key_modifier and key_modifier) or ''); | if not cfg or not cfg.prop_cats then return; end | ||
local key_modified = key .. ((key_modifier and key_modifier) or ''); | |||
if not added_prop_cats [key_modified] then | if not added_prop_cats [key_modified] then | ||
added_prop_cats [key_modified] = true; | added_prop_cats [key_modified] = true; | ||
table.insert (z.prop_cats_t, substitute (cfg.prop_cats [key], arguments)); | table.insert (z.prop_cats_t, substitute (cfg.prop_cats [key], arguments)); | ||
table.insert (z.prop_keys_t, 'cs1-prop-' .. key); | table.insert (z.prop_keys_t, 'cs1-prop-' .. key); | ||
end | end | ||
end | end | ||
--[[--------------------------< S A F E _ F O R _ I T A L I C S >--------------]] | |||
--[[--------------------------< S A F E _ F O R _ I T A L I C S >-------------- | |||
]] | |||
local function safe_for_italics (str) | local function safe_for_italics (str) | ||
if not is_set (str) then return str end | if not is_set (str) then return str end | ||
if str:sub (1, 1) == "'" then str = "<span></span>" .. str; end | if str:sub (1, 1) == "'" then str = "<span></span>" .. str; end | ||
if str:sub (-1, -1) == "'" then str = str .. "<span></span>"; end | if str:sub (-1, -1) == "'" then str = str .. "<span></span>"; end | ||
return str:gsub ('\n', ' '); | |||
return str:gsub ('\n', ' '); | |||
end | end | ||
--[[--------------------------< W R A P _ S T Y L E >--------------------------]] | |||
--[[--------------------------< W R A P _ S T Y L E >-------------------------- | |||
]] | |||
local function wrap_style (key, str) | local function wrap_style (key, str) | ||
if not is_set (str) then | if not is_set (str) then return ""; end | ||
if in_array (key, {'italic-title', 'trans-italic-title'}) then | |||
str = safe_for_italics (str); | str = safe_for_italics (str); | ||
end | end | ||
return substitute (cfg.presentation[key], {str}); | return substitute (cfg.presentation[key], {str}); | ||
end | end | ||
--[[--------------------------< M A K E _ S E P _ L I S T >--------------------]] | |||
--[[--------------------------< M A K E _ S E P _ L I S T >-------------------- | |||
]] | |||
local function make_sep_list (count, list_seq, sep_list, sep_list_pair, sep_list_end) | local function make_sep_list (count, list_seq, sep_list, sep_list_pair, sep_list_end) | ||
local list = ''; | local list = ''; | ||
if not sep_list and cfg then | |||
if not sep_list then | |||
sep_list = cfg.presentation['sep_list']; | sep_list = cfg.presentation['sep_list']; | ||
sep_list_pair = cfg.presentation['sep_list_pair']; | sep_list_pair = cfg.presentation['sep_list_pair']; | ||
sep_list_end = cfg.presentation['sep_list_end']; | sep_list_end = cfg.presentation['sep_list_end']; | ||
end | end | ||
if 2 >= count then | if 2 >= count then | ||
list = table.concat (list_seq, sep_list_pair); | list = table.concat (list_seq, sep_list_pair or ' and '); | ||
elseif 2 < count then | elseif 2 < count then | ||
list = table.concat (list_seq, sep_list, 1, count - 1); | list = table.concat (list_seq, sep_list or ', ', 1, count - 1); | ||
list = table.concat ({list, list_seq[count]}, sep_list_end); | list = table.concat ({list, list_seq[count]}, sep_list_end or ', and '); | ||
end | end | ||
return list; | return list; | ||
end | end | ||
--[[--------------------------< S E L E C T _ O N E >--------------------------]] | |||
--[[--------------------------< S E L E C T _ O N E >-------------------------- | |||
]] | |||
local function select_one (args, aliases_list, error_condition, index) | local function select_one (args, aliases_list, error_condition, index) | ||
local value = nil; | local value = nil; | ||
local selected = ''; | local selected = ''; | ||
local error_list = {}; | local error_list = {}; | ||
if index ~= nil then index = tostring(index); end | if index ~= nil then index = tostring(index); end | ||
-- aliases_list koruması | |||
for _, alias in ipairs (aliases_list) do | for _, alias in ipairs (aliases_list or {}) do | ||
if alias:match ('#') then | if alias:match ('#') then | ||
if '1' == index then | if '1' == index then | ||
value, selected = is_alias_used (args, alias, index, false, value, selected, error_list); | value, selected = is_alias_used (args, alias, index, false, value, selected, error_list); | ||
end | end | ||
value, selected = is_alias_used (args, alias, index, true, value, selected, error_list); | value, selected = is_alias_used (args, alias, index, true, value, selected, error_list); | ||
else | else | ||
value, selected = is_alias_used (args, alias, index, false, value, selected, error_list); | value, selected = is_alias_used (args, alias, index, false, value, selected, error_list); | ||
end | end | ||
end | end | ||
if #error_list > 0 and 'none' ~= error_condition then | |||
if #error_list > 0 and 'none' ~= error_condition then | for i, v in ipairs (error_list) do error_list[i] = wrap_style ('parameter', v); end | ||
for i, v in ipairs (error_list) do | |||
table.insert (error_list, wrap_style ('parameter', selected)); | table.insert (error_list, wrap_style ('parameter', selected)); | ||
set_message (error_condition, {make_sep_list (#error_list, error_list)}); | set_message (error_condition, {make_sep_list (#error_list, error_list)}); | ||
end | end | ||
return value, selected; | return value, selected; | ||
end | end | ||
--[[-------------------------< R E M O V E _ W I K I _ L I N K >---------------]] | |||
--[ | |||
] | |||
local function remove_wiki_link (str) | local function remove_wiki_link (str) | ||
return (str:gsub ("%[%[([^%[%]]*)%]%]", function(l) | return (str:gsub ("%[%[([^%[%]]*)%]%]", function(l) | ||
| Line 477: | Line 247: | ||
end | end | ||
--[[-------------------------< I S _ W I K I L I N K >-------------------------]] | |||
--[ | |||
] | |||
local function is_wikilink (str) | local function is_wikilink (str) | ||
local wl_type = 2; | |||
local wl_type = 2; | if not str:match ('^%[%[[^%]]+%]%]$') then return 0, str, ''; end | ||
local L, D = str:match ('^%[%[([^|]+)|([^%]]+)%]%]$'); | |||
if not str:match ('^%[%[[^%]]+%]%]$') then | if not is_set (D) then | ||
D = str:match ('^%[%[([^%]]*)|*%]%]$'); | |||
if not is_set (D) then | |||
D = str:match ('^%[%[([^%]]*)|*%]%]$'); | |||
wl_type = 1; | wl_type = 1; | ||
end | end | ||
D = mw.text.trim (D, '%s|'); | |||
D = mw.text.trim (D, '%s|'); | |||
return wl_type, D, L or ''; | return wl_type, D, L or ''; | ||
end | end | ||
--[[--------------------------< S T R I P _ A P O S T R O P H E _ M A R K U P >]] | |||
--[[--------------------------< S T R I P _ A P O S T R O P H E _ M A R K U P > | |||
]] | |||
local function strip_apostrophe_markup (argument) | local function strip_apostrophe_markup (argument) | ||
if not is_set (argument) | if not is_set (argument) or nil == argument:find ("''", 1, true) then | ||
return argument, nil; | return argument, nil; | ||
end | end | ||
local flag; | local flag; | ||
while true do | while true do | ||
if argument:find ("'''''", 1, true) then | if argument:find ("'''''", 1, true) then argument, flag = argument:gsub ("%'%'%'%'%'", ""); | ||
elseif argument:find ("''''", 1, true) then argument, flag=argument:gsub ("%'%'%'%'", ""); | |||
elseif argument:find ("''''", 1, true) then | elseif argument:find ("'''", 1, true) then argument, flag=argument:gsub ("%'%'%'", ""); | ||
elseif argument:find ("''", 1, true) then argument, flag = argument:gsub ("%'%'", ""); | |||
elseif argument:find ("'''", 1, true) then | else break; end | ||
elseif argument:find ("''", 1, true) then | |||
else | |||
end | end | ||
return argument, flag; | |||
return argument, flag; | |||
end | end | ||
--[[--------------------------< S E T _ S E L E C T E D _ M O D U L E S >------]] | |||
--[[--------------------------< S E T _ S E L E C T E D _ M O D U L E S >------ | |||
]] | |||
local function set_selected_modules (cfg_table_ptr) | local function set_selected_modules (cfg_table_ptr) | ||
cfg = cfg_table_ptr; | cfg = cfg_table_ptr; | ||
end | end | ||
return { | return { | ||
add_maint_cat = add_maint_cat, | add_maint_cat = add_maint_cat, | ||
add_prop_cat = add_prop_cat, | add_prop_cat = add_prop_cat, | ||
error_comment = error_comment, | error_comment = error_comment, | ||
| Line 587: | Line 300: | ||
substitute = substitute, | substitute = substitute, | ||
wrap_style = wrap_style, | wrap_style = wrap_style, | ||
z = z, | |||
z = z, | } | ||