Немає опису редагування |
Немає опису редагування |
||
(Не показані 11 проміжних версій 3 користувачів) | |||
Рядок 11: | Рядок 11: | ||
local settings = data.settings |
local settings = data.settings |
||
+ | -- Стандартні налаштування |
||
− | -- Default settings |
||
local default = { |
local default = { |
||
scale = 1, |
scale = 1, |
||
Рядок 23: | Рядок 23: | ||
if settings then |
if settings then |
||
if not settings.stylesheet then |
if not settings.stylesheet then |
||
+ | -- Зробіть окремий клон поточних налаштувань за промовчанням |
||
− | -- Make a separate clone of the current default settings |
||
defaultStyle = mw.clone( default ) |
defaultStyle = mw.clone( default ) |
||
end |
end |
||
Рядок 36: | Рядок 36: | ||
local sprite = mw.html.create( 'span' ):addClass( 'sprite' ) |
local sprite = mw.html.create( 'span' ):addClass( 'sprite' ) |
||
− | sprite:tag( 'br' ) |
||
+ | -- Метод від mw.html до css здійснює дуже повільне екранування вхідних даних, що подвоює час, |
||
− | -- mw.html's css method performs very slow escaping, which doubles the time it takes |
||
+ | -- необхідний для роботи, так що ми побудуємо стилі вручну, і помістимо їх в cssText. |
||
− | -- to run, so we'll construct the styles manually, and put them in the cssText |
||
+ | -- метод, який робить тільки html екранування (якиий значно швидший) |
||
− | -- method, which only does html escaping (which isn't slow) |
||
local styles = {} |
local styles = {} |
||
− | if setting( 'url' ) then |
+ | if not setting( 'nourl' ) and setting( 'url' ) then |
− | styles[#styles + 1] = 'background-image:' .. setting( 'url' ) |
+ | styles[#styles + 1] = 'background-image:' .. ( setting( 'url' ).url or setting( 'url' ) ) |
end |
end |
||
if setting( 'stylesheet' ) then |
if setting( 'stylesheet' ) then |
||
Рядок 54: | Рядок 53: | ||
styles[#styles + 1] = 'background-image:' .. p.getUrl( |
styles[#styles + 1] = 'background-image:' .. p.getUrl( |
||
setting( 'image' ) or setting( 'name' ) .. 'Sprite.png' |
setting( 'image' ) or setting( 'name' ) .. 'Sprite.png' |
||
− | ) |
+ | ).url |
end |
end |
||
local class = setting( 'class' ) |
local class = setting( 'class' ) |
||
Рядок 94: | Рядок 93: | ||
local spriteText |
local spriteText |
||
if text then |
if text then |
||
+ | if not args['wrap'] then |
||
− | root = mw.html.create( 'span' ):addClass( 'nowrap' ) |
+ | root = mw.html.create( 'span' ):addClass( 'nowrap' ) |
+ | end |
||
spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( text ) |
spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( text ) |
||
end |
end |
||
Рядок 113: | Рядок 114: | ||
local link = setting( 'link' ) or '' |
local link = setting( 'link' ) or '' |
||
if link ~= '' and mw.ustring.lower( link ) ~= 'none' then |
if link ~= '' and mw.ustring.lower( link ) ~= 'none' then |
||
+ | -- Зовнішнє посилання |
||
− | -- External link |
||
if link:find( '//' ) then |
if link:find( '//' ) then |
||
return '[' .. link .. ' ' .. tostring( root ) .. ']' |
return '[' .. link .. ' ' .. tostring( root ) .. ']' |
||
end |
end |
||
+ | -- Внутрішнє посилання |
||
− | -- Internal link |
||
local linkPrefix = setting( 'linkprefix' ) or '' |
local linkPrefix = setting( 'linkprefix' ) or '' |
||
return '[[' .. linkPrefix .. link .. '|' .. tostring( root ) .. ']]' |
return '[[' .. linkPrefix .. link .. '|' .. tostring( root ) .. ']]' |
||
Рядок 144: | Рядок 145: | ||
local title = mw.title.getCurrentTitle() |
local title = mw.title.getCurrentTitle() |
||
+ | -- Видалити категорії на сторінках інтернаціоналізації, сторінках розмови і в просторах імен Користувач/UserWiki/UserProfile. |
||
− | -- Remove categories on language pages, talk pages, and in User/UserWiki/UserProfile namespaces |
||
local disallowCats = args.nocat or title.isTalkPage or title.nsText:find( '^User' ) |
local disallowCats = args.nocat or title.isTalkPage or title.nsText:find( '^User' ) |
||
if idData then |
if idData then |
||
− | if idData.deprecated |
+ | if idData.deprecated then |
+ | args.class = ( args.class or '' ) .. ' sprite-deprecated' |
||
− | table.insert( categories, '[[Категорія:Pages using deprecated sprite names]]' ) |
||
+ | if not disallowCats then |
||
+ | categories[#categories + 1] = '[[Категорія:Сторінки, що використовують застарілі імена спрайтів]]' |
||
+ | end |
||
end |
end |
||
args.pos = idData.pos |
args.pos = idData.pos |
||
elseif not disallowCats then |
elseif not disallowCats then |
||
− | + | categories[#categories + 1] = '[[Категорія:Сторінки з відсутніми спрайтами]]' |
|
end |
end |
||
Рядок 181: | Рядок 185: | ||
end |
end |
||
− | function p.getUrl( |
+ | function p.getUrl( image, query, classname ) |
− | + | local f = mw.getCurrentFrame() |
|
+ | local t = { |
||
+ | url = f:expandTemplate{ |
||
+ | title = 'FileUrl', |
||
+ | args = { image, query = query } |
||
+ | }, |
||
+ | } |
||
+ | if classname and classname ~= '' then |
||
+ | t.style = f:expandTemplate{ |
||
+ | title = 'FileUrlStyle', |
||
+ | args = { classname, image, query = query } |
||
+ | } |
||
+ | end |
||
+ | return t |
||
+ | end |
||
+ | |||
+ | function p.getParsedUrlStyle( f ) |
||
+ | local args = f:getParent().args |
||
+ | local module = args[1] |
||
+ | return require( 'Модуль:' .. module ).settings.url.style |
||
end |
end |
||
Рядок 224: | Рядок 247: | ||
return table.concat( protections, ',' ) |
return table.concat( protections, ',' ) |
||
+ | end |
||
+ | |||
+ | local spriteStyle = '' |
||
+ | if data.settings.url and data.settings.url.style then |
||
+ | spriteStyle = data.settings.url.style |
||
end |
end |
||
local dataTitle = mw.title.new( 'Модуль:' .. dataPage ) |
local dataTitle = mw.title.new( 'Модуль:' .. dataPage ) |
||
+ | -- Тимчасово, доки це не оновиться |
||
+ | local classname = '' |
||
+ | if data.settings.stylesheet then |
||
+ | classname = data.settings.classname or |
||
+ | mw.ustring.lower( data.settings.name:gsub( ' ', '-' ) ) .. '-sprite' |
||
+ | end |
||
local spritesheet = data.settings.image or data.settings.name .. 'Sprite.png' |
local spritesheet = data.settings.image or data.settings.name .. 'Sprite.png' |
||
− | local spriteTitle = mw.title.new( ' |
+ | local spriteTitle = mw.title.new( 'File:' .. spritesheet ) |
local dataProtection = getProtection( dataTitle, 'edit' ) |
local dataProtection = getProtection( dataTitle, 'edit' ) |
||
local spriteProtection = getProtection( spriteTitle, 'upload', 'upload,reupload' ) |
local spriteProtection = getProtection( spriteTitle, 'upload', 'upload,reupload' ) |
||
Рядок 238: | Рядок 272: | ||
['data-spritesheet'] = spritesheet, |
['data-spritesheet'] = spritesheet, |
||
['data-spriteprotection'] = spriteProtection, |
['data-spriteprotection'] = spriteProtection, |
||
− | ['data-urlfunc'] = "require( [[Модуль: |
+ | ['data-urlfunc'] = "require( [[Модуль:Спрайт]] ).getUrl( '" .. spritesheet .. "', '$1', '" .. classname .. "' )", |
− | ['data-refreshtext'] = mw.text.nowiki( '{{#invoke: |
+ | ['data-refreshtext'] = mw.text.nowiki( '{{#invoke:Спрайт|doc|' .. dataPage .. '|refresh=1}}' ), |
['data-settings'] = mw.text.jsonEncode( data.settings ), |
['data-settings'] = mw.text.jsonEncode( data.settings ), |
||
} ) |
} ) |
||
Рядок 272: | Рядок 306: | ||
local box = section.boxes:tag( 'li' ):addClass( 'spritedoc-box' ):attr( 'data-pos', pos ) |
local box = section.boxes:tag( 'li' ):addClass( 'spritedoc-box' ):attr( 'data-pos', pos ) |
||
box:tag( 'div' ):addClass( 'spritedoc-image' ) |
box:tag( 'div' ):addClass( 'spritedoc-image' ) |
||
− | :wikitext( p.base{ pos = pos, data = dataPage } ) |
+ | :wikitext( p.base{ pos = pos, data = dataPage, nourl = spriteStyle ~= '' } ) |
names = box:tag( 'ul' ):addClass( 'spritedoc-names' ) |
names = box:tag( 'ul' ):addClass( 'spritedoc-names' ) |
||
Рядок 287: | Рядок 321: | ||
if args.refresh then |
if args.refresh then |
||
− | return '', tostring( body ) |
+ | return '', '', tostring( body ) |
end |
end |
||
− | + | local styles = f:callParserFunction( '#widget:SpriteDoc.css' ) |
|
+ | return styles, spriteStyle, tostring( body ) |
||
end |
end |
||
return p |
return p |
Версія за 13:50, 23 квітня 2021
Цей модуль реалізує {{Спрайт}}
. Загалом його слід викликати безпосередньо на сторінках шаблону, а не використовувати шаблон спрайту.
Батьківські аргументи автоматично об’єднуються з безпосередньо переданими аргументами (останні перезаписують перші), і всі аргументи нормалізуються, щоб скоротити пробіли та встановити порожні аргументи на nil
.
Залежності
Див. також
- Minecraft
{{Спрайт/Біом}}
{{Спрайт/Блок}}
{{Спрайт/Ефект}}
{{Спрайт/Сутність}}
{{Спрайт/Оточення}}
{{Спрайт/Предмет}}
{{ІнвСпрайт}}
{{SchematicSprite}}
{{Спрайт/Слот}}
{{Спрайт/Досягнення}}
{{Спрайт/НовДосягнення}}
- Minecraft Dungeons
- Minecraft (старе)
{{Спрайт/СтарБлок}}
- Інше
{{Спрайт}}
{{Спрайт/Backpacks}}
{{CommentSprite}}
{{Спрайт/Головна сторінка}}
- Модуль:Спрайт
local p = {}
function p.base( f )
local args = f
if f == mw.getCurrentFrame() then
args = require( 'Модуль:ProcessArgs' ).merge( true )
else
f = mw.getCurrentFrame()
end
local data = args.data and mw.loadData( 'Модуль:' .. args.data ) or {}
local settings = data.settings
-- Стандартні налаштування
local default = {
scale = 1,
sheetsize = 256,
size = 16,
pos = 1,
align = 'text-top'
}
local defaultStyle = default
if settings then
if not settings.stylesheet then
-- Зробіть окремий клон поточних налаштувань за промовчанням
defaultStyle = mw.clone( default )
end
for k, v in pairs( settings ) do
default[k] = v
end
end
local setting = function( arg )
return args[arg] or default[arg]
end
local sprite = mw.html.create( 'span' ):addClass( 'sprite' )
-- Метод від mw.html до css здійснює дуже повільне екранування вхідних даних, що подвоює час,
-- необхідний для роботи, так що ми побудуємо стилі вручну, і помістимо їх в cssText.
-- метод, який робить тільки html екранування (якиий значно швидший)
local styles = {}
if not setting( 'nourl' ) and setting( 'url' ) then
styles[#styles + 1] = 'background-image:' .. ( setting( 'url' ).url or setting( 'url' ) )
end
if setting( 'stylesheet' ) then
sprite:addClass(
setting( 'classname' ) or
mw.ustring.lower( setting( 'name' ):gsub( ' ', '-' ) ) .. '-sprite'
)
elseif not setting( 'url' ) then
styles[#styles + 1] = 'background-image:' .. p.getUrl(
setting( 'image' ) or setting( 'name' ) .. 'Sprite.png'
).url
end
local class = setting( 'class' )
if class then
sprite:addClass( class )
end
local size = setting( 'size' )
local sheetWidth = setting( 'sheetsize' )
local tiles = sheetWidth / size
local pos = setting( 'pos' ) - 1
local scale = setting( 'scale' )
local autoScale = setting( 'autoscale' )
if pos then
local left = pos % tiles * size * scale
local top = math.floor( pos / tiles ) * size * scale
styles[#styles + 1] = 'background-position:-' .. left .. 'px -' .. top .. 'px'
end
if not autoScale and scale ~= defaultStyle.scale then
styles[#styles + 1] = 'background-size:' .. sheetWidth * scale .. 'px auto'
end
if size ~= defaultStyle.size or ( not autoScale and scale ~= defaultStyle.scale ) then
styles[#styles + 1] = 'height:' .. size * scale .. 'px'
styles[#styles + 1] = 'width:' .. size * scale .. 'px'
end
local align = setting( 'align' )
if align ~= defaultStyle.align then
styles[#styles + 1] = 'vertical-align:' .. align
end
styles[#styles + 1] = setting( 'css' )
sprite:cssText( table.concat( styles, ';' ) )
local text = setting( 'text' )
local root
local spriteText
if text then
if not args['wrap'] then
root = mw.html.create( 'span' ):addClass( 'nowrap' )
end
spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( text )
end
local title = setting( 'title' )
if title then
( root or sprite ):attr( 'title', title )
end
if not root then
root = mw.html.create( '' )
end
root:node( sprite )
if spriteText then
root:node( spriteText )
end
local link = setting( 'link' ) or ''
if link ~= '' and mw.ustring.lower( link ) ~= 'none' then
-- Зовнішнє посилання
if link:find( '//' ) then
return '[' .. link .. ' ' .. tostring( root ) .. ']'
end
-- Внутрішнє посилання
local linkPrefix = setting( 'linkprefix' ) or ''
return '[[' .. linkPrefix .. link .. '|' .. tostring( root ) .. ']]'
end
return tostring( root )
end
function p.sprite( f )
local args = f
if f == mw.getCurrentFrame() then
args = require( 'Модуль:ProcessArgs' ).merge( true )
else
f = mw.getCurrentFrame()
end
local data = args.data and mw.loadData( 'Модуль:' .. args.data ) or {}
local categories = {}
local idData = args.iddata
if not idData then
local name = args.name or data.settings.name
local id = mw.text.trim( tostring( args[1] or '' ) )
idData = data.ids[id] or data.ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )]
end
local title = mw.title.getCurrentTitle()
-- Видалити категорії на сторінках інтернаціоналізації, сторінках розмови і в просторах імен Користувач/UserWiki/UserProfile.
local disallowCats = args.nocat or title.isTalkPage or title.nsText:find( '^User' )
if idData then
if idData.deprecated then
args.class = ( args.class or '' ) .. ' sprite-deprecated'
if not disallowCats then
categories[#categories + 1] = '[[Категорія:Сторінки, що використовують застарілі імена спрайтів]]'
end
end
args.pos = idData.pos
elseif not disallowCats then
categories[#categories + 1] = '[[Категорія:Сторінки з відсутніми спрайтами]]'
end
return p.base( args ), table.concat( categories )
end
function p.link( f )
local args = f
if f == mw.getCurrentFrame() then
args = require( 'Модуль:ProcessArgs' ).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.notext then
text = args.text or args[2] or link
end
args[1] = args.id or args[1]
args.link = args.link or link
args.text = text
return p.sprite( args )
end
function p.getUrl( image, query, classname )
local f = mw.getCurrentFrame()
local t = {
url = f:expandTemplate{
title = 'FileUrl',
args = { image, query = query }
},
}
if classname and classname ~= '' then
t.style = f:expandTemplate{
title = 'FileUrlStyle',
args = { classname, image, query = query }
}
end
return t
end
function p.getParsedUrlStyle( f )
local args = f:getParent().args
local module = args[1]
return require( 'Модуль:' .. module ).settings.url.style
end
function p.doc( f )
local args = f
if f == mw.getCurrentFrame() then
args = f.args
else
f = mw.getCurrentFrame()
end
local dataPage = mw.text.trim( args[1] )
local data = mw.loadData( 'Модуль:' .. dataPage )
local getProtection = function( title, action, extra )
local protections = { 'edit' }
if extra then
protections[#protections + 1] = extra
end
local addProtection = function( protection )
if protection == 'autoconfirmed' then
protection = 'editsemiprotected'
elseif protection == 'sysop' then
protection = 'editprotected'
end
protections[#protections + 1] = protection
end
local direct = title.protectionLevels[action] or {}
for _, protection in ipairs( direct ) do
addProtection( protection )
end
local cascading = title.cascadingProtection.restrictions[action] or {}
if #cascading > 0 then
protections[#protections + 1] = 'protect'
end
for _, protection in ipairs( cascading ) do
addProtection( protection )
end
return table.concat( protections, ',' )
end
local spriteStyle = ''
if data.settings.url and data.settings.url.style then
spriteStyle = data.settings.url.style
end
local dataTitle = mw.title.new( 'Модуль:' .. dataPage )
-- Тимчасово, доки це не оновиться
local classname = ''
if data.settings.stylesheet then
classname = data.settings.classname or
mw.ustring.lower( data.settings.name:gsub( ' ', '-' ) ) .. '-sprite'
end
local spritesheet = data.settings.image or data.settings.name .. 'Sprite.png'
local spriteTitle = mw.title.new( 'File:' .. spritesheet )
local dataProtection = getProtection( dataTitle, 'edit' )
local spriteProtection = getProtection( spriteTitle, 'upload', 'upload,reupload' )
local body = mw.html.create( 'div' ):attr( {
id = 'spritedoc',
['data-dataprotection'] = dataProtection,
['data-datatimestamp'] = f:callParserFunction( 'REVISIONTIMESTAMP', 'Модуль:' .. dataPage ),
['data-datapage'] = 'Модуль:' .. dataPage,
['data-spritesheet'] = spritesheet,
['data-spriteprotection'] = spriteProtection,
['data-urlfunc'] = "require( [[Модуль:Спрайт]] ).getUrl( '" .. spritesheet .. "', '$1', '" .. classname .. "' )",
['data-refreshtext'] = mw.text.nowiki( '{{#invoke:Спрайт|doc|' .. dataPage .. '|refresh=1}}' ),
['data-settings'] = mw.text.jsonEncode( data.settings ),
} )
local sections = {}
for _, sectionData in ipairs( data.sections or { name = 'Uncategorized' } ) do
local sectionTag = body:tag( 'div' ):addClass( 'spritedoc-section' ):attr( 'data-section-id', sectionData.id )
sectionTag:tag( 'h3' ):wikitext( sectionData.name )
sections[sectionData.id] = { boxes = sectionTag:tag( 'ul' ):addClass( 'spritedoc-boxes' ) }
end
local keyedData = {}
local i = 1
for name, idData in pairs( data.ids ) do
keyedData[i] = {
sortKey = mw.ustring.lower( name ),
name = name,
data = idData
}
i = i + 1
end
table.sort( keyedData, function( a, b )
return a.sortKey < b.sortKey
end )
for _, data in ipairs( keyedData ) do
local idData = data.data
local pos = idData.pos
local section = sections[idData.section]
local names = section[pos]
if not names then
local box = section.boxes:tag( 'li' ):addClass( 'spritedoc-box' ):attr( 'data-pos', pos )
box:tag( 'div' ):addClass( 'spritedoc-image' )
:wikitext( p.base{ pos = pos, data = dataPage, nourl = spriteStyle ~= '' } )
names = box:tag( 'ul' ):addClass( 'spritedoc-names' )
section[pos] = names
end
local nameElem = mw.html.create( 'li' ):addClass( 'spritedoc-name' )
local codeElem = nameElem:tag( 'code' ):wikitext( data.name )
if idData.deprecated then
codeElem:addClass( 'spritedoc-deprecated' )
end
names:wikitext( tostring( nameElem ) )
end
if args.refresh then
return '', '', tostring( body )
end
local styles = f:callParserFunction( '#widget:SpriteDoc.css' )
return styles, spriteStyle, tostring( body )
end
return p