Advertisement
naista2002

Untitled

Aug 1st, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.05 KB | None | 0 0
  1. -- Документация
  2. function p.doc( f )
  3.     local args = f
  4.     if f == mw.getCurrentFrame() then
  5.         args = f.args
  6.     else
  7.         f = mw.getCurrentFrame()
  8.     end
  9.     local settingsPage = mw.text.trim( args[1] )
  10.     local settings = mw.loadData( 'Модуль:' .. settingsPage )
  11.     local idsPage = 'Модуль:' .. ( settings["IDы"] or settings["имя"] .. 'Спрайт/ID' )
  12.     local spriteargs = {}
  13.     for k, v in pairs(args) do
  14.         if type(k) ~= 'number' then
  15.             spriteargs[k] = v
  16.         end
  17.     end
  18.    
  19.     local getProtection = function( title, action, extra )
  20.         local protections = { 'edit' }
  21.         if extra then
  22.             table.insert( protections, extra )
  23.         end
  24.        
  25.         local addProtection = function( protection )
  26.             if protection == 'autoconfirmed' then
  27.                 protection = 'editsemiprotected'
  28.             elseif protection == 'sysop' then
  29.                 protection = 'editprotected'
  30.             end
  31.            
  32.             table.insert( protections, protection )
  33.         end
  34.        
  35.         local direct = title.protectionLevels[action]
  36.         for _, protection in ipairs( direct ) do
  37.             addProtection( protection )
  38.         end
  39.         local cascading = title.cascadingProtection.restrictions[action] or {}
  40.         if #cascading > 0 then
  41.             table.insert( protections, 'protect' )
  42.         end
  43.         for _, protection in ipairs( cascading ) do
  44.             addProtection( protection )
  45.         end
  46.        
  47.         return table.concat( protections, ',' )
  48.     end
  49.    
  50.     local body
  51.     if args.refresh then
  52.         body = mw.html.create( '' )
  53.     else
  54.         local idsTitle = mw.title.new( idsPage )
  55.         local spritesheet = settings["изобр"] or settings["имя"] .. 'CSS.png'
  56.         local spriteTitle = mw.title.new( 'Файл:' .. spritesheet )
  57.         local idsProtection = getProtection( idsTitle, 'edit' )
  58.         local spriteProtection = getProtection( spriteTitle, 'upload', 'upload,reupload' )
  59.         body = mw.html.create( 'div' ):attr( {
  60.             id = 'spritedoc',
  61.             ['data-idspage'] = idsTitle.id,
  62.             ['data-idsprotection'] = idsProtection,
  63.             ['data-idstimestamp'] = f:callParserFunction( 'REVISIONTIMESTAMP', idsPage ),
  64.             ['data-spritesheet'] = spritesheet,
  65.             ['data-spriteprotection'] = spriteProtection,
  66.             ['data-pos'] = settings["поз"] or 1,
  67.             ['data-refreshtext'] = mw.text.nowiki( '{{#invoke:Спрайт|doc|' .. settingsPage .. '|обновить=1}}' )
  68.         } )
  69.     end
  70.    
  71.     local data = mw.loadData( idsPage )
  72.    
  73.     local sections = {}
  74.     for _, sectionData in ipairs( data["разделы"] or {"Некатегоризованные"} ) do
  75.         local sectionTag = body:tag( 'div' ):addClass( 'spritedoc-section' ):attr( 'data-section-id', sectionData["ID"] )
  76.         -- https://phabricator.wikimedia.org/T73594
  77.         sectionTag:wikitext( '<h3>', sectionData[1], '</h3>' )
  78.         sections[sectionData["ID"]] = { boxes = sectionTag:tag( 'ul' ):addClass( 'spritedoc-boxes' ) }
  79.     end
  80.    
  81.     local keyedData = {}
  82.     for name, idData in pairs( data["IDы"] ) do
  83.         table.insert( keyedData, {
  84.             ["ключ"] = mw.ustring.lower( name ),
  85.             ["имя"] = name,
  86.             ["данные"] = idData
  87.         } )
  88.     end
  89.     table.sort( keyedData, function( a, b )
  90.         return a["ключ"] < b["ключ"]
  91.     end )
  92.    
  93.     for _, data in ipairs( keyedData ) do
  94.         local idData = data["данные"]
  95.         local pos = idData["поз"]
  96.         local section = sections[idData["раздел"]]
  97.         local names = section[pos]
  98.         if not names then
  99.             local box = section.boxes:tag( 'li' ):addClass( 'spritedoc-box' ):attr( 'data-pos', pos )
  100.             local lspriteargs = mw.clone(spriteargs)
  101.             lspriteargs["поз"] = pos
  102.             lspriteargs["настройки"] = settingsPage
  103.            
  104.             box:tag( 'div' ):addClass( 'spritedoc-image' )
  105.                 :wikitext( p.base(lspriteargs) )
  106.            
  107.             names = box:tag( 'ul' ):addClass( 'spritedoc-names' )
  108.             section[pos] = names
  109.         end
  110.         local nameElem = mw.html.create( 'li' ):addClass( 'spritedoc-name' )
  111.         local codeElem = nameElem:tag( 'code' ):wikitext( data["имя"] )
  112.        
  113.         if idData["устарел"] then
  114.             codeElem:addClass( 'spritedoc-deprecated' )
  115.         end
  116.         names:wikitext( tostring( nameElem ) )
  117.     end
  118.    
  119.     if args["обновить"] then
  120.         return '', tostring( body )
  121.     end
  122.     return f:callParserFunction( '#widget:Stylesheet', { page = 'SpriteDoc' } ), tostring( body )
  123. end
  124.  
  125. return p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement