Moduł:Inventory slot: Różnice pomiędzy wersjami

Z Minecraft Wiki Polska
Przejdź do nawigacji Przejdź do wyszukiwania
[wersja nieprzejrzana][wersja nieprzejrzana]
Ivan-r (dyskusja | edycje)
dNie podano opisu zmian
Ivan-r (dyskusja | edycje)
d Revert consecutive edits by Ivan r (talk)
Linia 1: Linia 1:
-------------------------------------------------------------------
--- Модуль для отображения инвентарных слотов в Minecraft Wiki.
-------------------------------------------------------------------
local p = {}
local p = {}


-- Список приставок к названиям, обрабатываемых другими модулями.
-- List of special prefixes which should be handled by
-- Так будет легче, например, убирать их из целей ссылок.
-- other modules (such as being moved outside links)
-- ВНИМАНИЕ: указывайте все варианты склонения по родам и числам.
p.prefixes = {
p.prefixes = {
'Jakiekolwiek', 'Jakikolwiek', 'Jakakolwiek',
'Any',
'Uszkodzone', 'Uszkodzony', 'Uszkodzona'
'Matching',
'Damaged'
}
}


p.modAliases = {
function p.slot( f )
["thaum"] = "ThaumCraft",
["bc"] = "BuildCraft"
}
 
--- Создание слота
function p.slot(f)
--- Получение аргументов
local args = f.args or f
local args = f.args or f
if f == mw.getCurrentFrame() and args[1] == nil then
if f == mw.getCurrentFrame() and args[1] == nil then
Linia 26: Linia 15:
end
end
-- Первый аргумент
args[1] = mw.text.trim( args[1] or '' )
args[1] = mw.text.trim(args[1] or '')
--- Псевдонимы
-- Comment this next line out if you're not using aliases
-- Вы можете закомментировать следующую строку, если не используете псевдонимы
local aliases = mw.loadData( 'Module:Inventory slot/Aliases' )
-- local aliases = mw.loadData('Модуль:Инвентарный слот/Псевдонимы')
local modAliases = args["aliasyModow"] or ''
local modAliases = args.modaliases or ''
if modAliases ~= '' then
if modAliases ~= '' then
modAliases = mw.loadData('Moduł:' .. modAliases)
modAliases = mw.loadData( 'Module:' .. modAliases )
else
else
modAliases = nil
modAliases = nil
Linia 43: Linia 30:
local frames = {}
local frames = {}
for frame in mw.text.gsplit( args[1], '%s*;%s*' ) do
for frame in mw.text.gsplit( args[1], '%s*;%s*' ) do
local frameParts = p.getParts( frame, args["mod"] )
local frameParts = p.getParts( frame, args.mod )
local id = frameParts.name
local id = frameParts.name
Linia 67: Linia 54:
end
end
--- Построение спрайта
-- Параметры
local sprite
local sprite
local ids = mw.loadData([[Moduł:InvSprite/IDs]])["IDs"]
local ids = mw.loadData( [[Module:InvSprite/IDs]] ).ids
local modIds = {}
local modIds = {}
local back_modIds = {}
local animated = args[1]:find( ';' )
local animated = mw.ustring.find(args[1], ';')
local pageName = mw.title.getCurrentTitle().text
local pageName = mw.title.getCurrentTitle().text
local imgClass = args["klasa obrazu"]
local imgClass = args.imgclass
local numStyle = args["styl numerów"]
local numStyle = args.numstyle
local body = mw.html.create( 'span' ):addClass( 'invslot' ):css{ ['vertical-align'] = args.align }
local body = mw.html.create('span'):css{['vertical-align'] = args["wyrówn"]}
if animated then
if animated then
body:addClass('animated')
body:addClass( 'animated' )
end
end
if args["класс"] then
if args.class then
body:addClass(args["klasa"])
body:addClass( args.class )
end
end
body:addClass('invslot')
if args.style then
if args["стиль"] then
body:cssText( args.style )
body:cssText(args["styl"])
end
end
if (args["domyślne"] or '') ~= '' then
if ( args.default or '' ) ~= '' then
body:css('background-image', '{{FileUrl|' .. args["domyślne"] .. '.png}}')
body:css( 'background-image', '{{FileUrl|' .. args.default .. '.png}}' )
end
 
    ---спрайты для фона
if (args["domyślneCSS"] or '') ~= '' then
        local mod=args["mod"]
if mod then
local back_modData = back_modIds[mod]
if not back_modData and mw.title.new('Moduł:TłoSprit/' .. mod .. '/ID').exists then
back_modData = mw.loadData('Moduł:TłoSprit/' .. mod .. '/ID')
back_modIds[mod] = back_modData
end
if back_modData then
param = mw.loadData('Moduł:TłoSprit/' .. mod)
local n = args["domyślneCSS"]
local pos = back_modData[n] - 1
local size = param["wielkość"]
local tiles = param["format"] / size
local left = pos % tiles * size
local top = math.floor( pos / tiles ) * size
body:css('background-image', '{{FileUrl|' .. mod .. 'CSS.png}}')
body:css('background-size', 'auto')
body:css('background-position', '-' .. left .. 'px -' .. top ..'px')
end
end
end
end
--- Обработка фреймов
local first = true
local first = true
for frame in mw.text.gsplit(args[1], '%s*;%s*') do
for frame in mw.text.gsplit( args[1], '%s*;%s*' ) do
local item
local item
if frame ~= '' or frame == '' and animated then
if frame ~= '' or frame == '' and animated then
item = body:tag('span'):addClass('invslot-item')
item = body:tag( 'span' ):addClass( 'invslot-item' )
if imgClass then
if imgClass then
item:addClass(imgClass)
item:addClass( imgClass )
end
end
end
end
if frame == '' then
if frame == '' then
(item or body):tag('br')
( item or body ):tag( 'br' )
else
else
local category
local category
local parts = p.getParts(frame, args["mod"])
local parts = p.getParts( frame, args.mod )
local title = parts.title or mw.text.trim(args["tytuł"] or '')
local title = parts.title or mw.text.trim( args.title or '' )
local mod = parts.mod
local mod = parts.mod
local name = parts.name
local name = parts.name
Linia 146: Linia 100:
if mod then
if mod then
local modData = modIds[mod]
local modData = modIds[mod]
if not modData and mw.title.new('Moduł:InvSprite/' .. mod .. '/ID').exists then
if not modData and mw.title.new( 'Module:InvSprite/Mods/' .. mod .. '/IDs' ).exists then
modData = mw.loadData('Moduł:InvSprite/' .. mod .. '/ID')["IDы"]
modData = mw.loadData( 'Module:InvSprite/Mods/' .. mod .. '/IDs' )
modIds[mod] = modData
modIds[mod] = modData
end
end
Linia 161: Linia 115:
end
end
local link = args["link"] or ''
local link = args.link or ''
if link == '' then
if link == '' then
if mod then
if mod then
link = mod .. '/' .. name
link = 'Mods/' .. mod .. '/' .. name
else
else
link = mw.ustring.gsub(name, '^Uszkodzon[yea] ', '')
link = name:gsub( '^Damaged ', '' )
end
end
elseif mw.ustring.lower(link) == 'nie' then
elseif link:lower() == 'none' then
link = nil
link = nil
end
end
Linia 179: Linia 133:
if title == '' then
if title == '' then
plainTitle = name
plainTitle = name
elseif mw.ustring.lower(title) ~= 'nie' then
elseif title:lower() ~= 'none' then
plainTitle = mw.ustring.gsub(mw.ustring.gsub(title, '\\\\', '\'), '\\&', '&')
plainTitle = title:gsub( '\\\\', '\' ):gsub( '\\&', '&' )
local formatPattern = '&[0-9a-fk-or]'
local formatPattern = '&[0-9a-fk-or]'
if mw.ustring.match(plainTitle, formatPattern) then
if plainTitle:match( formatPattern ) then
formattedTitle = title
formattedTitle = title
plainTitle = mw.ustring.gsub(plainTitle, formatPattern, '')
plainTitle = plainTitle:gsub( formatPattern, '' )
end
end
Linia 191: Linia 145:
plainTitle = name
plainTitle = name
else
else
plainTitle = mw.ustring.gsub(mw.ustring.gsub(plainTitle, '\', '\\'), '&', '&')
plainTitle = plainTitle:gsub( '\', '\\' ):gsub( '&', '&' )
end
end
elseif link then
elseif link then
Linia 210: Linia 164:
-- as plain text, but MediaWiki doesn't
-- as plain text, but MediaWiki doesn't
local escapedTitle = ( plainTitle or '' ):gsub( '&', '&' )
local escapedTitle = ( plainTitle or '' ):gsub( '&', '&' )
item:addClass('invslot-item-image')
item:addClass( 'invslot-item-image' )
:wikitext('[[Файл:Grid ', img, '.png|32x32px|link=', link or '', '|', escapedTitle, ']]')
:wikitext( '[[File:Grid ', img, '.png|32x32px|link=', link or '', '|', escapedTitle, ']]' )
else
else
if not sprite then
if not sprite then
sprite = require([[Moduł:Sprite]]).sprite
sprite = require( [[Module:Sprite]] ).sprite
end
end
local image
local image
if mod then
if mod then
image = (args["tablica Ikonek"] or mod or "Inv") .. 'CSS.png'
image = args.spritesheet or mod .. 'Sprite.png'
end
end
if link then
if link then
item:wikitext('[[', link, '|')
item:wikitext( '[[', link, '|' )
end
end
 
local image, spriteCat = sprite{
local image, spriteCat;
iddata = idData, title = plainTitle,
if not mod then
image = image, settings = 'InvSprite'
    image, spriteCat = sprite{
}
    ["danychID"] = idData, ["tytuł"] = plainTitle,
item:node( image )
    ["obraz"] = image, ["ustawienia"] = 'InvSprite'
    }
else
    image, spriteCat = sprite{
    ["danychID"] = idData, ["tytuł"] = plainTitle,
    ["obraz"] = image, ["ustawienia"] = 'InvSprite/' .. mod
    }
end
item:node(image)
category = spriteCat
category = spriteCat
end
end
Linia 242: Linia 187:
if num and num > 1 and num < 1000 then
if num and num > 1 and num < 1000 then
if img and link then
if img and link then
item:wikitext('[[', link, '|')
item:wikitext( '[[', link, '|' )
end
end
local number = item
local number = item
:tag('span')
:tag( 'span' )
:addClass('invslot-stacksize')
:addClass( 'invslot-stacksize' )
:attr{title = plainTitle}
:attr{ title = plainTitle }
:wikitext(num)
:wikitext( num )
if numStyle then
if numStyle then
number:cssText(numStyle)
number:cssText( numStyle )
end
end
if img and link then
if img and link then
item:wikitext(']]')
item:wikitext( ']]' )
end
end
end
end
if idData and link then
if idData and link then
item:wikitext(']]')
item:wikitext( ']]' )
end
end
item:wikitext(category)
item:wikitext( category )
end
end
if first then
if first then
if animated and item then
if animated and item then
item:addClass('active')
item:addClass( 'active' )
end
end
first = false
first = false
Linia 277: Linia 222:
function p.expandAlias( frameParts, alias )
function p.expandAlias( frameParts, alias )
-- If the frame has no parts, we can just return the alias as-is
-- If the frame has no parts, we can just return the alias as-is
--[[if not frameParts.title and not frameParts.mod and not frameParts.num and not frameParts.text then
if not frameParts.title and not frameParts.mod and not frameParts.num and not frameParts.text then
return alias
return alias
end--]]
end
local expandedFrames = {}
local expandedFrames = {}
for aliasFrame in mw.text.gsplit(alias, '%s*;%s*') do
for aliasFrame in mw.text.gsplit( alias, '%s*;%s*' ) do
local aliasParts = p.getParts(aliasFrame)
local aliasParts = p.getParts( aliasFrame )
aliasParts.title = frameParts.title or aliasParts.title or ''
aliasParts.title = frameParts.title or aliasParts.title or ''
aliasParts.mod = frameParts.mod or aliasParts.mod or 'Minecraft'
aliasParts.mod = frameParts.mod or aliasParts.mod or 'Minecraft'
Linia 289: Linia 234:
aliasParts.text = frameParts.text or aliasParts.text or ''
aliasParts.text = frameParts.text or aliasParts.text or ''
table.insert(expandedFrames, mw.ustring.format(
table.insert( expandedFrames, string.format(
'[%s]%s:%s,%s[%s]',
'[%s]%s:%s,%s[%s]',
aliasParts.title, aliasParts.mod, aliasParts.name, aliasParts.num, aliasParts.text
aliasParts.title, aliasParts.mod, aliasParts.name, aliasParts.num, aliasParts.text
))
) )
end
end
return table.concat(expandedFrames, ';')
return table.concat( expandedFrames, ';' )
end
end


 
function p.getParts( frame, mod )
function p.getParts(frame, mod)
----Функция получает название предмета в формате "[титл]мод:имя[текст],число"
----parts.title = титл, название предмета при наведении
----parts.mod = мод
----parts.name = имя
----parts.text = текст, дополнительный текст при наведении на предмет
----parts.num = число
 
local parts = {}
local parts = {}
parts.title = mw.ustring.match(frame, '^%[%s*([^%]]+)%s*%]')
parts.title = frame:match( '^%[%s*([^%]]+)%s*%]' )
    local modPattern
parts.mod = mw.text.trim( frame:match( '([^:%]]+):' ) or mod or '' )
    if mw.ustring.match(frame, '^%[.*%]([a-zA-ZąćęłńóśźżĄĆĘŁŃÓŚŻŹа-яА-Я0-9 _-]+):') then
    modPattern = '^%[.*%]([a-zA-ZąćęłńóśźżĄĆĘŁŃÓŚŻŹа-яА-Я0-9 _-]+):'
    else
    modPattern = '^([a-zA-ZąćęłńóśźżĄĆĘŁŃÓŚŻŹа-яА-Я0-9 _-]+):'
    end
 
parts.mod = mw.text.trim(mw.ustring.match(frame, modPattern) or mod or '') ---- Получаем название мода
local vanilla = {v = 1, vanilla = 1, mc = 1, minecraft = 1}
local vanilla = { v = 1, vanilla = 1, mc = 1, minecraft = 1 }
if parts.mod == '' or vanilla[mw.ustring.lower(parts.mod)] then
if parts.mod == '' or vanilla[mw.ustring.lower( parts.mod )] then
parts.mod = nil
parts.mod = nil
end
end
if p.modAliases[parts.mod] then
parts.mod = p.modAliases[parts.mod]
local nameStart = ( frame:find( ':' ) or frame:find( '%]' ) or 0 ) + 1
end
 
local _, nameStartV = mw.ustring.find( frame, '^%[[^%]]*%]' )
local nameStart = ( ({mw.ustring.find( frame, modPattern )})[2] or nameStartV or 0 ) + 1
if nameStart - 1 == #frame then
if nameStart - 1 == #frame then
nameStart = 1
nameStart = 1
end
end
parts.name = mw.text.trim( mw.ustring.sub( frame, nameStart, ( mw.ustring.find( frame, '[,%[]', nameStart ) or 0 ) - 1 ) )
parts.name = mw.text.trim( frame:sub( nameStart, ( frame:find( '[,%[]', nameStart ) or 0 ) - 1 ) )
parts.num = math.floor(mw.ustring.match(frame, ',%s*(%d+)') or 0)
parts.num = math.floor( frame:match( ',%s*(%d+)' ) or 0 )
if parts.num == 0 then
if parts.num == 0 then
parts.num = nil
parts.num = nil
end
end
parts.text = mw.ustring.match(frame, '%[%s*([^%]]+)%s*%]$')
parts.text = frame:match( '%[%s*([^%]]+)%s*%]$' )
return parts
return parts

Wersja z 21:07, 22 kwi 2017

local p = {}

-- List of special prefixes which should be handled by
-- other modules (such as being moved outside links)
p.prefixes = {
	'Any',
	'Matching',
	'Damaged'
}

function p.slot( f )
	local args = f.args or f
	if f == mw.getCurrentFrame() and args[1] == nil then
		args = f:getParent().args
	end
	
	args[1] = mw.text.trim( args[1] or '' )
	
	-- Comment this next line out if you're not using aliases
	local aliases = mw.loadData( 'Module:Inventory slot/Aliases' )
	
	local modAliases = args.modaliases or ''
	if modAliases ~= '' then
		modAliases = mw.loadData( 'Module:' .. modAliases )
	else
		modAliases = nil
	end
	
	if aliases or modAliases then
		local frames = {}
		for frame in mw.text.gsplit( args[1], '%s*;%s*' ) do
			local frameParts = p.getParts( frame, args.mod )
			
			local id = frameParts.name
			if frameParts.mod then
				id = frameParts.mod .. ':' .. id
			end
			
			local alias
			if modAliases and modAliases[id] then
				alias = modAliases[id]
			elseif aliases and aliases[id] then
				alias = aliases[id]
			end
			
			if alias then
				table.insert( frames, p.expandAlias( frameParts, alias ) )
			else
				table.insert( frames, frame )
			end
		end
		
		args[1] = table.concat( frames, ';' )
	end
	
	local sprite
	local ids = mw.loadData( [[Module:InvSprite/IDs]] ).ids
	local modIds = {}
	local animated = args[1]:find( ';' )
	local pageName = mw.title.getCurrentTitle().text
	local imgClass = args.imgclass
	local numStyle = args.numstyle
	local body = mw.html.create( 'span' ):addClass( 'invslot' ):css{ ['vertical-align'] = args.align }
	if animated then
		body:addClass( 'animated' )
	end
	if args.class then
		body:addClass( args.class )
	end
	if args.style then
		body:cssText( args.style )
	end
	
	if ( args.default or '' ) ~= '' then
		body:css( 'background-image', '{{FileUrl|' .. args.default .. '.png}}' )
	end
	
	local first = true
	for frame in mw.text.gsplit( args[1], '%s*;%s*' ) do
		local item
		if frame ~= '' or frame == '' and animated then
			item = body:tag( 'span' ):addClass( 'invslot-item' )
			if imgClass then
				item:addClass( imgClass )
			end
		end
		
		if frame == '' then
			( item or body ):tag( 'br' )
		else
			local category
			local parts = p.getParts( frame, args.mod )
			local title = parts.title or mw.text.trim( args.title or '' )
			local mod = parts.mod
			local name = parts.name
			local num = parts.num
			local description = parts.text
			
			local img, idData
			if mod then
				local modData = modIds[mod]
				if not modData and mw.title.new( 'Module:InvSprite/Mods/' .. mod .. '/IDs' ).exists then
					modData = mw.loadData( 'Module:InvSprite/Mods/' .. mod .. '/IDs' )
					modIds[mod] = modData
				end
				if modData and modData[name] then
					idData = modData[name]
				else
					img = name .. ' (' .. mod .. ')'
				end
			elseif ids[name] then
				idData = ids[name]
			else
				img = name
			end
			
			local link = args.link or ''
			if link == '' then
				if mod then
					link = 'Mods/' .. mod .. '/' .. name
				else
					link = name:gsub( '^Damaged ', '' )
				end
			elseif link:lower() == 'none' then
				link = nil
			end
			if link == pageName then
				link = nil
			end
			
			local formattedTitle
			local plainTitle
			if title == '' then
				plainTitle = name
			elseif title:lower() ~= 'none' then
				plainTitle = title:gsub( '\\\\', '&#92;' ):gsub( '\\&', '&#38;' )
				
				local formatPattern = '&[0-9a-fk-or]'
				if plainTitle:match( formatPattern ) then
					formattedTitle = title
					plainTitle = plainTitle:gsub( formatPattern, '' )
				end
				
				if plainTitle == '' then
					plainTitle = name
				else
					plainTitle = plainTitle:gsub( '&#92;', '\\' ):gsub( '&#38;', '&' )
				end
			elseif link then
				if img then
					formattedTitle = ''
				else
					plainTitle = ''
				end
			end
			
			item:attr{
				['data-minetip-title'] = formattedTitle,
				['data-minetip-text'] = description
			}
			
			if img then
				-- & is re-escaped because mw.html treats attributes
				-- as plain text, but MediaWiki doesn't
				local escapedTitle = ( plainTitle or '' ):gsub( '&', '&#38;' )
				item:addClass( 'invslot-item-image' )
					:wikitext( '[[File:Grid ', img, '.png|32x32px|link=', link or '', '|', escapedTitle, ']]' )
			else
				if not sprite then
					sprite = require( [[Module:Sprite]] ).sprite
				end
				local image
				if mod then
					image = args.spritesheet or mod .. 'Sprite.png'
				end
				if link then
					item:wikitext( '[[', link, '|' )
				end
				local image, spriteCat = sprite{
					iddata = idData, title = plainTitle,
					image = image, settings = 'InvSprite'
				}
				item:node( image )
				category = spriteCat
			end
			
			if num and num > 1 and num < 1000 then
				if img and link then
					item:wikitext( '[[', link, '|' )
				end
				local number = item
					:tag( 'span' )
						:addClass( 'invslot-stacksize' )
						:attr{ title = plainTitle }
						:wikitext( num )
				if numStyle then
					number:cssText( numStyle )
				end
				if img and link then
					item:wikitext( ']]' )
				end
			end
			
			if idData and link then
				item:wikitext( ']]' )
			end
			
			item:wikitext( category )
		end
		
		if first then
			if animated and item then
				item:addClass( 'active' )
			end
			first = false
		end
	end
	
	return tostring( body )
end

function p.expandAlias( frameParts, alias )
	-- If the frame has no parts, we can just return the alias as-is
	if not frameParts.title and not frameParts.mod and not frameParts.num and not frameParts.text then
		return alias
	end
	
	local expandedFrames = {}
	for aliasFrame in mw.text.gsplit( alias, '%s*;%s*' ) do
		local aliasParts = p.getParts( aliasFrame )
		aliasParts.title = frameParts.title or aliasParts.title or ''
		aliasParts.mod = frameParts.mod or aliasParts.mod or 'Minecraft'
		aliasParts.num = frameParts.num or aliasParts.num or ''
		aliasParts.text = frameParts.text or aliasParts.text or ''
		
		table.insert( expandedFrames, string.format(
			'[%s]%s:%s,%s[%s]',
			aliasParts.title, aliasParts.mod, aliasParts.name, aliasParts.num, aliasParts.text
		) )
	end
	
	return table.concat( expandedFrames, ';' )
end

function p.getParts( frame, mod )
	local parts = {}
	parts.title = frame:match( '^%[%s*([^%]]+)%s*%]' )
	
	parts.mod = mw.text.trim( frame:match( '([^:%]]+):' ) or mod or '' )
	
	local vanilla = { v = 1, vanilla = 1, mc = 1, minecraft = 1 }
	if parts.mod == '' or vanilla[mw.ustring.lower( parts.mod )] then
		parts.mod = nil
	end
	
	local nameStart = ( frame:find( ':' ) or frame:find( '%]' ) or 0 ) + 1
	if nameStart - 1 == #frame then
		nameStart = 1
	end
	parts.name = mw.text.trim( frame:sub( nameStart, ( frame:find( '[,%[]', nameStart ) or 0 ) - 1 ) )
	
	parts.num = math.floor( frame:match( ',%s*(%d+)' ) or 0 )
	if parts.num == 0 then
		parts.num = nil
	end
	
	parts.text = frame:match( '%[%s*([^%]]+)%s*%]$' )
	
	return parts
end
 
return p