Перейти к содержанию

Модуль:СпрайтФайл

Материал из LemonCraft Wiki

Для документации этого модуля может быть создана страница Модуль:СпрайтФайл/doc

local p = {}

local i18n = {
	modArgs = [[Модуль:ProcessArgs]],
	modRevLink = [[Модуль:Обратная ссылка]],
}

function p.sprite(f)
	local args = f
	if f == mw.getCurrentFrame() then
		args = require(i18n.modArgs).merge(true)
	else
		f = mw.getCurrentFrame()
	end
	
	-- Категория
	local category
	
	-- Настройки по умолчанию
	local default = {
		['масштаб'] = 1,
		['разм'] = 16,
		['выравн'] = 'text-top'
	}
	
	-- Название спрайта
	local id = args[1] or ''
	if id ~= '' and not args['как есть'] then
		id = mw.ustring.lower(id):gsub('[_%s%+]', '-')
	end
	
	-- Ссылка
	local link = args['ссылка'] or ''
	if mw.ustring.lower(link) == 'нет' then
		link = ''
	elseif link ~= '' then
		local linkPrefix = (not link:find( '//' ) and args['предссылки']) or ''
		if linkPrefix ~= '' and mw.ustring.lower(linkPrefix) ~= 'нет' then
			link = linkPrefix .. link
		end
	end
	
	-- Размер спрайта
	local scale = args['масштаб'] or default['масштаб']
	local height = (args['высота'] or args['верт_разм'] or args['разм'] or default['разм']) * scale
	local width = (args['ширина'] or args['разм'] or default['разм']) * scale
	local size = width .. 'x' .. height .. 'px'
	
	local styles = {}
	if height ~= default['разм'] then
		styles[#styles + 1] = 'height:' .. height .. 'px'
	end
	if width ~= default['разм'] then
		styles[#styles + 1] = 'width:' .. width .. 'px'
	end
	
	-- Путь к файлу
	local name = args['имя']
	local file
	if not name or id == '' then
		file = 'Grid Unknown.png'
	elseif args['общий'] then
		file = require(i18n.modRevLink).reverseLink{id, 'Спрайт/' .. name}
		if not file then
			category = '[[Категория:Страницы с отсутствующими спрайтами]]'
			file = 'Grid Unknown.png'
		end
	else
		file = 'Спрайт-' .. name .. ' ' .. id .. '.png'
	end
	
	-- Построение спрайта
	local sprite = mw.html.create('span'):addClass('sprite')
	local img = '[[Файл:' .. file .. '|' .. size .. '|link=' .. link .. '|' .. (args['назв'] or '') .. ']]'
	sprite:node( img )
	
	-- стили
	local align = args['выравн'] or default['выравн']
	if align ~= default['выравн'] then
		styles[#styles + 1] = '--sprite-vertical-align:' .. align
	end
	styles[#styles + 1] = args['css']
	sprite:cssText(table.concat(styles, ';'))
	
	local root
	local spriteText
	if args['текст'] then
		if not args['перенос'] then
			root = mw.html.create('span'):addClass('nowrap')
		end
		spriteText = mw.html.create('span'):addClass('sprite-text'):wikitext(args['текст'])
		if args['назв'] then
			spriteText:attr('title', args['назв'])
		end
		if link ~= '' then
			-- Внешняя ссылка
			if link:find('//') then
				spriteText = '[' .. link .. ' ' .. tostring(spriteText) .. ']'
			else
				spriteText = '[[' .. link .. '|' .. tostring(spriteText) .. ']]'
			end
		end
	end
	
	if not root then
		root = mw.html.create('')
	end
	root:node( sprite )
	if spriteText then
		root:node(spriteText)
	end
	
	return tostring(root), category
end

function p.link(f)
	local args = f
	if f == mw.getCurrentFrame() then
		args = require(i18n.modArgs).merge(true)
	end
	
	local link = args[1]
	if args[1] and not args['ID'] then
		link = args[1]:match('^(.-)%+') or args[1]
	end
	local text
	if not args['безтекста'] then
		text = args['текст'] or args[2] or link
	end
	
	args[1] = args['ID'] or args[1]
	args['ссылка'] = args['ссылка'] or link
	args['текст'] = text
	
	return p.sprite(args)
end

return p