Module:ForceAnimate

Jump to navigation Jump to search
Documentation[create] [refresh]
This module has no documentation. If you know how to use this module, please create it.
local p = {}
function p.base(f)
    local args = f
    if f == mw.getCurrentFrame() then
        args = require('Module:ProcessArgs').merge(true)
    else
        f = mw.getCurrentFrame()
    end

    local cssText = {}
    local file = ''
    local link = args['link']
    local class = args['class']
    local option = {}
    local caption = ''

    -- Image argument parser --
    local match_patterns = {
        ['^([0-9]+)x([0-9]+)px$'] = 'WxH',
        ['^([0-9]+)px$'] = 'width',
        ['^x([0-9]+)px$'] = 'height',
        ['^right$'] = 'right',
        ['^left$'] = 'left',
        ['^center$'] = 'center',
        ['^none$'] = 'none',
        ['^thumb$'] = 'thumb',
        ['^frame$'] = 'frame',
    }
    local function parse_args(value)
        local match = false

        for p, t in pairs(match_patterns)
        do
            local m, n = string.match(value, p)

            if m then
                if t == "WxH" and n then
                    option['width'] = m
                    option['height'] = n
                elseif t == "width" then
                    option['width'] = m
                elseif t == "height" then
                    option['height'] = m
                elseif string.find(" left right ", m) then
                    option['float'] = m
                elseif t == "frame" then
                    option['frame'] = true
                    option['thumb'] = true
                else
                    option[m] = true
                end

                match = true
            end
        end

        return match
    end

    -- Parse arguments --
    for k, v in pairs(args)
    do
        local num = tonumber(k)
        if num then
            if num == 1 then
                file = v
            else
                if not parse_args(v) then
                    caption = v
                end
            end
        end
    end

    if file == '' then
        return ''
    end

    if option['thumb'] and not option['width'] and not option['height'] then
        if option['frame'] then
            option['width'] = '300'
        else
            option['width'] = '180'
        end
    end

    if option['width'] then
        cssText.width = tostring(option['width']) .. 'px'
    end
    if option['height'] then
        cssText.height = tostring(option['height']) .. 'px'
    end

    if not option['thumb'] then
        if option['float'] then
            cssText.float = option['float']
        elseif option['center'] then
            cssText.display = 'table'
            cssText.margin = '0 auto'
        elseif option['none'] then
            cssText.display = 'block'
            cssText.margin = 'auto'
        else
            cssText.display = 'inline-block'
        end
    end

    -- Generate HTML --
    local div = mw.html.create((option['float'] or option['thumb']) and 'div' or 'span')
        :addClass('nothumb')

    if option['none'] then
        div:addClass('floatnone')
    end

    local wikitext = '[[File:' .. mw.text.trim(file)

    if link then
        wikitext = wikitext .. '|link=' .. link
    end

    if class then
        wikitext = wikitext ..
        '|class=' .. class
    end

    wikitext = wikitext .. ']]'

    if option['thumb'] then
        div:addClass('thumb')

        if option['float'] == 'left' then
            div:addClass('tleft')
        else
            div:addClass('tright')
        end

        local figure = div:tag('div')
        figure:addClass('thumbinner')
        figure:css('display', "block")

        local figimage = figure:tag('div')
        figimage:addClass('thumbimage')
        figimage:wikitext(wikitext)
        cssText.display = "block"
        figimage:css(cssText)

        local figcaption = figure:tag('div')
        figcaption:addClass('thumbcaption')

        -- Magnify icon --
        if option['thumb'] and not option['frame'] then
            local magnify = figcaption:tag('div')
            magnify:addClass('magnify')
            magnify:wikitext('[[:File:' .. mw.text.trim(file) .. ']]')
        end

        -- Caption --
        figcaption:wikitext(caption)
    else
        div:wikitext(wikitext)
        div:css(cssText)
    end

    return tostring(div)
end

return p