Jump to content

Module:Class figure

From Wikisource
local getArgs = require('Module:Arguments').getArgs
local p = {}

local conf = {
    float = {
        ['left'] = 'float-left',
        ['right'] = 'float-right',
    },
    missingImg = '<span style="background-color: #f8f9fa; color:#202122; border: 1px solid #a2a9b1; width: 100%; text-align: center;">Image missing</span>'
}

local function imageFill(args, frame)
    local details = ""
    
    if args['image-size'] then
        details = details .. '|' .. args['image-size']
    end
    if args['image-alt'] then
        details = details .. '|alt=' .. args['image-alt']
    end
    
    if args['image'] then
        return '[[File:' .. args['image'] .. details .. ']]'
    else
    	return conf.missingImg
        --return frame:expandTemplate{ title = 'missing image' }
    end
end

local function captionFill(args)
    local str = ""
    
    if args.num or args.nums then
    	str = '<span class="wst-figure-number">'
    	if args.num then
    		str = str .. 'Fig. ' .. args.num
    	else
    		str = str .. 'Figs. ' .. args.nums
    	end
    	str = str .. '.</span>'
    end
    if (args.num or args.nums) and args.caption then
    	str = str .. '<span class="wst-figure-number-separator"></span>'
    end
    if args.caption then
    	str = str .. args.caption
    end
    return str
end

function p.main(frame)
    local args = getArgs(frame)
    local tag = args.tag or 'div'
    local num = args.num or args.nums

    -- wrapper
    local wrapper = mw.html.create(tag)
    wrapper:addClass("wst-figure-wrapper")
    wrapper:cssText(args.style)
    if args.width then
        wrapper:css("width", args.width)
    end
    if args.float then
        wrapper:addClass(conf.float[args.float])
    end
    if args.class then
        wrapper:addClass(args.class)
    end
    
    -- anchor
    if args.anchor then
    	wrapper:attr("id", args.anchor)
    elseif num then
    	wrapper:attr("id", 'Fig.' .. num)
    end
    
    -- image
    wrapper:tag(tag):addClass("wst-figure-image")
    	:wikitext(imageFill(args, frame))
    
    -- source
    if args.source then
    	wrapper:tag(tag):addClass("wst-figure-source")
                :wikitext(args.source)
    end
    
    -- caption
    if num or args.caption then
    	wrapper:tag(tag):addClass("wst-figure-caption")
                :wikitext(captionFill(args))
    end
    
    -- caption2
    if args.caption2 then
    	wrapper:tag(tag):addClass("wst-figure-caption2")
    	if args.tag == 'div' then
    		wrapper:newline() -- allows for lists
    	end
    	wrapper:wikitext(args.caption2)
    end
    
    -- key
    if args.key then
    	wrapper:tag(tag):addClass("wst-figure-key")
    	if args.tag == 'div' then
    		wrapper:newline() -- allows for lists
    	end
    	wrapper:wikitext(args.key)
    end
    
    return tostring(wrapper)
end

return p