FANDOM

!
21 Pages

--[==[ config ]==]
 
local debug = true
 
local config = {}
 
config['viewCount'] = {
	nnHall = false,
	tenThousandAbbrev = false, -- false, 'M', 'W'
	cumulativeCategories = false,
}
 
config['songBox'] = {
	songBoxLineHeight = '22px',
	songBoxImageSize = '256px',
}
 
--[==[ global variables ]==]
 
local export = {}
 
local pagename = mw.title.getCurrentTitle().text
 
local lang = mw.language.getContentLanguage()
 
local externalData = require('Module:Song/externalData')
 
local gsub = mw.ustring.gsub
local floor = math.floor
local match = mw.ustring.match
 
local st = {
	['$viewcount+ $serviceName views'] = '%s+ %s views',
	['$serviceName logo'] = '%s logo',
 
	['Image of "$songName"'] = 'Image of "%s"',
	['"$songName"'] = '"%s"',
	['Song title'] = 'Song title',
	['Uploaded $date'] = 'Uploaded %s',
	['Uploaded $date, with $views'] = 'Uploaded %s, with %s',
	['Featuring'] = 'Featuring',
	['Producers'] = 'Producers',
	['Links'] = 'Links',
 
	['LinkCommentLeadingSpace'] = ' ',
	['($)'] = '(%s)',
}
 
st['Song title'] = '歌曲名称'
st['Uploaded $date'] = '于%s投稿'
st['Uploaded $date, with $views'] = '于%s投稿,再生数为 %s'
st['Featuring'] = '演唱'
st['Producers'] = 'P主'
st['Links'] = '链接'
 
--[==[ local functions ]==]
 
local function cat(catName, catSortKey)
	return '[[' .. (debug and ':' or '') .. 'Category:' .. catname .. '|' .. catSortKey.. ']]'
end
 
--[==[ export functions ]==]
 
function export.viewCount(args)
	local ret = {}
 
	local serviceId = args[1]                          -- id  = 'yt'
	local viewCountStr = gsub(args[2], '[ .,]', '')    -- str = '39390'
	local viewCountLen = #(viewCountStr)               -- len = 5
	local viewCount = tonumber(viewCountStr)           -- vc  = 39390
 
	local serviceName = export.external({ serviceId = serviceId, serviceProperty = 'name' })
 
	local function isApproaching(milestone, viewCount)                 -- milestone = 10,000     10 ^ (4 - 1)
		local maximum = milestone - 1                              -- maximum = 9,999       /
		local minimum = maximum - 10 ^ (#tostring(maximum) - 1)    -- minimum = 9,999 - 1,000 = 8,999
		return (viewCount >= minimum) and (viewCount <= maximum)
	end
 
	local function abbrevNumber(viewCount)
		local ret = {}
	end
 
	if (viewCountLen > 2+3+3) then
		return error('A view count this fantastic is not supported yet: 【' .. viewCount .. '】')
	end
 
	if (viewCountLen <= 1) then
		viewCount = viewCount                     --          .
	elseif (viewCountLen <= 3) then
		viewCount = floor(viewCount/10)*10        --        ?.0
	elseif (viewCountLen <= 3+3) then
		viewCount = floor(viewCount/100)*100      --    ??. .00
	elseif (viewCountLen <= 2+3+3) then
		viewCount = floor(viewCount/1000)*1000    -- ?. ... 000    where . is any number and ? is 0 to 1 numbers
	end
 
	table.insert(ret, string.format(st['$viewcount+ $serviceName views'], lang:formatNum(viewCount), serviceName))
 
	if isApproaching(10000, viewCount) then
		table.insert(ret, '')
	elseif (viewCount >= 10000) and (viewCount <= 99999) then
		table.insert(ret, '')
	end
 
	return table.concat(ret)
end
 
function export.external(args)
	local ret
 
	ret = externalData[args.serviceId]
 
	if args.serviceProperty == 'name' then
		return ret[args.serviceProperty]
	end
 
	if args.serviceId == '-' then
		ret['name'] = args.name
		ret['url'] = args.mediaId
	end
 
	for property, value in pairs(ret) do
		if args.serviceId == '-' and type(value) == 'function' then
			value = value(args.mediaId)
		elseif type(value) == 'function' then
			value = value(args.mediaId)
		elseif type(value) == 'string' and args.mediaId then
			value = gsub(value, '@mediaId@', args.mediaId)
		end
 
		if match(property, '^img') then
			if args.thumb then value = 'File:' .. args.thumb end
 
			if match(value, '^File:') then
				value = '[[' .. value ..
					'|link=' .. 
					'|alt=' .. string.format(st['$serviceName logo'], ret['name']) ..
					'|' .. ret['name'] ..
				']]'
			end
			value = mw.text.tag('span', { class = 'external-thumb' }, value)
		end
 
		ret[property] = value
	end
 
	ret.ico = mw.text.tag(
		'span',
		{ class = 'external-icon' },
		'[[File:Favicon-' .. (ret.ico and args.serviceId or 'etc') .. '.png' ..
			'|link=' .. ret.url .. 
			'|alt=[' .. args.serviceId .. ']' .. 
			(args.mediaComment and '|' .. args.mediaComment or '') ..
		']]'
	)
 
	if args.serviceProperty then
		return ret[args.serviceProperty]
	end
 
	return ret
end
 
function export.linkLong(args)
	local data = export.external({ serviceId = args.serviceId, mediaId = args.mediaId, name = args.name, mediaComment = args.mediaComment })
	if args.mediaComment then args.mediaComment = mw.text.tag('small', nil, string.format(st['($)'], args.mediaComment)) end
 
	return data.ico .. ' [' .. data.url .. ' '.. data.name .. ']' .. st['LinkCommentLeadingSpace'] .. (args.mediaComment and args.mediaComment or '')
end
 
function export.linkIco(args)
end
 
function export.songBox(args)
	local songBox = mw.html.create('div')
		:addClass('song-box')
		:css('line-height', config.songBox.songBoxLineHeight)
 
	local divTypeClasses = {
		[1] = 'song-box-row1',
		[2] = 'song-box-row2',
		[0] = 'no-bullets',
		[3] = 'song-box-edit hidden',
	}
 
	function div(type, content, styles)
		local div = mw.html.create('div'):addClass(divTypeClasses[type])
 
		if type == 0 then content = '\n* ' .. content end
 
		if styles then div:css(styles) end
 
		if type == 1 or type == 2 then
			div:tag('b'):wikitext(content)
		else
			div:wikitext(content)
		end
 
		songBox
			:node(div)
			:wikitext("\n") -- solution for weird problem with <ul> not enclosing <li>s. raw html line is too long?
	end
 
	if args.image then
		songBox
			:tag('div')
				:addClass('song-box-image')
				:wikitext(
					'[[File:' .. args.image ..
						'|' .. config.songBox.songBoxImageSize ..
						'|center' ..
						'|border' ..
						'|' .. string.format(st['Image of "$songName"'], pagename) ..
						'|' .. string.format(st['"$songName"'], pagename) ..
					']]'
				)
	end
 
	div(1, st['Song title'])
	div(0, args.title)
 
	if (args.date and args.date ~= 'none') then
		local styles = { ['font-size'] = '80%', ['line-height'] = '1.75', ['padding'] = '3px' }
 
		if (args.views and args.views ~= 'none') then
			div(1, string.format(st['Uploaded $date, with $views'], args.date, args.views), styles)
		else
			div(1, string.format(st['Uploaded $date'], args.date), styles)
		end
	end
 
	div(1, st['Featuring'])
	div(0, args.singers)
 
	div(1, st['Producers'])
	div(0, args.producers)
 
	div(1, st['Links'])
	div(0, args.links)
 
	div(3, mw.getCurrentFrame():expandTemplate({ title = 'editsection', args = { 0 } }), { ['line-height'] = 1 })
 
	return tostring(songBox)
end
 
--[==[ export functions as templates ]==]
 
function export.tViewCount(frame)
	return export.viewCount(frame:getParent().args)
end
 
function export.tExternal(frame)
	local args = frame:getParent().args
	if args[1] == '-' then
		return export.external({
			serviceId = args[1],
			serviceProperty = args[2],
			mediaId = args[3],
			name = args[4],
			mediaComment = args[5],
			thumb = args['thumb'],
		})
	else
		return export.external({
			serviceId = args[1],
			serviceProperty = args[2],
			mediaId = args[3],
			mediaComment = args[4],
			thumb = args['thumb'],
		})
	end
end
 
function export.tLinkLong(frame)
	local args = frame:getParent().args
	if args[1] == '-' then
		return export.linkLong({
			serviceId = args[1],
			mediaId = args[2],
			name = args[3],
			mediaComment = args[4],
		})
	else
		return export.linkLong({
			serviceId = args[1],
			mediaId = args[2],
			mediaComment = args[3],
		})
	end
end
 
function export.tLinkIco(frame)
	return export.linkIco(frame:getParent().args)
end
 
function export.tSongBox(frame)
	local args = {}
	for k, v in pairs(frame:getParent().args) do
		if v ~= '' then
			args[k] = v
		end
	end
	if false then
		return require('module:debug').dump(args)
	elseif true then
		return '<pre>'..require('module:debug').dump(mw.text.nowiki(export.songBox(args)))..'</pre>'..export.songBox(args)
	end
	return export.songBox(args)
end
 
return export