Module:Sandbox/Chao

From BlazBlue Wiki
< Module:Sandbox
Revision as of 00:22, 1 August 2019 by Chao (talk | contribs) (save function for dumping tables to console)

Script error: The function "main" does not exist.


--[[
simulate passing a frame to the function in the console with:
myFrame = { args = { '1', '2', namedparam = "value" } }
=p.BBRadio ( myFrame )
--]]

local p = {}
local cargo = mw.ext.cargo

function p.BBRadio( frame )
	intro = frame.args['intro'] or ''
	intro = html_escape(intro)
	
	limit = frame.args['limit'] or 100
	
	link_parts = {
		'tables=Files,BBRadio_Cuts',
		'fields=Files._pageName, Files.Caption',
		'where=Files.Characters HOLDS "' .. html_escape(frame.args[1]) .. '" AND BBRadio_Cuts.Season="' .. frame.args[2] .. '"',
		'join_on=Files._pageName=BBRadio_Cuts._pageName',
		'order_by=_pageName',
		'format=gallery',
		'offset=0',
		'limit=' .. limit,
		'default=',
		'intro=' .. intro,
		'show bytes=0',
		'show dimensions=0',
		'show filename=0',
		'caption field=Caption',
		'image width=250'
	}
	link = html_escape(concatvalues(link_parts,"&"))
	
	parts = {
		'https://blazblue.wiki/index.php\?title=Special:ViewData&',
		link,
		'">'
	}
	final_link = concatvalues(parts,"")
	mw.log(final_link)
	return final_link
end

-- concat all the strings in table s together with the given delimiter
function concatvalues(s,delimiter)
    local t = { }
    for k,v in ipairs(s) do
        t[#t+1] = tostring(v)
    end
    return table.concat(t,delimiter)
end

function html_escape(s)
    return (string.gsub(s, "[<>\"'/ ,]", {
        ["<"] = "%3C",
        [">"] = "%3E",
        ['"'] = "%22",
        ["'"] = "%27",
        ["/"] = "%2F",
        [" "] = "+",
        [","] = "%2C"
    }))
end

-- helpful for printing tables
function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. dump(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end

return p