Module:Song

--

local export = {} local find = mw.ustring.find local match = mw.ustring.match local sub = mw.ustring.sub local replace = mw.ustring.gsub local split = mw.text.split local lower = mw.ustring.lower local len = mw.ustring.len local resolve_redirect = require("module:resolve redirect").show

local function extract_sb(page_name) local sb = mw.title.new(page_name):getContent sb = resolve_redirect(sb)

local sb_start = find(sb, "{{song box 2\n") -- find where the song box starts in text sb = sub(sb, sb_start + len("{{song box 2\n")) -- reduce text to begin after the identifying text local sb_end = find(sb, "\n}}\n") -- find where the song box ends sb = sub(sb, 1, sb_end) -- reduce text to end before the brackets

return sb end

local function extract_sb_param(sb, sb_param, split_type) local param_start = find(sb, "|"..sb_param.." += ") -- find where parameter name starts in text sb = sub(sb, param_start, len(sb)) -- reduce text to begin with parameter name sb = replace(sb, "^|"..sb_param.." += ", "") -- remove parameter name from text sb = split(sb, "\n |") -- split by song box parameter sb = sb[1] -- keep the part before the next parameter starts

if split_type == "first line" then sb = match(sb, "^([^\n]+)") -- keep only first line elseif split_type == "all" then -- do nothing end

if not sb then sb = '' end -- idk man sometimes it chokes over undefined images

sb = replace(sb, "\n%* *", "\n") -- remove bullets at the beginning of a line and any spaces following them

return sb end

local function links(text) text = replace(text, "\n", " ")

local links = replace(text, "{{l|(..)|([^|{}]+)|?([^|{}]*)}}", "%1 %2;") local comments = replace(text, "{{l|(..)|([^|{}]+)|?([^|{}]*)}}", "%3;")

return mw.getCurrentFrame:expandTemplate{ title = "ico", args = { links = links, comments = comments } } end

local function title(text, page_name, manual_kanji, manual_rom) local original_title = text local romaji = ""

original_title = replace(original_title, '^%"(.+)%"$', "%1") -- strip title of quotation marks original_title = replace(original_title, "^%'%'%'(.+)%'%'%'$", "%1") -- strip title of bolding

if find(page_name, "%)$") then -- if page name ends in a parentheses		romaji = replace(page_name, "^.+ %((.+)%)$", "%1")	end

if manual_kanji ~= "" then original_title = manual_kanji end if manual_rom ~= "" then romaji = manual_rom end

if romaji == "" or romaji == "-" then return '"' .. original_title .. '"' else return '"' .. romaji .. '"' .. ' (' .. original_title .. ')' end end

local function image(text) local image = mw.title.new(text, "File") or ""

if image == "" then return '' --	else return ' ' -- class="hidden" → Help:Wikitext_best_practices end end

local function extract_roles(text) local text_split = split(text, "\n")

local producer_name = mw.title.getCurrentTitle.text local corresp = {} local doubt_text = mw.getCurrentFrame:expandTemplate{ title = "attention", args = {"are you sure this producer is involved?"} }

-- modified from w:c:u126:Module:Reciprocal link test for i = 1, #text_split, 1 do		if not find(text_split[i], "%[%[") then text_split[i] = "" else text_split[i] = replace(text_split[i], "%]%] %((.+)%)", "@@@%1") text_split[i] = replace(text_split[i], ".+%[%[", "") text_split[i] = replace(text_split[i], "|.+%]%]", "") text_split[i] = replace(text_split[i], "%[%[", "") text_split[i] = replace(text_split[i], "%]%]", "") if not find(text_split[i], "@@@") then text_split[i] = text_split[i] .. "@@@" -- so that asking for the producer roles doesn't return totally empty end text_split[i] = split(text_split[i], "@@@")

text_split[i][1] = lower(text_split[i][1]) -- to match $producer_name below corresp[text_split[i][1]] = text_split[i][2] -- text_split[i][1] is the producer name and text_split[i][2] is the roles end end

producer_name = lower(producer_name) -- lowercase for 'nakano4' etc: remember that the page name is always internally initial capitalized

return corresp[producer_name] or doubt_text end

function export.show(frame) local a = frame:getParent.args local page_name, kanji, rom, roles = a[1], a['kanji'] or "", a['rom'] or "", a['roles'] or "" local sb = extract_sb(page_name)

-- Extract each parameter and set it aside local sb_p = {} sb_p['links'] = extract_sb_param(sb, 'links', 'all') sb_p['title'] = extract_sb_param(sb, 'title', 'first line') sb_p['image'] = extract_sb_param(sb, 'image', 'first line') sb_p['singers'] = extract_sb_param(sb, 'singers', 'first line') sb_p['date'] = extract_sb_param(sb, 'date', 'first line') sb_p['producers'] = extract_sb_param(sb, 'producers', 'all')

-- Process each parameter local final_text = {} table.insert(final_text, links(sb_p['links'])) table.insert(final_text, title(sb_p['title'], page_name, kanji, rom) .. image(sb_p['image'])) table.insert(final_text, mw.getCurrentFrame:preprocess(sb_p['singers'])) table.insert(final_text, sb_p['date']) if roles ~= "" then table.insert(final_text, roles) else table.insert(final_text, extract_roles(sb_p['producers'])) end

return table.concat(final_text, " || ") end

return export