Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 17:27, 8 March 2026 by Muqsit (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:CLabel/doc

local p = {}
local mArguments

function p.main(frame)
    mArguments = require('Module:Arguments')
    local args = mArguments.getArgs(frame)
    return p._main(args)
end

function p._main(args)
    -- Get current frame for extensionTag
    local frame = mw.getCurrentFrame()

    -- Determine game parameter with fallbacks
    local game = args.game
    if not game or game == '' then
        if args.chara and args.chara ~= '' then
            game = args[1] or ''
        else
            game = args[2] and args[1] or mw.title.getCurrentTitle().rootText
        end
    end

    -- Determine chara parameter with fallbacks
    local chara = args.chara
    if not chara or chara == '' then
        chara = args[2] or args[1] or mw.title.getCurrentTitle():getText():match("/([^/]+)$")
    end

    -- Generate link
    local link = ''
    if args.linkOverride or args.link then
        local linkVal = args.linkOverride or args.link
        if linkVal ~= 'none' and linkVal ~= '' then
            if linkVal == 'auto' then
                link = game .. '/' .. chara
            else
                link = linkVal:gsub(':', ':')
            end
        end
    else
        link = game .. '/' .. chara
    end

    -- Generate icon
	local iconFile = args.icon or game .. '_' .. chara .. '.webp'
	local altText = '' 
    if args.label == 'none' then
        altText = chara
    end
    local size = args.size or '24px'

    -- Build HTML using mw.html with classes
    local root = mw.html.create('span')
        :addClass('clabel-container')

    if args.label == 'none' then
        root:tag('span')
            :addClass('clabel')
            :wikitext('[[File:' .. iconFile .. '|link=' .. link .. '|x' .. size .. '|alt=' .. altText .. ']]')
            :done()
    else
        root:tag('span')
            :addClass('clabel')
            :wikitext('[[File:' .. iconFile .. '|link=' .. link .. '|x' .. size .. '|alt=' .. altText .. ']]')
            :done()
        :tag('span')
            :addClass('clabel-invisible-space')
            :wikitext(' ')
            :done()
        :tag('span')
            :addClass('clabel-text')
            :wikitext(p.formatText(args, chara, link))
            :done()
    end

    return tostring(root)
end

function p.formatText(args, chara, link)
    local labelText = args.label or chara
    local formattedText = ''

    if args.format == 'none' then
        formattedText = labelText
    elseif args.format == 'italic' then
        formattedText = mw.html.create('span')
            :addClass('clabel-italic')
            :wikitext(labelText)
            :done()
    elseif args.format == 'bold' then
        formattedText = mw.html.create('span')
            :addClass('clabel-bold')
            :wikitext(labelText)
            :done()
    elseif args.format == 'italicbold' or args.format == 'bolditalic' then
        formattedText = mw.html.create('span')
            :addClass('clabel-bold')
            :addClass('clabel-italic')
            :wikitext(labelText)
            :done()
    else
        -- Default to semi-bold
        formattedText = mw.html.create('span')
            :addClass('clabel-semi-bold')
            :wikitext(labelText)
            :done()
    end

    if link ~= '' then
        return '[[' .. link .. '|' .. tostring(formattedText) .. ']]'
    end

    return tostring(formattedText)
end

return p