Jump to content

Module:Badge display

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BoonDock (talk | contribs) at 12:58, 19 April 2023 (// Edit via Wikitext Extension for VSCode). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('strict')
local p = {}
local data = mw.loadData('Module:BadgeDisplay/data')
local getArgs = require('Module:Arguments').getArgs
local GlobalTemplateArgs = '' -- define as global (to this module) variable


function p.DisplayImage(frame)
    -- Usage from a template etc:
    -- {{#invoke:BadgeDisplay|DisplayImage
    -- | image = NameOfImage.jpg
    -- | caption = Caption for the image
    -- | description = Description of the image
    -- | float = left/right/center
    -- | width = width in pixels or other units of the table
    -- | size = size in pixels or other units, e.g. "x25px"
    -- }}

    local args = getArgs(frame)

    -- Set default values for optional parameters
    local float = args.float or 'none'
    local size = args.size or 'x200px'
	local imageName = args.image or "Ribbon - Question mark.png"
    local width = args.width or ''
    local code =''
    code = args[1] -- Taking a risk. Set code to first unnamed param but then overwriting it if code is named
    code =  args.code
    if not code or '' == code then
		-- return '<span style="color:#d33">Error: missing positional parameter 1</span>'
		return '<span style="color:#d33"> (Invalid Code: <b>' .. code .. '</b> )</span> [[Category:BadgeDisplay Error]]'
	end

--	local code = code:upper() -- DO NOT DO THIS. Code is case sensitive -- Converts the first argument to uppercase
	if not data[code] then
		-- return '<span style="color:#d33">Error: invalid positional parameter 1: ' .. code .. '</span>'
		return '<span style="color:#d33"> (Code not found: <b>' .. code .. '</b> )</span> [[Category:BadgeDisplay Error]]'
	end

    -- {Code = "xx", Type = "", Description = "xx", Class="", Variation="", Image="XX", PageLink="XX",  Country="ZAR", Note="", Org="SANDF", RecipCat="" }
    -- Data from the record into local variables
    local Code = data[code].Code
    local Type = data[code].Type
    local Description = data[code].Description
    local Class = data[code].Class
    local Variation = data[code].Variation
    local Image = data[code].Image
    local PageLink = data[code].PageLink
    local Country = data[code].Country
    local Note = data[code].Note
    local Org = data[code].Org

    
    -- Build the wikitext table
    local wikitext=''
    -- if width then 
    --    wikitext = wikitext .. '{| class="wikitable" style="width:' .. (tonumber(width) * 1.1) .. 'px; margin:0 auto;border:none;"\n'
    -- else
        wikitext = wikitext .. '{| class="wikitable" style="width:250px; margin:0 auto;border:none;"\n'
    -- end
    wikitext = wikitext .. '|+ [[' .. PageLink .. "|" .. Description .. ']]\n|-\n'
    --wikitext = wikitext .. '|caption style="text-align:center;"|' .. (args.caption or 'No Caption') .. '\n|-\n'
    wikitext = wikitext .. '|style="text-align:center;border:none;"| [[File:' .. Image .. '|' .. size .. ']]\n|-\n'
    --wikitext = wikitext .. '|style="text-align:center;"| [[File:' .. (args.image or 'No Image') .. ']]\n|-\n'
    wikitext = wikitext .. '|style="text-align:center;"|' .. Description .. " <br />" .. Class .. " <br />" .. Variation .. '\n|-\n'
    wikitext = wikitext .. '|}\n'
    wikitext = wikitext .. '=== Debug === \n'
    wikitext = wikitext .. '* float: ' .. float .. '\n'
    wikitext = wikitext .. '* width: ' .. width .. '\n'
    wikitext = wikitext .. '* size: ' .. size .. '\n'
    wikitext = wikitext .. '* imageName: ' .. imageName .. '\n'

    return wikitext
end

return p