Difference between revisions of "Module:Animated"

From Minetest Wiki
Jump to navigation Jump to search
m (Replaced newer mw.html with HtmlBuilder)
m (Replaced newer mw.text with string)
Line 23: Line 23:
 
     })
 
     })
  
     local input = mw.text.trim(args[1])
+
     local input = args[1]
     for name in mw.text.gsplit(input, '%s*,%s*') do
+
     for name in string.gmatch(input, '([^,]+)') do
 
         local span = createImageSpan()
 
         local span = createImageSpan()
 
         animatedSpan:node(span)
 
         animatedSpan:node(span)
  
         name = mw.text.trim(name)
+
         name = string.match(name, '^%s*(.-)%s*$')
 
         if name ~= '' then
 
         if name ~= '' then
 
             span:wikitext('[[File:', name)
 
             span:wikitext('[[File:', name)
Line 49: Line 49:
 
     })
 
     })
  
     local names = args[1] and mw.text.split(mw.text.trim(args[1]), '%s*,%s*') or {}
+
     local names = args[1] and string.gmatch(args[1], '([^,]+)') or {}
     local counts = args[2] and mw.text.split(mw.text.trim(args[2]), '%s*,%s*') or {}
+
     local counts = args[2] and string.gmatch(args[2], '([^,]+)') or {}
 
     for i, name in ipairs(names) do
 
     for i, name in ipairs(names) do
 
         local span = createImageSpan()
 
         local span = createImageSpan()
 
         animatedSpan:node(span)
 
         animatedSpan:node(span)
  
         name = mw.text.trim(name)
+
         name = string.match(name, '^%s*(.-)%s*$')
 
         if name ~= '' then
 
         if name ~= '' then
 
             span:wikitext('[[File:', name, '.png|32px|link=', name, ']]')
 
             span:wikitext('[[File:', name, '.png|32px|link=', name, ']]')

Revision as of 05:43, 9 April 2017

local p = {}

local HtmlBuilder = require('Module:HtmlBuilder') local getArgs = require('Module:Arguments').getArgs

function p.images(frame)

   local args = getArgs(frame, {inherited = true})
   return p._images(args)

end

function p.grid(frame)

   local args = getArgs(frame, {inherited = true})
   return p._grid(args)

end

function p._images(args)

   local size = args.size or args[2]
   local animatedSpan = p.createAnimatedSpan({
       size = size,
       background = args.background,
       border = args.border,
       padding = args.padding
   })
   local input = args[1]
   for name in string.gmatch(input, '([^,]+)') do
       local span = createImageSpan()
       animatedSpan:node(span)
       name = string.match(name, '^%s*(.-)%s*$')
       if name ~=  then
           span:wikitext('[[File:', name)
           if size then
               span:wikitext('|', size)
           end
           span:wikitext('|link=File:', name, ']]')
       end
   end
   return tostring(animatedSpan)

end

function p._grid(args)

   local animatedSpan = p.createAnimatedSpan({
       size = '32px',
       background = '#888',
       border = '1px solid #333',
       padding = args.padded and '10px' or nil
   })
   local names = args[1] and string.gmatch(args[1], '([^,]+)') or {}
   local counts = args[2] and string.gmatch(args[2], '([^,]+)') or {}
   for i, name in ipairs(names) do
       local span = createImageSpan()
       animatedSpan:node(span)
       name = string.match(name, '^%s*(.-)%s*$')
       if name ~=  then
           span:wikitext('32px')
           local count = counts[i]
           if count then
               span:node(
                   HtmlBuilder.create('span')
                       :css('position', 'relative')
                       :css('top', '-16px')
                       :css('left', '25px')
                       :css('font-weight', 'bold')
                       :css('color', 'white')
                       :css('text-shadow', '1px 1px black')
                       :wikitext(count))
           end
       end
   end
   return tostring(animatedSpan)

end

function p.createAnimatedSpan(args)

   local size = args.size
   local background = args.background
   local border = args.border
   local padding = args.padding
   local span = HtmlBuilder.create('span'):addClass('animated')
   if size then
       span:css('width', size)
       span:css('height', size)
   end
   if background then
       span:css('background-color', background)
   end
   if border then
       span:css('border', border)
   end
   if padding then
       span:css('padding', padding)
   end
   return span

end

function p.createImageSpan()

   return HtmlBuilder.create('span'):addClass('image')

end

return p