Module:Political party linked: Difference between revisions
Appearance
Content deleted Content added
No edit summary |
update from sandbox |
||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
local |
local color = '#F8F9FA' |
||
⚫ | |||
local categories = { |
local categories = { |
||
Line 39: | Line 40: | ||
end |
end |
||
-- Example of having all the data - color and names - in one table. Requires one page to be edited instead of two when adding a new party. |
|||
function p._fetch(args) |
function p._fetch(args) |
||
if not args[1] then |
if not args[1] then |
||
Line 49: | Line 49: | ||
end |
end |
||
local party = stripToNil(args[1]) |
|||
local out_type = stripToNil(args[2]) |
local out_type = stripToNil(args[2]) |
||
⚫ | |||
out_type = 'color' |
|||
⚫ | |||
local index = getFirstLetter(party) |
local index = getFirstLetter(party) |
||
Line 61: | Line 58: | ||
local party_alt = data.alternate[party] |
local party_alt = data.alternate[party] |
||
⚫ | |||
if party_alt then |
if party_alt then |
||
party = party_alt |
party = party_alt |
||
Line 77: | Line 73: | ||
-- Check if database value exists |
-- Check if database value exists |
||
-- * Not even in database - return given error or input |
-- * Not even in database - return given error or input |
||
-- * No color - return error |
|||
-- * No shortname/abbrev - return first non-blank of abbrev->shortname->input |
-- * No shortname/abbrev - return first non-blank of abbrev->shortname->input |
||
if not party_info then |
if not party_info then |
||
⚫ | |||
if out_type == 'color' then |
|||
return args.error or default_color |
|||
else |
|||
⚫ | |||
⚫ | |||
end |
end |
||
local display_name = party_info[out_type] |
local display_name = party_info[out_type] |
||
if display_name == "" then |
if display_name == "" then |
||
if out_type == ' |
if out_type == 'abbrev' then |
||
⚫ | |||
elseif out_type == 'abbrev' then |
|||
if party_info.shortname ~= "" then |
if party_info.shortname ~= "" then |
||
display_name = party_info.shortname |
display_name = party_info.shortname |
||
Line 110: | Line 99: | ||
elseif out_type == 'dab' then |
elseif out_type == 'dab' then |
||
display_name = party |
display_name = party |
||
⚫ | |||
⚫ | |||
else |
else |
||
return nil |
|||
⚫ | |||
end |
end |
||
end |
end |
||
if out_type == 'color' then return string.gsub(display_name, '#', '#') end |
|||
local link = party_info.link |
local link = party_info.link |
||
Line 126: | Line 115: | ||
return display_name |
return display_name |
||
end |
end |
||
⚫ | |||
function p._tagCfetch(args) |
|||
local linked_party = p._fetch(args) |
|||
if party_info then |
|||
⚫ | |||
⚫ | |||
return linked_party, color |
|||
end |
end |
||
Revision as of 16:42, 1 February 2023
local p = {}
local color = '#F8F9FA'
local party_info
local categories = {
party_not_in_list = '[[Category:Pages using Political party with unknown party]]',
shortname_not_in_list = '[[Category:Pages using Political party with missing shortname]]',
color_not_in_list = '[[Category:Pages using Political party with missing color]]',
}
local function create_error(error_message)
return string.format('<strong class="error">%s</strong>', error_message)
end
local function getFirstLetter(party)
local index = mw.ustring.sub(party, 1, 1)
-- Set index for non-A-Z starts
if string.match(index, '%A') then
return '1'
end
return string.upper(index)
end
local function page_exist(page_name)
--if require('Module:Disambiguation')._isDisambiguationPage(page_name) then return false end
if mw.title.new(page_name).exists then return true end
return false
end
local function stripToNil(text)
-- If text is a string, return its trimmed content, or nil if empty.
-- Otherwise return text (which may, for example, be nil).
if type(text) == 'string' then
text = text:match('(%S.-)%s*$')
local delink = require('Module:Delink')._delink
text = delink({text, wikilinks = "target"})
end
return text
end
function p._fetch(args)
if not args[1] then
return create_error("parameter 1 should be a party name.")
end
if not args[2] then
return create_error("parameter 2 should be the output type.")
end
local party = stripToNil(args[1])
local out_type = stripToNil(args[2])
local index = getFirstLetter(party)
-- Load data from submodule
local data = mw.loadData('Module:Political party/' .. index)
local data_all = data.full
local party_alt = data.alternate[party]
if party_alt then
party = party_alt
if data_all[party_alt] then
party_info = data_all[party_alt]
else
index = getFirstLetter(party_alt)
data = mw.loadData('Module:Political party/' .. index)
party_info = data.full[party_alt]
end
else
party_info = data_all[party]
end
-- Check if database value exists
-- * Not even in database - return given error or input
-- * No shortname/abbrev - return first non-blank of abbrev->shortname->input
if not party_info then
return args.error or party
end
local display_name = party_info[out_type]
if display_name == "" then
if out_type == 'abbrev' then
if party_info.shortname ~= "" then
display_name = party_info.shortname
else
display_name = party
end
elseif out_type == 'shortname' then
if party_info.abbrev ~= "" then
display_name = party_info.abbrev
else
display_name = party
end
else
display_name = party
end
elseif not display_name then
if out_type == 'full' then
display_name = mw.ustring.gsub(party, '%s+%b()$', '')
elseif out_type == 'dab' then
display_name = party
elseif out_type == '_shortname' then
display_name = args[3]
else
return nil
end
end
local link = party_info.link
if link then
if link == "" then return display_name end
return string.format("[[%s|%s]]", link, display_name)
elseif page_exist(party) then
return string.format("[[%s|%s]]", party, display_name)
else
return display_name
end
end
function p._tagCfetch(args)
local linked_party = p._fetch(args)
if party_info then
color = (party_info.color == "") and create_error("Value not in template. Please request that it be added.") or string.gsub(party_info.color, '#', '#')
end
return linked_party, color
end
function p.fetch(frame)
-- Initialise and populate variables
local getArgs = require("Module:Arguments").getArgs
local args = getArgs(frame)
return p._fetch(args)
end
return p