Module:SpriteDoc

local util_args = require('Module:ArgsUtil') local util_table = require('Module:TableUtil') local util_text = require('Module:TextUtil') local Sprite = require('Module:Sprite')._main local p = {} local h = {}

function p.main(frame) local DROPDOWN_TOGGLE_OPTIONS = { 'deprecated', 'nolink', 'black', 'dark' } local args = util_args.merge(true) local sprite_type = args[1] local dataPage = sprite_type .. 'Sprite' local data = mw.loadData('Module:' .. dataPage) local body = h.getDocBody(data, sprite_type, dataPage) 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 = name:lower, 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 or 1] 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( Sprite({ data.name, type = sprite_type, notext = true, nolink = true, nourl = false }) ) 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 ) for _, v in ipairs(DROPDOWN_TOGGLE_OPTIONS) do			if idData[v] then codeElem:addClass('spritedoc-' .. v)			end end names:wikitext( tostring( nameElem ) ) end return body, '' end

function h.getDocBody(data, sprite_type, dataPage) local spritesheet = data.settings.image or data.settings.name .. 'Sprite.png' local dataTitle = mw.title.new( 'Module:' .. dataPage ) local spriteTitle = mw.title.new( 'File:' .. spritesheet ) local f = mw.getCurrentFrame local body = mw.html.create('div'):attr({		id = 'spritedoc',		['data-dataprotection'] = h.getProtection( dataTitle, 'edit' ),		['data-datatimestamp'] = f:callParserFunction( 'REVISIONTIMESTAMP', 'Module:' .. dataPage ),		['data-datapage'] = 'Module:' .. dataPage,		['data-spritesheet'] = spritesheet,		['data-spriteprotection'] = h.getProtection( spriteTitle, 'upload', 'upload,reupload' ),		['data-urlfunc'] = "require( Module:Sprite ).getUrl( '" .. spritesheet .. "', '$1' )",		['data-refreshtext'] = mw.text.nowiki( '' ),		['data-settings'] = mw.text.jsonEncode( data.settings ),	}) return body 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

return p