Module:Lyrics

From Blue Archive Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Lyrics/doc

-- This module is adapted from https://zh.moegirl.org.cn/Module:Lyrics and https://zh.moegirl.org.cn/Module:Lyrics/Roma. See their history pages for attribution.
-- This module is licensed under CC BY-NC-SA 3.0, which is different from other content on this wiki. Please note that changes
-- made to this page are not licensed under CC BY-SA 4.0.

local p = {}

local getArgs = require('Module:Arguments').getArgs
local lang = require('Module:Lang')

function p._lyrics(args, hookTrigger, customArgs)
	local original = args.original or ''
	local translated = args.translated or ''
	if hookTrigger then
		original, translated = hookTrigger('preSplit', original, translated, customArgs)
	end

	--mw.text.split的效率太低啦,我愿称之为shit
	local orig = {}
	local iter = mw.ustring.gmatch(original..'\n', '([^\n]*)\n')
	for val in iter do
		table.insert(orig, val)
	end
	local tran = {}
	local iter = mw.ustring.gmatch(translated..'\n', '([^\n]*)\n')
	for val in iter do
		table.insert(tran, val)
	end

	local llang = args.llang or 'ja'
	local rlang = args.rlang or 'en'
	local lstyle = args.lstyle or ''
	local rstyle = args.rstyle or ''

	local html = mw.html.create('div')
					:addClass('Lyrics')
					:css('width', args.width)
					:cssText(args.containerstyle or '')
					
	local hover = args.hover or (translated == '' and "false" or "true")
	if hover == "true" then
		hover = true
		html:addClass("Lyrics-hover")
	end

	if ((args['hasRuby'] or '') ~= '') or (not args.colorsMode and (args.original or ''):find('<ruby')) then
		html:addClass('Lyrics-has-ruby')
	else
		html:addClass('Lyrics-no-ruby')
	end

	local hastran = true
	if #tran == 1 and tran[1] == '' then
		hastran = false
	else
		html:addClass('Lyrics-has-translate')
	end

	if hookTrigger then
		hookTrigger('preParse', orig, hastran and tran or false, customArgs)
	end
	
	-- special content ported from Module:Lyrics/Roma
	local romaji_toggle = ""
	if args.romaji then
		local romaji = args.romaji
		local furigana = {}
		local iter = mw.ustring.gmatch(romaji..'\n', '([^\n]*)\n')
		for val in iter do
			table.insert(furigana, val)
		end
		local combined = {}
		local romaji_name = args.name or "lyrics-romaji"
		romaji_toggle = '<div style="float:right">[<span class="mw-customtoggle-' .. romaji_name ..'" style="color: rgb(6, 69, 173)">Toggle furigana</span>]</div>'
		for i=1, #orig do
			local furigana_line = furigana[i] or ""
			-- the swear word is kept to honor the original author (User:实验性:无用论废人)
			local fuck = '<ruby>' .. orig[i] .. '<rp>(</rp><rt style="font-size:0.75em"><span class="mw-collapsible mw-uncollapsed" id="mw-customcollapsible-' .. romaji_name .. '" style="">' .. furigana[i] .. '</span></rt><rp>)</rp></ruby>'
			table.insert( combined , fuck )
		end
		-- replace original with the combined one
		orig = combined
	end

	local lines = {}
	local len = math.max(#orig, #tran)
	for i=1, len do
		local line_original = orig[i] or ''
		local line = mw.html.create('div'):addClass('Lyrics-line')
			:tag('div')
				:addClass('Lyrics-original')
				:cssText(lstyle)
				:node(lang.wrap(line_original, llang))
				:done()
		if hastran then
			local line_translation = tran[i] or ''
			line:tag('div')
				:addClass('Lyrics-translated')
				:cssText(rstyle)
				:node(lang.wrap(line_translation, rlang))
				:done()
			if (hover and line_original == '' and line_translation == '') or string.find(line_translation, "#NoHover") then
				line:addClass('Lyrics-line-nohover')
			end
		end
		table.insert(lines, tostring(line))
	end

	if hookTrigger then
		hookTrigger('postParse', lines, customArgs)
	end
	html:node(table.concat(lines))
	-- Clear the floating
	html:tag('div'):cssText('clear:both')
	html = tostring(html)

	if hookTrigger then
		html = hookTrigger('preOutput', html, customArgs)
	end

	local copyright = ''
	local frame = mw.getCurrentFrame()
	local LDC = frame:callParserFunction{ name = '#var', args = { 'LDC', '0' } }
	if LDC ~= '1' and args.override ~= '1' then
		copyright = '<small>The copyright of the lyrics displayed here belongs to ' .. (args.author or 'its copyright holders') .. '.</small>'
	end
	local css = frame:extensionTag{ name = 'templatestyles', args = { src = 'Template:Lyrics/styles.css' } }

	return romaji_toggle .. copyright .. css .. html
end

function p.lyrics(frame)
	local args = getArgs(frame, {wrappers='Template:Lyrics'})
	return p._lyrics(args)
end

return p