Module:Sandbox/BotAntony/NavboxBeautifier

local p = {}

local tr = require('Module:Tr').translate local cargo = mw.ext.cargo

local match = string.match local sub = string.sub local trim = mw.text.trim

local lang = 'en' local maxSizeConst = '32px' local result = {} local leftpadCounter = 0

--== Images ==-- local function explode(div,str) if (div == '') then return false end local pos, arr = 0, {} for st,sp in function return string.find(str, div, pos, true) end do		arr[#arr + 1] = trim(string.sub(str, pos, st-1)) pos = sp + 1 end arr[#arr + 1] = trim(string.sub(str, pos)) return arr end

local function getInfoFromCargo(imagename) -- try to get from cargo cache local result = mw.ext.cargo.query('Imageinfo', 'width, height, cached', {		-- escape apostrophes in the imagename input		where = 'image='.. "'"..imagename:gsub("'", "\\'"):gsub("&#39;", "\\'").."'",		orderBy = "cached DESC",		limit = 1,	}) for _, row in ipairs(result) do		return tonumber(row['width']), tonumber(row['height']), row['cached'] end end

local function getInfoFromImageName(imagename) -- don't cache 's result when parsing imagesize fails. local imageTitle = mw.title.new("File:" .. imagename) local width, height = imageTitle.file.width, imageTitle.file.height return width, height end

local function getSizeInfo(imagename, maxsize) local width, height, cached = getInfoFromCargo(imagename) -- cache missed, init cache if not cached then width, height = getInfoFromImageName(imagename) end if width == 0 then width = nil end if height == 0 then height = nil end return width, height end

local function getImageSize(imagename, width, height, scale, maxwidth, maxheight) -- get size info from image file itself (may be expensive) local w, h = getSizeInfo(imagename) -- store data to cache

-- if width and height are not given as input, but scale/maxwidth/maxheight are, then -- set width and height to the original dimensions of the image if not width and not height and (scale or maxwidth or maxheight) then width, height = w, h	end

-- apply scale to width/height if needed if scale then if width then width = width * scale end if height then height = height * scale end end

-- apply maxwidth/maxheight if maxwidth then if width then if width > maxwidth then width = maxwidth end else if height then width = maxwidth end end end if maxheight then if height then if height > maxheight then height = maxheight end else if width then height = maxheight end end end

-- round to natural numbers if width then width = math.ceil(width) end if height then height = math.ceil(height) end

return width, height end

local function parseMaxSize(maxsize) if not maxsize then return end local maxwidth, maxheight = unpack(explode('x', string.gsub(maxsize, 'px', ''))) maxwidth, maxheight = tonumber(maxwidth), tonumber(maxheight) if maxwidth == 0 then maxwidth = nil end if maxheight == 0 then maxheight = nil end return maxwidth, maxheight end --== End ==--

local function formatLink(link) local result = "" if match(link, "%[%[.*%]%]") or match(link, "%{%{.*%}%}") then return link else local result = {} -- "link|text&file#size" local text = link text, result["size"] = unpack(explode("%", text)) text, result["image"] = unpack(explode("&", text)) result[1], result[2] = unpack(explode("|", text)) if not result["image"] or result["image"] == '' then result["image"] = result[1] .. '.png' end if not result["size"] or result["size"] == '' or sub(result["size"], -2, -1) == '*1' then local maxwidth, maxheight = parseMaxSize(maxSizeConst) local width, height = getImageSize(result["image"], nil, nil, 1, maxwidth, maxheight) result["size"] = (width or '32') .. 'x' .. (height or '32') .. 'px*1' -- '*1' is needed to detect which sizes were added by beautifier end if not result[2] or result[2] == "" then result[2] = tr(result[1], lang) end result["lang"] = lang return result[1] .. '|' .. result[2] .. '&' .. result['image'] .. '#' .. result['size'] end end

local leftpadCache = { [0] = '' }

local function leftpad(str, num) local cached = '' if leftpadCache[num] then return leftpadCache[num] .. str else for i = 1, num do cached = cached .. '	'		end leftpadCache[num] = cached return cached .. str end end

local function getList(list) for i = 1, #list do		if type(list[i]) == "table" then result[#result + 1] = leftpad('{', leftpadCounter) leftpadCounter = leftpadCounter + 1 getList(list[i]) leftpadCounter = leftpadCounter - 1 result[#result + 1] = leftpad('},', leftpadCounter) else result[#result + 1] = leftpad('"' .. formatLink(list[i]) .. '",', leftpadCounter) end end end

local function getH1(list) if list[1]['title'] then for i = 1, #list do			result[#result + 1] = leftpad('{', leftpadCounter) leftpadCounter = leftpadCounter + 1 result[#result + 1] = leftpad('["title"] = "' .. list[i]["title"] .. '",', leftpadCounter) getList(list[i]) leftpadCounter = leftpadCounter - 1 result[#result + 1] = leftpad('},', leftpadCounter) end else getList(list) end end

local function getHeader(data) for i = 1, #data do		if data[i]['__metadata'] then result[#result + 1] = leftpad('{', leftpadCounter) leftpadCounter = leftpadCounter + 1 result[#result + 1] = leftpad('["__metadata"] = "' .. data[i]['__metadata'] .. '",', leftpadCounter) result[#result + 1] = leftpad('["header"] = "' .. data[i]['header'] .. '",', leftpadCounter) getHeader(data[i]) leftpadCounter = leftpadCounter - 1 result[#result + 1] = leftpad('},', leftpadCounter) elseif data[i]['h1'] then result[#result + 1] = leftpad('{', leftpadCounter) leftpadCounter = leftpadCounter + 1 result[#result + 1] = leftpad('["h1"] = "' .. data[i]['h1'] .. '",', leftpadCounter) getH1(data[i]) leftpadCounter = leftpadCounter - 1 result[#result + 1] = leftpad('},', leftpadCounter) else formatLink(data[i]) end end end

local data = require('Module:Navbox/Weapons')

function p.test result = {} result[#result + 1] = leftpad('{', leftpadCounter) leftpadCounter = leftpadCounter + 1 result[#result + 1] = leftpad('["__metadata"] = "' .. data['__metadata'] .. '",', leftpadCounter) result[#result + 1] = leftpad('["header"] = "' .. data['header'] .. '",', leftpadCounter) getHeader(data) leftpadCounter = leftpadCounter - 1 result[#result + 1] = leftpad('}', leftpadCounter) mw.log(table.concat(result, '\n')) end

return p