Modul:Grid

local p = {}

-- Individual cell
function p.cell( f )
 local args = f.args or f
 if f == mw.getCurrentFrame() and args[1] == nil then
 args = f:getParent().args
 end

 args[1] = mw.text.trim( args[1] or '' )

 -- Comment this next line out if you're not using aliases
 local aliases = mw.loadData( 'Modul:Grid/Aliases' )
 if aliases then
 local frames = {}
 for frame in mw.text.gsplit( args[1], '%s\*;%s\*' ) do
 local frameParts = p.getParts( frame )
 if aliases[frameParts.name] then
 local aliasFrames = {}
 for aliasFrame in mw.text.gsplit( aliases[frameParts.name], '%s\*;%s\*' ) do
 local aliasParts = p.getParts( aliasFrame )
 aliasParts.title = frameParts.title or aliasParts.title or ''
 aliasParts.mod = frameParts.mod or aliasParts.mod or ''
 aliasParts.num = frameParts.num or aliasParts.num or ''
 aliasParts.text = frameParts.text or aliasParts.text or ''

 aliasFrames[#aliasFrames + 1] = string.format( '[%s]%s:%s,%s[%s]', aliasParts.title, aliasParts.mod, aliasParts.name, aliasParts.num, aliasParts.text )
 end

 frames[#frames + 1] = table.concat( aliasFrames, ';' )
 else
 frames[#frames + 1] = frame
 end
 end

 args[1] = table.concat( frames, ';' )
 end

 local animated = args[1]:find( ';' )
 local pageName = mw.title.getCurrentTitle().subpageText
 local class = args.class or ''
 local imgClass = args.imgclass or ''
 local align = args.align or ''
 local cell = {}

 for frame in mw.text.gsplit( args[1], '%s\*;%s\*' ) do
 if frame == '' then
 if animated then
 table.insert( cell, '<span class="item"><br></span>' )
 else
 table.insert( cell, '<br>' )
 end
 else
 local parts = p.getParts( frame, args.mod )
 local tooltipTitle = parts.title
 local mod = parts.mod
 local name = parts.name
 local num = parts.num
 local tooltipDesc = parts.text

 local img
 if mod then
 img = name .. ' (' .. mod .. ')'
 else
 img = name
 end

 local link = args.link or ''
 if link == '' then
 if name == pageName then
 link = 'none'
 elseif mod then
 link = 'Mod/' .. mod
 else
 link = name
 end
 end

 local title = args.title or ''
 if title == '' then
 if tooltipTitle then
 title = tooltipTitle:gsub( '&[0-9a-fk-or]', '' )
 elseif link:lower() == 'none' or link ~= name:gsub( '%s%(.\*', '' ) then
 title = name:gsub( '%s%(.\*', '' )
 end
 end

 if not tooltipTitle and title:lower() == 'none' then
 tooltipTitle = 0
 end

 local alt = img
 if title:lower() ~= 'none' then
 alt = title
 end
 if mod then
 alt = mod .. ':' .. alt
 end

 if link:lower() == 'none' then
 link = ''
 end
 if title:lower() == 'none' then
 title = ''
 end
 if mod then
 title = mod .. ':' .. title
 end

 local tooltip = ''
 if tooltipTitle then
 tooltip = ' data-minetip-title="' .. tooltipTitle .. '"'
 end

 if tooltipDesc then
 tooltip = tooltip .. ' data-minetip-text="' .. tooltipDesc .. '"'
 end

 local image = {
 '<span class="item ' .. imgClass .. '" title="' .. title .. '"' .. tooltip .. '>',
 '[[Datei:Grid ' .. img .. '.png|32x32px|link=' .. link .. '|alt=' .. alt .. ']]',
 '</span>',
 }
 image[1] = image[1]:gsub( ' title=""', '' )
 image[2] = image[2]:gsub( '||', '|' )

 if num and num > 1 and num < 1000 then
 if link ~= '' then
 num = '[[' .. link .. '|' .. num .. ']]'
 end
 table.insert( image, 3, '<span class="number">' .. num .. '</span>' )
 end

 table.insert( cell, table.concat( image, '' ) )
 end
 end

 if animated then
 cell[1] = cell[1]:gsub( 'class="item', 'class="item active' )
 class = 'animated ' .. class
 end

 if align ~= '' then
 align = ' style="vertical-align:' .. align .. '"'
 end
 local html = {
 '<span class="grid ' .. class .. '"' .. align .. '>',
 table.concat( cell, '' ),
 '</span>'
 }

 if ( args.default or '' ) ~= '' then
 local defaultClass = ''
 if animated then
 defaultClass = ' skip'
 end
 table.insert( html, 2, '<span class="default-item' .. defaultClass .. '">[[Datei:Grid ' .. args.default .. '.png|32x32px|alt=|link=]]</span>' )
 end

 html = table.concat( html, '' ):gsub( ' "', '"' )
 return html
end

function p.getParts( frame, mod )
 local parts = {}
 parts.title = frame:match( '^%[%s\*([^%]]+)%s\*%]' )

 parts.mod = mw.text.trim( frame:match( '([^:%]]+):' ) or mod or '' )

 local vanilla = { v = 1, vanilla = 1, mc = 1, minecraft = 1 }
 if parts.mod == '' or vanilla[parts.mod:lower()] then
 parts.mod = nil
 end

 local nameStart = ( frame:find( ':' ) or frame:find( '%]' ) or 0 ) + 1
 if nameStart - 1 == #frame then
 nameStart = 1
 end
 parts.name = mw.text.trim( frame:sub( nameStart, ( frame:find( '[,%[]', nameStart ) or 0 ) - 1 ) )

 parts.num = math.floor( frame:match( ',%s\*(%d+)' ) or 0 )
 if parts.num == 0 then
 parts.num = nil
 end

 parts.text = frame:match( '%[%s\*([^%]]+)%s\*%]$' )

 return parts
end

--- GUI variants; called directly to avoid the overhead of a bunch of #invoke calls per GUI
-- Crafting table
function p.craftingTable( f )
 local args = f
 if f == mw.getCurrentFrame() then
 args = f:getParent().args
 end

 local arrow = 'Pfeil (klein)'
 local shapeless = ''
 local mirror = ''

 if args.arrow or '' ~= '' then
 arrow = args.arrow .. ' (' .. args.Mod .. ')'
 end
 if args.shapeless or '' ~= '' then
 shapeless = '<span title="Die Zutaten können in beliebiger Anordnung in den Craftingfeldern platziert werden.">[[Datei:Grid\_Layout\_Formlos.png|link=]]</span>'
 end
 if args.mirror or '' ~= '' then
 shapeless = '<span title="Die Zutaten können auch vertikal gespiegelt in den Craftingfeldern platziert werden.">[[Datei:Grid\_Layout\_Spiegelung.png|link=]]</span>'
 end

 local html = {
 '{| class="grid-Crafting\_Table" cellpadding="0" cellspacing="0"',
 '| ' .. p.cell{ args.A1, mod = args.Mod, link = args.A1link, title = args.A1title },
 '| ' .. p.cell{ args.B1, mod = args.Mod, link = args.B1link, title = args.B1title },
 '| ' .. p.cell{ args.C1, mod = args.Mod, link = args.C1link, title = args.C1title },
 '| rowspan="2" class="arrow" | [[Datei:Grid Layout ' .. arrow .. '.png|link=]]',
 '| rowspan="3" | ' .. p.cell{ args.Output, mod = args.Mod, link = args.Olink, title = args.Otitle, class = 'output' },
 '|-',
 '| ' .. p.cell{ args.A2, mod = args.Mod, link = args.A2link, title = args.A2title },
 '| ' .. p.cell{ args.B2, mod = args.Mod, link = args.B2link, title = args.B2title },
 '| ' .. p.cell{ args.C2, mod = args.Mod, link = args.C2link, title = args.C2title },
 '|-',
 '| ' .. p.cell{ args.A3, mod = args.Mod, link = args.A3link, title = args.A3title },
 '| ' .. p.cell{ args.B3, mod = args.Mod, link = args.B3link, title = args.B3title },
 '| ' .. p.cell{ args.C3, mod = args.Mod, link = args.C3link, title = args.C3title },
 '| class="shapeless" | ' .. shapeless,
 '|}'
 }

 return table.concat( html, '\n' );
end

-- Furnace
function p.furnace( f )
 local args = f
 if f == mw.getCurrentFrame() then
 args = f:getParent().args
 end
 args = require( 'Modul:ParameterUmwandeln' ).norm( args )

 local progress = 'Ofenfortschritt'
 local burning = ' (inaktiv)'
 local smelting = burning
 local fuelUsage = 'Feuer'

 if args.Progress then
 progress = args.Progress .. ' Progress'
 if args.Mod then
 progress = progress .. ' (' .. args.Mod .. ')'
 end
 end

 if args.Input and args.Fuel then
 burning = ''
 if args.Output then
 smelting = ''
 end
 end

 if args.FuelUsage then
 fuelUsage = args.FuelUsage
 if args.Mod then
 fuelUsage = fuelUsage .. ' (' .. args.Mod .. ')'
 end
 end

 local html = {
 '{| class="grid-Furnace" cellpadding="0" cellspacing="0"',
 '| ' .. p.cell{ args.Input, mod = args.Mod, link = args.Ilink, title = args.Ititle },
 '| rowspan="3" class="arrow" | [[Datei:Grid Layout ' .. progress .. smelting .. '.png|link=]]',
 '| rowspan="3" class="output" | ' .. p.cell{ args.Output, mod = args.Mod, link = args.Olink, title = args.Otitle, class = 'output' },
 '|-',
 '| [[Datei:Grid Layout ' .. fuelUsage .. burning .. '.png|link=]]',
 '|-',
 '| ' .. p.cell{ args.Fuel, mod = args.Mod, link = args.Flink, title = args.Ftitle },
 '|}'
 }

 return table.concat( html, '\n' );
end

-- Furnace (RedPower2)
function p.furnace\_rp2( f )
 local args = f
 if f == mw.getCurrentFrame() then
 args = f:getParent().args
 end
 args = require( 'Modul:ParameterUmwandeln' ).norm( args )

 local progress = 'Ofenfortschritt'
 local burning = ' (inaktiv)'
 local smelting = burning
 local fuelUsage = 'Feuer'
 local shapeless = ''

 if args.Progress then
 progress = args.Progress .. ' Progress'
 if args.Mod then
 progress = progress .. ' (' .. args.Mod .. ')'
 end
 end

 if args.Input and args.Fuel then
 burning = ''
 if args.Output then
 smelting = ''
 end
 end

 if args.FuelUsage then
 fuelUsage = args.FuelUsage
 if args.Mod then
 fuelUsage = fuelUsage .. ' (' .. args.Mod .. ')'
 end
 end

 if args.shapeless or '' ~= '' then
 shapeless = '<span title="Die Zutaten können in beliebiger Anordnung in den Craftingfeldern platziert werden.">[[Datei:Grid\_Layout\_Formlos.png|link=]]</span>'
 end

 local html = {
 '{| class="grid-Furnace" cellpadding="0" cellspacing="0"',
 '| ',
 '| style="width:16px;" |',
 '| ' .. p.cell{ args.A1, mod = args.Mod, link = args.A1link, title = args.A1title },
 '| ' .. p.cell{ args.B1, mod = args.Mod, link = args.B1link, title = args.B1title },
 '| ' .. p.cell{ args.C1, mod = args.Mod, link = args.C1link, title = args.C1title },
 '| rowspan="3" class="arrow" | [[Datei:Grid Layout ' .. progress .. smelting .. '.png|link=]]',
 '| rowspan="3" class="output" | ' .. p.cell{ args.Output, mod = args.Mod, link = args.Olink, title = args.Otitle, class = 'output' },
 '|-',
 '| [[Datei:Grid Layout ' .. fuelUsage .. burning .. '.png|link=]]',
 '| ',
 '| ' .. p.cell{ args.A2, mod = args.Mod, link = args.A2link, title = args.A2title },
 '| ' .. p.cell{ args.B2, mod = args.Mod, link = args.B2link, title = args.B2title },
 '| ' .. p.cell{ args.C2, mod = args.Mod, link = args.C2link, title = args.C2title },
 '|-',
 '| ' .. p.cell{ args.Fuel, mod = args.Mod, link = args.Flink, title = args.Ftitle },
 '| ',
 '| ' .. p.cell{ args.A3, mod = args.Mod, link = args.A3link, title = args.A3title },
 '| ' .. p.cell{ args.B3, mod = args.Mod, link = args.B3link, title = args.B3title },
 '| ' .. p.cell{ args.C3, mod = args.Mod, link = args.C3link, title = args.C3title },
 '| class="shapeless" | ' .. shapeless,
 '|}'
 }

 return table.concat( html, '\n' );
end

-- Brewing Stand
function p.brewingStand( f )
 local args = f
 if f == mw.getCurrentFrame() then
 args = f:getParent().args
 end
 args = require( 'Modul:ParameterUmwandeln' ).norm( args )

 local inactive = ' (inaktiv)'
 if args.Input and ( args.Output1 or args.Output2 or args.Output3 ) then
 inactive = ''
 end

 local html = {
 '<div class="grid-Brewing\_Stand">',
 '{| cellpadding="0" cellspacing="0"',
 '| class="fuel" | ' .. p.cell{ ';Lohenstaub', link='',default='Layout Lohenstaub leer' },
 '| class="coil" | [[Datei:Grid Layout Brauleitung.png|link=]]',
 '| class="input" | ' .. p.cell{ args.Input, mod = args.Mod, link = args.Ilink, title = args.Ititle },
 '| [[Datei:Grid Layout Braupfeil' .. inactive .. '.png|link=]]',
 '|-',
 '| ',
 '| ',
 '| class="output1" | ' .. p.cell{ args.Output1, mod = args.Mod, link = args.O1link, title = args.O1title, default = 'Layout Brauen (leer)' },
 '| class="output2" | ' .. p.cell{ args.Output2, mod = args.Mod, link = args.O2link, title = args.O2title, default = 'Layout Brauen (leer)' },
 '| class="output3" | ' .. p.cell{ args.Output3, mod = args.Mod, link = args.O3link, title = args.O3title, default = 'Layout Brauen (leer)' },
 '|-',
 '| ',
 '| ',
 '| class="paths" colspan="3" | [[Datei:Grid Layout Brauwege.png|link=]]',
 '|}',
 '</div>'
 }

 return table.concat( html, '\n' );
end

return p