Module:Delimited tag

From Modded Wiki
Revision as of 03:30, 5 December 2020 by minecraft>Nixinova (Create module version of Template:Code)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

function p.output(f)

	local args = f
	if f == mw.getCurrentFrame() then 
		args = require('Module:ProcessArgs').merge(true)
	end

	local tag = function(txt)
		return '<' .. args['tag'] .. '>' .. txt .. '</' .. args['tag'] .. '>'
	end
	local listLevel = args['list-level'] or '**'

	local output = tag(args[1])
	
	local finalDelimiterValues = {
		['none'] = '<nowiki/>',
		[','] = ',&#32;',
		['and'] = '&#32;and&#32;',
		['&'] = '&nbsp;&amp;&nbsp;', ['amp'] = '&nbsp;&amp;&nbsp;',
		['or'] = '&#32;or&nbsp;',
		['to'] = '&#32;to&nbsp;',
		['br'] = '<br/>', ['newline'] = '<br/>',
		['list'] = '<br/>' .. listLevel, ['bullet'] = '<br/>' .. listLevel, ['*'] = '<br/>' .. listLevel,
		['and-list'] = '&#32;and<br/>' .. listLevel,
		['or-list'] = '&#32;or<br/>' .. listLevel,
		['&-list'] = '&#32;&<br/>' .. listLevel, ['amp-list'] = '&#32;&<br/>' .. listLevel,
		['comma-list'] = ',<br/>' .. listLevel,
	}
	
	local midDelimiterValues = {
		['and'] = '&#32;', ['&'] = '&#32;', ['amp'] = '&#32;',
		['and-list'] = ',<br>\n' .. listLevel, ['&-list'] = ',<br>\n' .. listLevel,
	}

	local delimitersWithSerial = {
		['and'] = true,
		['or'] = true,
		['and-list'] = true,
		['&-list'] = true,
		['or-list'] = true
	}
	
	local delimiter = args['delimiter'] or args['delim'] or args['d'] or '&nbsp;'
	if finalDelimiterValues[delimiter] then delimiter = finalDelimiterValues[delimiter] end
	local midDelimiter = midDelimiterValues[delimiter] or delimiter
	local serialDelimiter = delimitersWithSerial[delimiter] and ',' or ''
	
	for i,unit in ipairs(args) do
		local currentDelimiter = args[i+1] and midDelimiter or (serialDelimiter .. delimiter)
		output = output .. currentDelimiter .. tag(args[i])
	end
	
end

return p