Module:Card list

From Coral Island Wiki
Revision as of 03:21, 18 January 2024 by Salty Nori (talk | contribs) (add mini)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

local p = {}
local lib = require('Module:Feature')
local Card = require('Module:Card')._main
local Ilib = require('Module:Icon list')

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		removeBlanks = false,
		wrapper = { 'Template:Card list' }
	})
	return p._main(args)
end

function p._main(args)
	local input = args[1] or ''
	local itemDelim = args.delim or ';'
	local items = lib.split(input, itemDelim)
	if next(items) == nil then
		return mw.html.create()
	else
		return p.buildList(items, args)
	end
end


function p.buildList(items, args)
	local countDelim = args.amount_delim or '*'
	local qualityDelim = args.quality_delim or '/'
	local paramDelim = args.param_delim or '$'
	local noteDelim = args.note_delim or '»'
	
	-- put all text after the first noteDelim into the second capture
	local notePattern
	if lib.isNotEmpty(noteDelim) then
		notePattern = '^(.-)' .. noteDelim .. '(.*)$'
	end
	
	-- list container
	local container = mw.html.create():tag('div'):addClass('card-list-container')
	if args.cols and tonumber(args.cols) ~= nil then
		container:addClass('columntemplate'):css{ ['-moz-column-count'] = args.cols, ['-webkit-column-count'] = args.cols, ['column-count'] = args.cols }
	end
	
	-- card list
	for i, str in ipairs(items) do
		if not lib.isEmpty(str) then
			local entry, cardArgs = Ilib.splitParams(str, paramDelim) --check for entry-specific params
			
			local pre, note = Ilib.splitNote(entry, notePattern) --check for entry-specific note
			
			local pre_parts = lib.split(pre, qualityDelim) --check for entry-specific note
			local item = pre_parts[1]
			local quality = pre_parts[2]
			
			local item_parts = lib.split(item, countDelim) --check for entry-specific amount
			local name = item_parts[1]
			local amount = item_parts[2]
			
			--set card arguments without replacing those set by entry-specific params
			cardArgs.name			= cardArgs[1]				or cardArgs.name		or name
			cardArgs.amount			= cardArgs.amount			or cardArgs.x			or cardArgs[2]			or amount or args.amount or args.x or args[2]
			cardArgs.note			= cardArgs.note				or note					or args.note
			cardArgs.q				= cardArgs.quality			or quality				or args.quality
			cardArgs.text			= cardArgs.text				or args.text
			cardArgs.link			= cardArgs.link				or args.link
			cardArgs.ext			= cardArgs.ext				or args.ext
			cardArgs.nolink			= cardArgs.nolink			or args.nolink
			cardArgs.prefix			= cardArgs.prefix			or args.prefix
			cardArgs.suffix			= cardArgs.suffix			or args.suffix
			cardArgs.show_caption	= cardArgs.show_caption		or args.show_caption
			cardArgs.mini			= cardArgs.mini				or args.mini
			
			container:node(Card(cardArgs))
		end
	end
	return container
end
	
return p