Pergi ke kandungan

Modul:Tag

Daripada Wikisumber
require('strict')

local p = {} --p stands for package

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local make_style_string = require('Module:Optional style').make_style_string

function p._tag(args)
	local tag = args[1] or args.tag or 'tag'
	
	local pair_aliases = {
		['close']	= 'close',
		['c']		= 'close',
		
		['empty']	= 'empty',
		['e']		= 'empty',
		['s']		= 'empty',
		['single']	= 'empty',
		['v']		= 'empty',
		['void']	= 'empty',
		
		['open']	= 'open',
		['o']		= 'open',
		
		['pair']	= 'pair',
		['p']		= 'pair'
	}
	local pair = pair_aliases[args[2] or args['pair']] or 'pair'
	
	local wrapcode = yesno(args['wrap']) or false
	local style = args.style
	local attribs = args.attribs or args.params or ''
	local content = args.content
	
	-- Code tag
	local code_class
	if wrapcode then
		code_class = 'class="wrap"'
	else
		code_class = 'class="nowrap"'
	end
	local code_style
	if style == 'plain' then
		code_style = make_style_string({['border'] = 'none', ['background'] = 'transparent'})
	else
		code_style = make_style_string({['style'] = style})
	end
	
	-- Opening tag
	local tag_open
	if pair == 'close' then
		tag_open = ''
	elseif tag == '!--' then
		tag_open = '<!--'
		if pair == 'empty' then
			tag_open = tag_open .. ' -->'
		end
	else
		tag_open = mw.text.trim('<' .. tag .. ' ' .. attribs)
		if pair == 'empty' then
			tag_open = tag_open .. ' />'
		else
			tag_open = tag_open .. '>'
		end
	end
	
	-- Content between tags
	local tag_content
	if pair == 'empty' then
		tag_content = ''
	elseif pair == 'pair' then
		tag_content = content or '...'
	else
		tag_content = content or ''
	end
	
	-- Closing tag
	local tag_close
	if pair == 'empty' or pair == 'open' then
		tag_close = ''
	elseif tag == '!--' then
		tag_close = '-->'
	else
		tag_close = '</' .. tag .. '>'
	end
	
	return '<code ' .. mw.text.trim(code_class .. ' ' .. code_style) .. '>'
		.. mw.text.encode(tag_open) .. tag_content .. mw.text.encode(tag_close)
		.. '</code>'
end

function p.tag(frame)
	return p._tag(getArgs(frame))
end

return p