Revision as of 23:31, 30 January 2014 view sourceJackmcbarn (talk | contribs)31,380 edits make released date work← Previous edit | Latest revision as of 05:53, 15 November 2018 view source Jc86035 (talk | contribs)Extended confirmed users, Template editors34,237 edits Undid revision 868910974 by Zackmann08 (talk) this breaks the substitution for the deprecated parameters.Tag: Undo | ||
(15 intermediate revisions by 6 users not shown) | |||
Line 4: | Line 4: | ||
local function getArgNums(args, prefix) | local function getArgNums(args, prefix) | ||
-- Returns a table containing the numbers of the arguments that exist | -- Returns a table containing the numbers of the arguments that exist | ||
-- for the specified prefix. For example, if the prefix |
-- for the specified prefix. For example, if the prefix were to be 'data', and | ||
-- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}. | -- 'data1', 'data2', and 'data5' were to exist, it would return {1, 2, 5}. | ||
local nums = {} | local nums = {} | ||
for k, v in pairs(args) do | for k, v in pairs(args) do | ||
Line 13: | Line 13: | ||
table.sort(nums) | table.sort(nums) | ||
return nums | return nums | ||
end | |||
-- Forked from Module:Unsubst-infobox | |||
local specialParams = { | |||
= 'template content' | |||
} | |||
p = function ( frame ) | |||
if not frame.args then | |||
error( '{{#invoke:Singles|}} requires parameter $B (template content)' ) | |||
end | |||
if mw.isSubsting() then | |||
---- substing | |||
-- Passed args | |||
local args = {} | |||
⚫ | for k, v in pairs( frame:getParent().args ) do | ||
args = v | |||
end | |||
-- Build an equivalent template invocation | |||
-- First, find the title to use | |||
local titleobj = mw.title.new(frame:getParent():getTitle()) | |||
local title | |||
if titleobj.namespace == 10 then -- NS_TEMPLATE | |||
title = titleobj.text | |||
elseif titleobj.namespace == 0 then -- NS_MAIN | |||
title = ':' .. titleobj.text | |||
else | |||
title = titleobj.prefixedText | |||
end | |||
-- Remove empty fields | |||
⚫ | for k, v in pairs( args ) do | ||
if v == '' then args = nil end | |||
end | |||
-- Pull aliases | |||
⚫ | local nums = getArgNums(args, 'ingle ?') | ||
for _, num in ipairs(nums) do | |||
⚫ | args = args or args or args | ||
args = args or args or args or '' | |||
args, args, args, args = nil, nil, nil, nil | |||
end | |||
for k, v in pairs( {Type = 'type', Name = 'name'} ) do | |||
if args and not args then args, args = args, nil end | |||
end | |||
-- Build the invocation body | |||
local ret = '{{' .. title | |||
-- Make parameter list | |||
local params = {'name', 'type'} | |||
for _, num in ipairs( nums ) do table.insert( params, 'single' .. num ); table.insert( params, 'single' .. num .. 'date' ) end | |||
-- Align parameters correctly and remove extra ones | |||
local maxlength = 0 | |||
for k, v in ipairs( params ) do | |||
local tmp = mw.ustring.len( v ) | |||
if tmp > maxlength then maxlength = tmp end | |||
end | |||
for k, v in ipairs( params ) do | |||
ret = ret .. '\n | ' .. v .. string.rep(' ', (maxlength - mw.ustring.len( v ))) .. ' = ' .. (args or '') | |||
end | |||
ret = ret .. '\n}}' | |||
ret = mw.ustring.gsub(ret, '%s+\n', '\n') | |||
return ret | |||
else | |||
-- Not substing | |||
-- Just return the "body" | |||
return frame.args | |||
end | |||
end | end | ||
function p.main(frame) | function p.main(frame) | ||
local args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:Singles'}) | |||
⚫ | |||
local out = |
local out = '' | ||
local nums = getArgNums(args, 'ingle ?') | |||
<th style="background:{{Template:Infobox album/color|]=] .. (args.Type or '') .. ] from '']=] .. (args.Name or '') .. [=[''</th> | |||
for _, num in ipairs(nums) do | |||
</tr> | |||
⚫ | out = out .. '\n# <span class="item"><span class="fn">"' .. (args or args or args) .. '"</span>' | ||
<tr style="text-align:left; vertical-align:top; line-height:11px;"> | |||
local date = args or args or args | |||
<td colspan="3">]=] | |||
⚫ | |||
⚫ | |||
⚫ | out = out .. '\n# <span class="item"><span class="fn">"' .. (args or args) .. '"</span>' | ||
⚫ | |
||
if date then | if date then | ||
out = out .. '<br />Released: ' .. date | out = out .. '<br />Released: ' .. date | ||
Line 31: | Line 104: | ||
out = out .. '</span>' | out = out .. '</span>' | ||
end | end | ||
out = out .. [=[</td> | |||
if out ~= '' then | |||
</tr>]=] | |||
if mw.ustring.match(out, '</?t') then out = out .. ' ]' end | |||
return '<div style="text-align:left">' .. out .. '\n</div>' | |||
end | |||
return out | return out | ||
end | end |
Latest revision as of 05:53, 15 November 2018
Module documentation[view] [edit] [history] [purge]This module implements the {{Singles}} template. Please see the template page for usage instructions.
The above documentation is transcluded from Module:Singles/doc. (edit | history)Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.
local p = {} -- Ripped from Module:Infobox. TODO: Make a utility module that can do this kind of thing local function getArgNums(args, prefix) -- Returns a table containing the numbers of the arguments that exist -- for the specified prefix. For example, if the prefix were to be 'data', and -- 'data1', 'data2', and 'data5' were to exist, it would return {1, 2, 5}. local nums = {} for k, v in pairs(args) do local num = tostring(k):match('^' .. prefix .. '(%d*)$') if num then table.insert(nums, tonumber(num)) end end table.sort(nums) return nums end -- Forked from Module:Unsubst-infobox local specialParams = { = 'template content' } p = function ( frame ) if not frame.args then error( '{{#invoke:Singles|}} requires parameter $B (template content)' ) end if mw.isSubsting() then ---- substing -- Passed args local args = {} for k, v in pairs( frame:getParent().args ) do args = v end -- Build an equivalent template invocation -- First, find the title to use local titleobj = mw.title.new(frame:getParent():getTitle()) local title if titleobj.namespace == 10 then -- NS_TEMPLATE title = titleobj.text elseif titleobj.namespace == 0 then -- NS_MAIN title = ':' .. titleobj.text else title = titleobj.prefixedText end -- Remove empty fields for k, v in pairs( args ) do if v == '' then args = nil end end -- Pull aliases local nums = getArgNums(args, 'ingle ?') for _, num in ipairs(nums) do args = args or args or args args = args or args or args or '' args, args, args, args = nil, nil, nil, nil end for k, v in pairs( {Type = 'type', Name = 'name'} ) do if args and not args then args, args = args, nil end end -- Build the invocation body local ret = '{{' .. title -- Make parameter list local params = {'name', 'type'} for _, num in ipairs( nums ) do table.insert( params, 'single' .. num ); table.insert( params, 'single' .. num .. 'date' ) end -- Align parameters correctly and remove extra ones local maxlength = 0 for k, v in ipairs( params ) do local tmp = mw.ustring.len( v ) if tmp > maxlength then maxlength = tmp end end for k, v in ipairs( params ) do ret = ret .. '\n | ' .. v .. string.rep(' ', (maxlength - mw.ustring.len( v ))) .. ' = ' .. (args or '') end ret = ret .. '\n}}' ret = mw.ustring.gsub(ret, '%s+\n', '\n') return ret else -- Not substing -- Just return the "body" return frame.args end end function p.main(frame) local args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:Singles'}) local out = '' local nums = getArgNums(args, 'ingle ?') for _, num in ipairs(nums) do out = out .. '\n# <span class="item"><span class="fn">"' .. (args or args or args) .. '"</span>' local date = args or args or args if date then out = out .. '<br />Released: ' .. date end out = out .. '</span>' end if out ~= '' then if mw.ustring.match(out, '</?t') then out = out .. ' ]' end return '<div style="text-align:left">' .. out .. '\n</div>' end return out end return p