Documentation for this module may be created at ሞድዩል:Text format/doc

local z = {}

function z.inItalics(text, ref)
	if ref and text then
		return '<i>' .. text .. '</i>' .. ref
	elseif text then
	    return '<i>' .. text .. '</i>'
	end
end

-- Adapted from [[:en:Module:String2]], function sentence
function z.inUppercase(text)
	if not text or text == '' then
		return text
	end
	
	-- [[hypothesis (scientific method)|hypothesis]]
	
	if text:find("^%[%[[^|]+|[^%]]+%]%]") then
		local b, c = text:find("|%A*%a") 
		return string.sub(text, 1, c-1) .. string.upper(string.sub(text, c, c)) .. string.sub(text, c+1)
	end
	
	local letterpos = text:find('%a')
	if letterpos then
		local first = text:sub(1, letterpos - 1)
		local letter = text:sub(letterpos, letterpos)
		local rest = text:sub(letterpos + 1)
		return first .. string.upper(letter) .. rest
	else
		return text
	end
end

function z.inSmallCaps(text)
	if not text or text == '' then
		return text
	end	
	return '<span style="font-variant:small-caps">' .. text .. '</span>'
end

function z.inMultipleLines(list)
	local result
	local copy={}
	require('Module:Tables').insertElementsWithValue(list, copy)
	
	-- Only return something if there is at least one element
	if copy[1] then
		return table.concat(copy, '<br/>')
	end
end

function z.betweenQuotationMarks(text, ref)
	if not text or text == '' then
		return text
	elseif ref and ref ~='' then
		return '«' .. text .. '»' .. ref
	else 
	    return '«' .. text .. '»'
	end
end

function z.inBrackets(text, qualifying)
	if not text or text == '' then
		return
	elseif qualifying and qualifying ~= '' then
	    return text .. ' (' .. qualifying .. ')'
	else
		return text
	end
end

function z.separatedByComma(list)
	local copy={}
	require('Module:Tables').insertElementsWithValue(list, copy)
	
	return table.concat(copy, '፣ ')
end

-- :: Generate a link
function z.toLink(link, text, qualifying)
	local result
	
	if link and text then
		result = '[[' .. link .. '|' .. text .. ']]'
	elseif link then
		result = '[[' .. link .. ']]'
	else
		result = text
	end
	
	if result and qualifying then
		return result .. ' <small>(' .. qualifying .. ')</small>'
	else
		return result
	end
end

-- Generate a link (extended deployment)
function z.toLink2(link, options)
	if link then
		local label, namespace, size, border, link2, imageFooter, mustExist, onlytext

		if type(options) == 'table' then
			label	= options['label'] or ''
			namespace	= tonumber(options['namespace']) or 0
			size		= options['size'] or '250px'
			border		= options['border']
			link2		= options['link']
			imageFooter	= options['footer']
			mustExist	= options['must exist'] or options['mustExist'] or options['mustexist']
			onlytext	= options['onlytext']
		elseif type(options) == 'string' then
			label = options
		elseif type(options) == 'number' then
			namespace = options
		else
			label = link
			namespace = 0
		end

		local titleObj = mw.title.new(link, namespace) or {}

		local title2Obj = {}
		if link2 then title2Obj= mw.title.new(link2, 0) end

		local result = {}

		table.insert(result, titleObj.fullText)

		if not mustExist and namespace ~= 6 and namespace ~= 14 then
			if label then table.insert(result, label) end
			if qualifier then return '[[' .. prefix .. table.concat(result, '|') .. ']]' .. ' <small>(' .. qualifier .. ')</small>' end
			return '[[' .. table.concat(result, '|') .. ']]'
		end

		if titleObj.exists or titleObj.fileExists then
			-- File:
			if namespace == 6 then
				if onlytext then
					if label then table.insert(result, label) end
					return '[[:' .. table.concat(result, '|') .. ']]'
				end
				
				if size then table.insert(result, size) end
				if border then table.insert(result, 'border') end
				if title2Obj.exists then table.insert(result, 'link=' .. title2Obj.fullText) end
				if label then table.insert(result, label) end
				if imageFooter then return '[[' .. table.concat(result, '|') .. ']]' .. '<br>' .. imageFooter end
				return '[[' .. table.concat(result, '|') .. ']]'
			end
			
			-- Category:
			if namespace == 14 then
				if onlytext then
					if label then table.insert(result, label) end
					return '[[:' .. table.concat(result, '|') .. ']]'
				end
				return '[[' .. titleObj.fullText .. ']]'
			end

			-- The rest
			if label then table.insert(result, label) end
			return '[[' .. table.concat(result, '|') .. ']]'

		end

		return label
	end
end

function z.callFromATemplate(frame)
	local args = frame.args
	
	if args['argument type'] == 'table' then
		local board = {args[2], args[3], args[4]}
		return z[args[1]](board)
	else
		return z[args[1]](args[2], args[3], args[4])
	end
end

return z