| Данная страница входит в проект Обновление модулей. |
Экспериментальный модуль для создания документации к таблицам спрайтов.
-- Модуль для отображения иконок из таблиц спрайтов.
-- Внимание: Неосторожные изменения модуля могут привести к проблемам на большом количестве статей!
local p = {}
local h = {}
-- Faster than mw.text.trim, but does not have the memory penalty of gsub-based
-- solutions
function h.fasterTrim(str)
local byte = string.byte
local startIndex = 0
local space, newline, tab = byte(" \n\t", 1, 3)
local strByte
repeat
startIndex = startIndex + 1
strByte = byte(str, startIndex, startIndex)
until (strByte ~= space) and (strByte ~= newline) and (strByte ~= tab)
local endIndex = str:len() + 1
repeat
endIndex = endIndex - 1
strByte = byte(str, endIndex, endIndex)
until (strByte ~= space) and (strByte ~= newline) and (strByte ~= tab)
return str:sub(startIndex, endIndex)
end
function h.getProtection(title, action, extra)
local protections = {'edit', extra}
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
function h.sortIdData(idDataList)
local lang = mw.language.getContentLanguage()
local keyedData = {}
local i = 1
for name, idData in pairs( idDataList ) do
keyedData[i] = {
sortKey = lang:lc( name ),
name = name
}
i = i + 1
end
table.sort( keyedData, function( a, b )
return a.sortKey < b.sortKey
end )
-- Optimize for garbage collection, I guess?
for _, entry in ipairs(keyedData) do
entry.sortKey = nil
end
return keyedData
end
function h.getDefaults(settings)
local default = {
["масштаб"] = 1,
["формат"] = 256,
["разм"] = 16,
["поз"] = 1,
["выравн"] = 'text-top',
["страница"] = '',
}
local defaultStyle = default
if settings then
if not settings['таблстилей'] then
-- Создаём отдельную копию текущих настроек по умолчанию
defaultStyle = mw.clone( default )
end
for k, v in pairs( settings ) do
default[k] = v
end
end
return default, defaultStyle
end
-- Создание спрайта
function p.base(args)
local data = args['данные']
local default = args.default
local defaultStyle = args.defaultStyle
local className = args.className
local sprite = mw.html.create('span')
:addClass('sprite')
:addClass(className)
:addClass(default['класс'])
-- Метод css от mw.html производит очень медленное экранирование входных данных, что тормозит работу в два раза. Вместо
-- этого стили будут создаваться вручную, и будут передаваться через метод cssText, который делает только экранирование HTML,
-- что куда быстрее.
local styles = {}
local page = default['страница'] or default['главная_страница']
local urlSetting = default['url']
if not args.nourl and urlSetting then
styles[#styles + 1] = 'background-image:' .. (urlSetting.url or urlSetting)
end
if (not default['таблстилей']) and page then
-- временная заглушка для GregTech
styles[#styles + 1] = 'background-image: {{FileUrl|' .. (default['изобр'] or default['имя'] .. page .. 'CSS.png') .. '}}'
elseif not urlSetting then
styles[#styles + 1] = 'background-image:' .. p.getUrl(
default['изобр'] or (default['имя'] .. 'CSS.png')
).url
end
-- Настройки страницы многостраничного спрайта
local scaleq = 1
local settingPage = default[page]
local pageSize, pageFormat
if settingPage then
scaleq = settingPage['множитель'] or 1
pageSize = settingPage['разм']
pageFormat = settingPage['формат']
else
scaleq = default['множитель'] or 1
end
local size = pageSize or default['разм'] -- размер спрайта в пикселях
local v_size = default['верт_разм'] or size -- размер спрайта в пикселях в высоту
local pos = math.abs(args['поз']) - 1 -- положение спрайта в таблице
local sheetWidth = pageFormat or default['формат'] -- ширина таблицы спрайта в пикселях
local tiles = sheetWidth / size -- количество спрайтов в одной строке
local left = pos % tiles * size -- горизонтальная координата спрайта
local top = math.floor(pos / tiles) * v_size -- вертикальная координата спрайта
local scale = default['масштаб'] * scaleq -- масштаб спрайта (во сколько раз увеличить или уменьшить размер)
local autoscale = default['автомасштаб'] -- автоматическое применение масштабирования
local align = default['выравн'] -- выравнивание по вертикали
-- Координаты
if left > 0 or top > 0 then
styles[#styles + 1] = 'background-position: -' .. left * scale .. 'px -' .. top * scale .. 'px'
end
-- Масштаб
local nonDefaultScale = not autoscale and scale ~= defaultStyle["масштаб"]
if nonDefaultScale then
styles[#styles + 1] = 'background-size: ' .. sheetWidth * scale .. 'px auto'
end
-- Размеры спрайта
if size ~= defaultStyle["разм"] or nonDefaultScale then
styles[#styles + 1] = 'height: ' .. v_size * scale .. 'px'
styles[#styles + 1] = 'width: ' .. size * scale .. 'px'
end
-- Выравнивание
if align ~= defaultStyle["выравн"] then
styles[#styles + 1] = 'vertical-align: ' .. align
end
-- Дополнительный CSS-код, указанный в параметре
styles[#styles + 1] = default['css']
-- Применение полученных CSS-стилей к спрайту.
sprite:cssText(table.concat(styles, ';'))
return tostring( mw.html.create( '' ):node( sprite ) )
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.doc(f)
local args = mw.getCurrentFrame().args
local dataPage = h.fasterTrim( args[1] )
local data = mw.loadData( 'Модуль:' .. dataPage )
local idDataSections = data['разделы']
local idDataList = data['IDы']
local dataSettings = data['настройки']
local idDataListOverride = dataSettings['списокID']
if idDataListOverride then
local overrideList = mw.loadData( 'Модуль:' .. idDataListOverride )
idDataList = overrideList['IDы']
idDataSections = overrideList['разделы']
end
local spriteStyle = ''
if dataSettings.url and dataSettings.url.style then
spriteStyle = dataSettings.url.style
end
local dataTitle = mw.title.new( 'Модуль:' .. dataPage )
-- Temporary until this is updated
local classname
if dataSettings["таблстилей"] then
classname = dataSettings["имякласса"] or
mw.ustring.lower( dataSettings["имя"]:gsub( ' ', '-' ) ) .. '-sprite'
end
local spritesheet = dataSettings["изобр"] or dataSettings["имя"] .. 'CSS.png'
local spriteTitle = mw.title.new( 'Файл:' .. spritesheet )
local body = mw.html.create( 'div' ):attr( {
id = 'spritedoc',
['data-dataprotection'] = h.getProtection( dataTitle, 'edit' ),
['data-datatimestamp'] = f:callParserFunction( 'REVISIONTIMESTAMP', 'Модуль:' .. dataPage ),
['data-datapage'] = 'Модуль:' .. dataPage,
['data-spritesheet'] = spritesheet,
['data-spriteprotection'] = h.getProtection( spriteTitle, 'upload', 'upload,reupload' ),
['data-urlfunc'] = "require( [[Модуль:Спрайт]] ).getUrl( '" .. spritesheet .. "', '$1', '" .. (classname or '') .. "' )",
['data-refreshtext'] = mw.text.nowiki( '{{#invoke:Документация спрайта|doc|' .. dataPage .. '|refresh=1}}' ),
['data-settings'] = mw.text.jsonEncode( data["настройки"] ),
} )
local sections = {}
for _, sectionData in ipairs( idDataSections or { ["назв"] = 'Некатегоризованные' } ) do
local sectionTag = body:tag( 'div' ):addClass( 'spritedoc-section' ):attr( 'data-section-id', sectionData.ID )
sectionTag:tag( 'h3' ):wikitext( sectionData["назв"] )
sections[sectionData.ID] = { boxes = sectionTag:tag( 'ul' ):addClass( 'spritedoc-boxes' ) }
end
local keyedData = h.sortIdData(idDataList)
local spriteArgs = { ["поз"] = -1, ["данные"] = data, nourl = spriteStyle ~= '' }
spriteArgs.default, spriteArgs.defaultStyle = h.getDefaults(dataSettings)
spriteArgs.className = classname
for _, data in ipairs( keyedData ) do
local idData = idDataList[data.name]
local pos = idData['поз']
spriteArgs['поз'] = pos
local section = sections[idData['раздел']]
if not section then
error(("У спрайта «%s» (позиция %d) указан некорректный ID раздела: %s"):format(data.name, pos, idData['раздел']))
end
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(spriteArgs) )
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['устарел'] 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