Module:Reformat

local export = {} local replace = mw.ustring.gsub local trim = mw.text.trim local find = mw.ustring.find local match = mw.ustring.match

-- {{subst:#invoke:reformat|derivatives -- {{subst:#invoke:reformat|derivatives_tabber -- {{subst:#invoke:reformat|song_box_new -- {{subst:#invoke:reformat|producer_song_box

function export.derivatives(frame) local text = trim([[

{{Derivatives | | | | | |
 *  | | |
 *  | | | | | |<sc_id2> |<sc2_comment> |<5s_id> |<5s_comment>
 * <mz_id> |<mz_comment> |<vm_id> |<vm_comment> |<tm_id> |<tm_comment>
 * |<etc_comment> | |<etc2_comment>
 * |<romaji (english)>
 * | | }}
 * | | }}
 * | | }}
 * | | }}

]])

local keep = { 'nnd_id', 'yt_id', 'yt_comment', 'author', 'category', 'title', 'description', 'color' }

local optional = { 'nnd_comment', 'bb_id', 'bb_comment', 'nnd_id2', 'nnd2_comment', 'yt_id2', 'yt2_comment', 'pp_id', 'pp_comment', 'piapro_id', 'piapro_comment', 'sc_id', 'sc_comment', 'sc_id2', 'sc2_comment', '5s_id', '5s_comment', 'mz_id', 'mz_comment', 'vm_id', 'vm_comment', 'tm_id', 'tm_comment', 'etc', 'etc_comment', 'etc2', 'etc2_comment', 'singer', 'romaji %(english%)', 'width', 'height', 'icon', 'thumb' }

local p = frame.args

for param, value in pairs(p) do		if param == 'title' then value = replace(value, 'dance cover', 'Dance Cover') value = replace(value, 'cover', 'Cover') value = replace(value, 'demo', 'Demo') value = replace(value, ' x ', ' × ') end if param == 'author' then -- value = replace(value, ' %([A-Z][a-z][A-Za-z, ]+', mw.ustring.lower) -- 「(Cover)」→「(cover)」 but not 「(UST)」→「(uST)」				-- converts 「Rin tuning」 to 「rin tuning」 like it's told to, but this isn't what we want so whatever			value = replace(value, ' x ', ' × ')		end		if param == 'category' then			value = replace(value, 'Cover', 'cover')			value = replace(value, '[Uu]taite', 'Human')			value = replace(value, '[Uu][Tt][Aa][Uu]', 'UTAU')			value = replace(value, '[Vv][Oo][Cc][Aa][Ll][Oo][Ii][Dd]', 'VOCALOID')			-- value = replace(value, ';', ',')		end		if param == 'color' then			value = replace(value, ' *; *color: *', '; color:')			value = replace(value, ';$', '')		end		if param == 'romaji (english)' then			param = 'romaji %(english%)'		end

if value ~= '' then text = replace(text, '|<'..param..'>', '|'..param..' = '..value) end end

for i, param in ipairs(keep) do		text = replace(text, '|<'..param..'> ?', '|'..param..' = ') end for i, param in ipairs(optional) do		text = replace(text, '|<'..param..'> ?', '') end

text = replace(text, ' +', ' ') text = replace(text, '\n+', '\n') text = replace(text, ' \n', '\n') text = replace(text, '=\n', '= \n')

text = replace(text, 'romaji %%%(english%%%) = ', 'romaji (english) = ')

text = replace(text, '|nnd_id = |yt_id = |yt_comment = \n', '') text = replace(text, 'Derivatives |singer', 'Derivatives\n|singer') text = replace(text, 'Derivatives |author', 'Derivatives\n|author')

return text end

function export.derivatives_tabber(frame) local text = trim([[

{{Derivatives tabber |vocaloid = |VOCALOID = <VOCALOID>

|utau = |UTAU = <UTAU>

|other = |OTHER = <OTHER>

|human = |HUMAN = <HUMAN>

|dance = |DANCE = <DANCE>

|arrangement = |ARRANGEMENT = <ARRANGEMENT>

|fanmadepv = |FANMADEPV = <FANMADEPV>

|spinoff = |SPINOFF = <SPINOFF>

|more = |MORE = <MORE> }}

]])

local list_lower = { 'vocaloid', 'utau', 'other', 'human', 'dance', 'arrangement', 'fanmadepv', 'spinoff', 'more' } local list_upper = { 'VOCALOID', 'UTAU', 'OTHER', 'HUMAN', 'DANCE', 'ARRANGEMENT', 'FANMADEPV', 'SPINOFF', 'MORE' } local list, other = {}, {}

local p = frame.args

if p['VOCALOID'] or p['UTAU'] or p['OTHER'] or p['HUMAN'] or p['DANCE'] or p['ARRANGEMENT'] or p['FANMADEPV'] or p['SPINOFF'] or p['MORE'] then list = list_upper other = list_lower else list = list_lower other = list_upper end

for param, value in pairs(p) do		if value ~= '' then text = replace(text, '<'..param..'>', value) end end

for i, param in ipairs(list) do		text = replace(text, '\n<'..param..'>', '') end for i, param in ipairs(other) do		text = replace(text, '\n |'..param..' =\n<'..param..'>', '') end

return text end

function export.song_box_new(frame) local text = trim([[

{{Song box 2 |color    = |image    = |title    = |date     = |views    = |singers  = |producers = |links    = }}

]])

local list = { 'color', 'image', 'title', 'date', 'views', 'singers', 'producers', 'links' }

local p = frame.args

for param, value in pairs(p) do		if param == 'title' or param == 'singers' or param == 'producers' or param == 'links' then value = replace(value, '\n%* *', '\n* ') value = replace(value, ' ?<[Bb][Rr] ?%/?> ?', '\n* ') end if param == 'views' then value = replace(value, '&', 'and') end if param == 'color' then value = replace(value, ' *; *color: *', '; color:') value = replace(value, ';$', '') end

if value ~= '' then text = replace(text, '<'..param..'>', value) end end

for i, param in ipairs(list) do		text = replace(text, '<'..param..'>', '') end

return text end

function export.producer_song_box(frame) local text = trim([[

{{Infobox song
 * <nnd_id> |<nnd_comment> |<yt_id> |<yt_comment> |<bb_id> |<bb_comment>
 * <nnd_id2> |<nnd2_comment> |<yt_id2> |<yt2_comment>
 * <pp_id> |<pp_comment> |<piapro_id> |<piapro_comment> |<sc_id> |<sc_comment> |<5s_id> |<5s_comment>
 * <mz_id> |<mz_comment> |<vm_id> |<5s_comment> |<tm_id> |<tm_comment>
 * |<etc_name> |<etc_comment> |<xtra_url> |<xtra_name> |<xtra_comment>
 * <overallPVwork> | |
 * | | }}
 * | | }}
 * | | }}
 * | | }}
 * | | }}

]])

local keep = { 'title1', 'nnd_id', 'yt_id', 'yt_comment', 'uploaddate', 'vocaloid', 'composer', 'lyricist', 'description' } local keep_2 = { 'overallPVwork', 'illustrator', 'videoeditor' }

local optional = { 'title2', 'icon', 'thumb', 'xtra_url', 'etc', 'xtra_name', 'etc_name', 'xtra_comment', 'etc_comment', 'bb_id', 'sc_id', '5s_id', 'mz_id', 'nnd_comment', 'nnd_id2', 'nnd2_comment', 'yt_id2', 'yt2_comment', 'bb_comment', 'piapro_id', 'pp_id', 'piapro_comment', 'pp_comment', 'sc_comment', '5s_comment', 'mz_comment', 'vm_id', 'vm_comment', 'tm_id', 'tm_comment' }

local p = frame.args

if p['overallpvwork'] then p['overallPVwork'] = p['overallpvwork'] end

for param, value in pairs(p) do		if value ~= '' then text = replace(text, '|<'..param..'>', '|'..param..' = '..value) end end

for i, param in ipairs(keep) do		text = replace(text, '|<'..param..'> ?', '|'..param..' = ') end for i, param in ipairs(optional) do		text = replace(text, '|<'..param..'> ?', '') end

if p['overallPVwork'] then text = replace(text, '| ?', '') text = replace(text, '| ?', '') else text = replace(text, '|<overallPVwork> ?', '|overallPVwork = ') text = replace(text, '| ?', '') text = replace(text, '| ?', '') end

text = replace(text, ' +', ' ') text = replace(text, '\n+', '\n') text = replace(text, ' \n', '\n') text = replace(text, '=\n', '= \n')

text = replace(text, '|nnd_id = |yt_id = |yt_comment = \n?', '') text = replace(text, '|nnd_id = |', '|')

return text end

return export