Difference between revisions of "Module:Animated"

From Minetest Wiki
Jump to navigation Jump to search
m
m
Line 80: Line 80:
 
     local counts = {}
 
     local counts = {}
 
     if args[1] then
 
     if args[1] then
         string.gsub(args[1], "([^,]+)", function(name) names[#names + 1] = name end)
+
         string.gsub(args[1], '([^,]+)', function(name) names[#names + 1] = name end)
 
     end
 
     end
 
     if args[2] then
 
     if args[2] then
         string.gsub(args[2], "([^,]+)", function(count) counts[#counts + 1] = count end)
+
         string.gsub(args[2], '([^,]+)', function(count) counts[#counts + 1] = count end)
 
     end
 
     end
 
     for i, name in ipairs(names) do
 
     for i, name in ipairs(names) do

Revision as of 15:13, 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

local function createAnimatedDiv(args)

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

end

local function createImageDiv()

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

end

function p._images(args)

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

end

function p._grid(args)

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

end

return p