Skip to content

Commit ba509a6

Browse files
authored
Add main.lua for political party module functionality
1 parent fb4b49a commit ba509a6

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

‎Political party/main.lua‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
local p = {}
2+
3+
local default_color = '#F8F9FA'
4+
5+
local categories = {
6+
party_not_in_list = '[[Category:Pages using Political party with unknown party]]',
7+
shortname_not_in_list = '[[Category:Pages using Political party with missing shortname]]',
8+
color_not_in_list = '[[Category:Pages using Political party with missing color]]',
9+
}
10+
11+
local function create_error(error_message)
12+
return string.format('<strong class="error">%s</strong>', error_message)
13+
end
14+
15+
local function getFirstLetter(party)
16+
local index = mw.ustring.sub(party, 1, 1)
17+
-- Set index for non-A-Z starts
18+
if string.match(index, '%A') then
19+
return '1'
20+
end
21+
return string.upper(index)
22+
end
23+
24+
local function stripToNil(text)
25+
-- If text is a string, return its trimmed content, or nil if empty.
26+
-- Otherwise return text (which may, for example, be nil).
27+
if type(text) == 'string' then
28+
text = text:match('(%S.-)%s*$')
29+
local delink = require('Module:Delink')._delink
30+
text = delink({text, wikilinks = "target"})
31+
end
32+
return text
33+
end
34+
35+
-- 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.
36+
function p._fetch(args)
37+
if not args[1] then
38+
return create_error("parameter 1 should be a party name.")
39+
end
40+
41+
if not args[2] then
42+
return create_error("parameter 2 should be the output type.")
43+
end
44+
45+
local party = stripToNil(args[1])
46+
local out_type = stripToNil(args[2])
47+
if out_type == 'colour' then
48+
out_type = 'color'
49+
end
50+
local index = getFirstLetter(party)
51+
52+
-- Load data from submodule
53+
local data = mw.loadData('Module:Political party/' .. index)
54+
local data_all = data.full
55+
56+
local party_alt = data.alternate[party]
57+
local party_info
58+
if party_alt then
59+
if data_all[party_alt] then
60+
party_info = data_all[party_alt]
61+
else
62+
index = getFirstLetter(party_alt)
63+
data = mw.loadData('Module:Political party/' .. index)
64+
party_info = data.full[party_alt]
65+
end
66+
else
67+
party_info = data_all[party]
68+
end
69+
70+
-- Check if database value exists
71+
-- * Not even in database - return given error or input
72+
-- * No color - return error
73+
-- * No shortname/abbrev - return first non-blank of abbrev->shortname->input
74+
if not party_info then
75+
if out_type == 'color' then
76+
return args.error or default_color
77+
else
78+
return args.error or party
79+
end
80+
end
81+
local return_value = party_info[out_type]
82+
if return_value == "" then
83+
if out_type == 'color' then
84+
return args.error or create_error("Value not in template. Please request that it be added.")
85+
elseif out_type == 'abbrev' then
86+
if party_info.shortname ~= "" then
87+
return party_info.shortname
88+
else
89+
return party
90+
end
91+
elseif out_type == 'shortname' then
92+
if party_info.abbrev ~= "" then
93+
return party_info.abbrev
94+
else
95+
return party
96+
end
97+
else
98+
return party
99+
end
100+
end
101+
102+
if out_type == 'color' and string.find(return_value, '#') then
103+
return_value = string.gsub(return_value, '#', '&#35;')
104+
end
105+
return return_value
106+
end
107+
108+
function p.fetch(frame)
109+
-- Initialise and populate variables
110+
local getArgs = require("Module:Arguments").getArgs
111+
local args = getArgs(frame)
112+
113+
return p._fetch(args)
114+
end
115+
116+
return p

0 commit comments

Comments
 (0)