Module:Sprite: Difference between revisions
Actually support not showing text, rather than hacking it with an empty string |
Update to support merging settings and IDs module into a single data module, and specifying an inline URL in the settings for instant cache busting. The sprite documentation will be broken until the modules are merged. |
||
| Line 7: | Line 7: | ||
f = mw.getCurrentFrame() | f = mw.getCurrentFrame() | ||
end | end | ||
local data = args.data and mw.loadData( 'Module:' .. args.data ) or {} | |||
local settings = data.settings or args.settings and mw.loadData( 'Module:' .. args.settings ) | |||
settings = settings and settings.settings or settings | |||
-- Default settings | -- Default settings | ||
| Line 18: | Line 22: | ||
local defaultStyle = default | local defaultStyle = default | ||
if | if settings then | ||
if not settings.stylesheet then | if not settings.stylesheet then | ||
-- Make a separate clone of the current default settings | -- Make a separate clone of the current default settings | ||
| Line 41: | Line 44: | ||
local styles = {} | local styles = {} | ||
if setting( 'url' ) then | |||
styles[#styles + 1] = 'background-image:' .. setting( 'url' ) | |||
end | |||
if setting( 'stylesheet' ) then | if setting( 'stylesheet' ) then | ||
sprite:addClass( | sprite:addClass( | ||
| Line 46: | Line 52: | ||
mw.ustring.lower( setting( 'name' ):gsub( ' ', '-' ) ) .. '-sprite' | 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' | setting( 'image' ) or setting( 'name' ) .. 'Sprite.png' | ||
) | |||
end | end | ||
local class = setting( 'class' ) | local class = setting( 'class' ) | ||
| Line 57: | Line 63: | ||
local size = setting( 'size' ) | local size = setting( 'size' ) | ||
local sheetWidth = setting( 'sheetsize' ) | local sheetWidth = setting( 'sheetsize' ) | ||
local tiles = sheetWidth / size | local tiles = sheetWidth / size | ||
local | local pos = setting( 'pos' ) - 1 | ||
local scale = setting( 'scale' ) | local scale = setting( 'scale' ) | ||
local autoScale = setting( 'autoscale' ) | local autoScale = setting( 'autoscale' ) | ||
if left | 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 | end | ||
if not autoScale and scale ~= defaultStyle.scale then | if not autoScale and scale ~= defaultStyle.scale then | ||
styles[#styles + 1] = 'background-size:' .. sheetWidth * scale .. 'px auto' | |||
end | end | ||
if size ~= defaultStyle.size or ( not autoScale and scale ~= defaultStyle.scale ) then | 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 | end | ||
local align = setting( 'align' ) | local align = setting( 'align' ) | ||
if align ~= defaultStyle.align then | if align ~= defaultStyle.align then | ||
styles[#styles + 1] = 'vertical-align:' .. align | |||
end | end | ||
styles[#styles + 1] = setting( 'css' ) | |||
sprite:cssText( table.concat( styles, ';' ) ) | sprite:cssText( table.concat( styles, ';' ) ) | ||
| Line 126: | Line 135: | ||
end | end | ||
local default = args.settings and mw.loadData( 'Module:' .. args.settings ) | |||
local data = args.data and mw.loadData( 'Module:' .. args.data ) or {} | |||
default = default and default.settings or default or data.settings or {} | |||
local categories = {} | local categories = {} | ||
local idData = args.iddata | local idData = args.iddata | ||
if not idData then | if not idData then | ||
local name = args.name or default.name | local name = args.name or default.name | ||
local ids = mw.loadData( 'Module:' .. ( args.ids or default.ids or name .. '/IDs' ) ) | local ids = data.ids or mw.loadData( 'Module:' .. ( args.ids or default.ids or name .. '/IDs' ) ).ids | ||
local id = mw.text.trim( tostring( args[1] or '' ) ) | local id = mw.text.trim( tostring( args[1] or '' ) ) | ||
idData = ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )] | idData = ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )] | ||
| Line 145: | Line 151: | ||
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 and not disallowCats then | |||
categories[#categories + 1] = f:expandTemplate{ title = 'Translation category', args = { 'Pages using deprecated sprite names', project = 0 } } | |||
end | end | ||
args.pos = idData.pos | |||
elseif not disallowCats then | elseif not disallowCats then | ||
categories[#categories + 1] = f:expandTemplate{ title = 'Translation category', args = { 'Pages with missing sprites', project = 0 } } | |||
end | end | ||
return p.base( args ), table.concat( categories | return p.base( args ), table.concat( categories ) | ||
end | end | ||
| Line 181: | Line 183: | ||
return p.sprite( args ) | return p.sprite( args ) | ||
end | |||
function p.getUrl( spritesheet, query ) | |||
return mw.getCurrentFrame():expandTemplate{ title = 'FileUrl', args = { spritesheet, query = query } } | |||
end | end | ||
| Line 190: | Line 196: | ||
f = mw.getCurrentFrame() | f = mw.getCurrentFrame() | ||
end | end | ||
local | local dataPage = mw.text.trim( args[1] ) | ||
local | local data = mw.loadData( 'Module:' .. dataPage ) | ||
local getProtection = function( title, action, extra ) | local getProtection = function( title, action, extra ) | ||
local protections = { 'edit' } | local protections = { 'edit' } | ||
if extra then | if extra then | ||
protections[#protections + 1] = extra | |||
end | end | ||
| Line 207: | Line 212: | ||
end | end | ||
protections[#protections + 1] = protection | |||
end | end | ||
| Line 216: | Line 221: | ||
local cascading = title.cascadingProtection.restrictions[action] or {} | local cascading = title.cascadingProtection.restrictions[action] or {} | ||
if #cascading > 0 then | if #cascading > 0 then | ||
protections[#protections + 1] = 'protect' | |||
end | end | ||
for _, protection in ipairs( cascading ) do | for _, protection in ipairs( cascading ) do | ||
| Line 225: | Line 230: | ||
end | end | ||
local | local dataTitle = mw.title.new( 'Module:' .. dataPage ) | ||
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', 'Module:' .. dataPage ), | |||
['data-datapage'] = 'Module:' .. dataPage, | |||
['data-spritesheet'] = spritesheet, | |||
['data-spriteprotection'] = spriteProtection, | |||
['data-urlfunc'] = "require( [[Module:Sprite]] ).getUrl( '" .. spritesheet .. "', '$1' )", | |||
['data-refreshtext'] = mw.text.nowiki( '{{#invoke:sprite|doc|' .. dataPage .. '|refresh=1}}' ), | |||
['data-settings'] = mw.text.jsonEncode( data.settings ), | |||
} ) | |||
local sections = {} | local sections = {} | ||
for _, sectionData in ipairs( data.sections or { 'Uncategorized' } ) do | for _, sectionData in ipairs( data.sections or { name = 'Uncategorized' } ) do | ||
local sectionTag = body:tag( 'div' ):addClass( 'spritedoc-section' ):attr( 'data-section-id', sectionData.id ) | local sectionTag = body:tag( 'div' ):addClass( 'spritedoc-section' ):attr( 'data-section-id', sectionData.id ) | ||
sectionTag:tag( 'h3' ):wikitext( sectionData.name ) | |||
sectionTag: | |||
sections[sectionData.id] = { boxes = sectionTag:tag( 'ul' ):addClass( 'spritedoc-boxes' ) } | sections[sectionData.id] = { boxes = sectionTag:tag( 'ul' ):addClass( 'spritedoc-boxes' ) } | ||
end | end | ||
local keyedData = {} | local keyedData = {} | ||
local i = 1 | |||
for name, idData in pairs( data.ids ) do | for name, idData in pairs( data.ids ) do | ||
keyedData[i] = { | |||
sortKey = mw.ustring.lower( name ), | sortKey = mw.ustring.lower( name ), | ||
name = name, | name = name, | ||
data = idData | data = idData | ||
} | } | ||
i = i + 1 | |||
end | end | ||
table.sort( keyedData, function( a, b ) | table.sort( keyedData, function( a, b ) | ||
| Line 276: | Line 276: | ||
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, | :wikitext( p.base{ pos = pos, data = dataPage } ) | ||
names = box:tag( 'ul' ):addClass( 'spritedoc-names' ) | names = box:tag( 'ul' ):addClass( 'spritedoc-names' ) | ||
| Line 293: | Line 293: | ||
return '', tostring( body ) | return '', tostring( body ) | ||
end | end | ||
return f:callParserFunction( '#widget: | return f:callParserFunction( '#widget:SpriteDoc.css' ), tostring( body ) | ||
end | end | ||
return p | return p | ||