Difference between revisions of "Module:Portal toolbox"
Jump to navigation
Jump to search
test>Andrybak (reduce amount of duplication needed) |
m (9 revisions imported) |
||
| (4 intermediate revisions by one other user not shown) | |||
| Line 3: | Line 3: | ||
p = {} | p = {} | ||
| − | local function | + | local function add_header_row(frame, tbl, text) |
local row = tbl:tag('tr') | local row = tbl:tag('tr') | ||
row:tag('th') | row:tag('th') | ||
| Line 11: | Line 11: | ||
local function edit(frame, pagename) | local function edit(frame, pagename) | ||
| − | return frame:expandTemplate{ title='edit', args = { pagename } } | + | return frame:expandTemplate{ title='edit', args={ pagename } } |
end | end | ||
| − | local function | + | local function add_template_row(frame, tbl, pagename) |
| + | local row = tbl:tag('tr') | ||
| + | row:tag('td') | ||
| + | :wikitext(frame:expandTemplate{ title='tl', args={ pagename } }) | ||
| + | row:tag('td') | ||
| + | :wikitext(edit(frame, 'Template:' .. pagename)) | ||
| + | end | ||
| + | |||
| + | local function add_wikilink_row(frame, tbl, pagename, text, right) | ||
local row = tbl:tag('tr') | local row = tbl:tag('tr') | ||
row:tag('td') | row:tag('td') | ||
:wikitext('[[' .. pagename .. '|' .. (text or pagename) .. ']]') | :wikitext('[[' .. pagename .. '|' .. (text or pagename) .. ']]') | ||
row:tag('td') | row:tag('td') | ||
| − | :wikitext(right) | + | :wikitext(right or edit(frame, pagename)) |
end | end | ||
| − | local function | + | local function add_section(frame, args, tbl, add_section_header, arg_prefix, page_prefix, row_function) |
| − | + | if row_function == nil then | |
| + | row_function = add_wikilink_row | ||
| + | end | ||
local nums = {} | local nums = {} | ||
for k, _ in pairs(args) do | for k, _ in pairs(args) do | ||
if type(k) == 'string' then | if type(k) == 'string' then | ||
| − | local num = k:match('^' .. | + | local num = k:match('^' .. arg_prefix .. '(%d+)$') |
if num then | if num then | ||
table.insert(nums, tonumber(num)) | table.insert(nums, tonumber(num)) | ||
end | end | ||
end | end | ||
| + | end | ||
| + | if #nums == 0 then | ||
| + | return | ||
end | end | ||
table.sort(nums) | table.sort(nums) | ||
| + | add_header_row(frame, tbl, add_section_header) | ||
for _, num in ipairs(nums) do | for _, num in ipairs(nums) do | ||
| − | local arg_name = | + | local arg_name = arg_prefix .. num |
| − | local | + | local arg = args[arg_name] |
| − | local pagename = page_prefix .. | + | local pagename = page_prefix .. arg |
local text = args[arg_name .. 'text'] | local text = args[arg_name .. 'text'] | ||
local right = args[arg_name .. 'right'] | local right = args[arg_name .. 'right'] | ||
| − | + | row_function(frame, tbl, pagename, text or arg, right) | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
end | end | ||
end | end | ||
| Line 56: | Line 64: | ||
tbl:tag('caption') | tbl:tag('caption') | ||
:wikitext('Portal toolbox') | :wikitext('Portal toolbox') | ||
| − | + | add_header_row(frame, tbl, 'Main portal page') | |
local rootTitle = mw.title.getCurrentTitle().rootPageTitle.subjectPageTitle | local rootTitle = mw.title.getCurrentTitle().rootPageTitle.subjectPageTitle | ||
| − | + | add_wikilink_row(frame, tbl, rootTitle.fullText) | |
| − | + | add_section(frame, args, tbl, 'Static subpages', 'static', rootTitle.fullText .. '/') | |
| − | + | add_section(frame, args, tbl, 'Dynamic subpages', 'dynamic', rootTitle.fullText .. '/') | |
| − | + | add_section(frame, args, tbl, 'Templates', 'template', '', add_template_row) | |
| + | add_section(frame, args, tbl, 'Other', 'other', '') | ||
return tbl | return tbl | ||
end | end | ||
Latest revision as of 17:26, 8 September 2020
| This module is rated as pre-alpha. It is unfinished, and may or may not be in active development. It should not be used from article namespace pages. Modules remain pre-alpha until the original editor (or someone who takes one over if it is abandoned for some time) is satisfied with the basic structure. |
This module implements {{Portal toolbox}}. Please see the template page for documentation.
local getArgs = require('Module:Arguments').getArgs
p = {}
local function add_header_row(frame, tbl, text)
local row = tbl:tag('tr')
row:tag('th')
:attr('colspan', '2')
:wikitext(text)
end
local function edit(frame, pagename)
return frame:expandTemplate{ title='edit', args={ pagename } }
end
local function add_template_row(frame, tbl, pagename)
local row = tbl:tag('tr')
row:tag('td')
:wikitext(frame:expandTemplate{ title='tl', args={ pagename } })
row:tag('td')
:wikitext(edit(frame, 'Template:' .. pagename))
end
local function add_wikilink_row(frame, tbl, pagename, text, right)
local row = tbl:tag('tr')
row:tag('td')
:wikitext('[[' .. pagename .. '|' .. (text or pagename) .. ']]')
row:tag('td')
:wikitext(right or edit(frame, pagename))
end
local function add_section(frame, args, tbl, add_section_header, arg_prefix, page_prefix, row_function)
if row_function == nil then
row_function = add_wikilink_row
end
local nums = {}
for k, _ in pairs(args) do
if type(k) == 'string' then
local num = k:match('^' .. arg_prefix .. '(%d+)$')
if num then
table.insert(nums, tonumber(num))
end
end
end
if #nums == 0 then
return
end
table.sort(nums)
add_header_row(frame, tbl, add_section_header)
for _, num in ipairs(nums) do
local arg_name = arg_prefix .. num
local arg = args[arg_name]
local pagename = page_prefix .. arg
local text = args[arg_name .. 'text']
local right = args[arg_name .. 'right']
row_function(frame, tbl, pagename, text or arg, right)
end
end
local function main(frame)
local args = getArgs(frame)
local tbl = mw.html.create('table')
:cssText('float:right; border:1px navy solid;')
tbl:tag('caption')
:wikitext('Portal toolbox')
add_header_row(frame, tbl, 'Main portal page')
local rootTitle = mw.title.getCurrentTitle().rootPageTitle.subjectPageTitle
add_wikilink_row(frame, tbl, rootTitle.fullText)
add_section(frame, args, tbl, 'Static subpages', 'static', rootTitle.fullText .. '/')
add_section(frame, args, tbl, 'Dynamic subpages', 'dynamic', rootTitle.fullText .. '/')
add_section(frame, args, tbl, 'Templates', 'template', '', add_template_row)
add_section(frame, args, tbl, 'Other', 'other', '')
return tbl
end
p.main = main
return p