FANDOM


-- USER:XUANMIEN Module:Song
 
-- <nowiki>
 
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, index)
    local sb = mw.title.new(page_name):getContent()
    sb = resolve_redirect(sb)
 
    for i = 1, index do
        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
    end
    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", " ")
	text = mw.text.trim(text)
 
    if text == 'none' then
        return 'None'
    end
 
	local links = replace(text, "{{l|1?%s*=?%s*(..)|2?%s*=?%s*([^|{}]+)|?3?%s*=?%s*([^|{}]*)|?4?%s*=?%s*([^|{}]*)}}", "%1 %2;")
	local comments = replace(text, "{{l|1?%s*=?%s*(..)|2?%s*=?%s*([^|{}]+)|?3?%s*=?%s*([^|{}]*|?4?%s*=?%s*([^|{}]*))}}", "%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 manual_kanji ~= "" then
		original_title = manual_kanji
	end
	if manual_rom ~= "" then
		romaji = manual_rom
	end
 
	if romaji == "" or romaji == "-" then
		return '"[[' .. page_name .. '|' .. original_title .. ']]"'
	else
		return '"[[' .. page_name .. '|' .. romaji .. ']]"' .. ' (' .. original_title .. ')'
	end
end
 
local function image(text)
	local image = mw.title.new(text, "File") or ""
 
	if image == "" then
		return ''
--[==[
	elseif not image.exists then
		return '<strong class="error">[[File:' .. text .. "]] doesn't exist!</strong> [[Category:Error/Producer table]]"
]==]
	else
		return '<span class="producer-table-thumbnail hidden">[[File:' .. text .. '|120x90px|link=]]</span>' -- class="hidden" → [[Help:Wikitext_best_practices]]
	end
end
 
local function extract_roles(text)
	local text_split = split(text, "\n")
 
	local producer_name = lower(mw.text.trim(mw.title.getCurrentTitle().rootText:match("^([^%(]*)")))
	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
		local prod = match(text_split[i], "^%s*%[%[[^|]-|?([^|]+)%]%]")
		local roles = match(text_split[i], "%]%]%s*%((.*)%)%s*$")
		if prod and roles then
			corresp[lower(prod)] = roles
		elseif prod then
			corresp[lower(prod)] = "producer"
		end
	end
 
	return corresp[producer_name] or doubt_text
end
 
function export.show(frame)
	local a = frame:getParent().args
	local page_name, kanji, rom, roles, index = a[1], a['kanji'] or "", a['rom'] or "", a['roles'] or "", a['index'] and tonumber(a['index']) or 1
	local sb = extract_sb(page_name, index)
 
	-- Extract each parameter and set it aside
	local sb_p = {}
	sb_p['links'] = extract_sb_param(sb, 'links', 'first line')
	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
Community content is available under CC-BY-SA unless otherwise noted.