Module:Icon list

From Coral Island Wiki
Jump to navigation Jump to search

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

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

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		removeBlanks = false,
		wrapper = { 'Template:Icon 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.splitNote(entry, notePattern)
	if notePattern then
		item, note = entry:match(notePattern)
		if item == nil then -- will be nil if note is not present
			return entry
		end
		return item, note
	end
	return entry
end

function p.splitParams(entry, paramDelim)
	if entry:find('{.-}') then
		local params = string.match(entry, '{(.-)}')
		entry = entry:gsub('{.-}', '')
		params = lib.split(params, paramDelim)
		local returns = {}
		for i, param in ipairs(params) do
			local name, val = string.match(param, '^%s*(.-)%s*=%s*(.-)%s*$')
			if name ~= nil and name ~= '' and val ~= nil then --named params
				returns[name] = val
			elseif param ~= nil and param ~= '' then --unnamed params
				table.insert(returns, param)
			end
		end
		return entry, returns
	end
	return entry, {}
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('span')
	if lib.isNotEmpty(args.cols) and tonumber(args.cols) ~= nil then
		container = mw.html.create():tag('div'):addClass('columntemplate'):css{ ['-moz-column-count'] = args.cols, ['-webkit-column-count'] = args.cols, ['column-count'] = args.cols }
	end
	container:addClass('icon-list-container')
	
	-- icon list
	local list = container:tag('span')
	if lib.isNotEmpty(args.bullet_list) then
		list = container:tag('ul')
	elseif lib.isNotEmpty(args.inline) then
		list:addClass('icon-list-inline')
	elseif lib.isNotEmpty(args.inline_list) then
		list:addClass('icon-list-inline-sentence')
	else
		list:addClass('icon-list')
	end
	
	for i, str in ipairs(items) do
		if not lib.isEmpty(str) then
			local ret = list
			if lib.isNotEmpty(args.bullet_list) then ret = list:tag('li') end
			for num, entry in ipairs(lib.split(str, '//')) do
				local entry, iconArgs = p.splitParams(entry, paramDelim) --check for entry-specific params
				
				local pre, note = p.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 icon arguments without replacing those set by entry-specific params
				iconArgs.name			= iconArgs[1]				or iconArgs.name		or name
				iconArgs.amount			= iconArgs.amount			or iconArgs[2]			or amount or args.amount or args[2]
				iconArgs.note			= iconArgs.note				or note
				iconArgs.q				= iconArgs.quality			or quality				or args.quality
				iconArgs.text			= iconArgs.text				or args.text
				iconArgs.size			= iconArgs.size				or args.size
				iconArgs.link			= iconArgs.link				or args.link
				iconArgs.ext			= iconArgs.ext				or args.ext
				iconArgs.notext			= iconArgs.notext			or args.notext
				iconArgs.nolink			= iconArgs.nolink			or args.nolink
				iconArgs.prefix			= iconArgs.prefix			or args.prefix
				iconArgs.suffix			= iconArgs.suffix			or args.suffix
				
				-- mw.logObject(iconArgs)
				if num > 1 then ret:tag('i'):wikitext(' or ') end
				ret:node(Icon(iconArgs))
			end
		end
	end
	return container
end
	
return p