13 Articles
Project Nakagami

Module:Stack: Difference between revisions

No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 5: Line 5:
-- stacksize: The size of stacks, default 64, but sometimes 16 or other.
-- stacksize: The size of stacks, default 64, but sometimes 16 or other.
-- link: The page to link.
-- link: The page to link.
-- nox: Whether to avoid prepending x before the main number.
function p.AddStackBreakout(frame)
function p.AddStackBreakout(frame)
     local DEFAULT_STACK_SIZE = 64
     local DEFAULT_STACK_SIZE = 64
Line 24: Line 25:


     local TextHTML = mw.html.create('span');
     local TextHTML = mw.html.create('span');
     TextHTML:wikitext(Count);
     if frame:getArgument('nox'):expand() == "true" then TextHTML:wikitext(Count);
     if FullStacks >= 1 then
    else TextHTML:wikitext('x' .. Count); end
     if (FullStacks == 1 and PlusItems > 0) or FullStacks > 1 then
         local StackHTML = TextHTML:tag('span');
         local StackHTML = TextHTML:tag('span');
         StackHTML:addClass(SPAN_CLASS)
         StackHTML:addClass(SPAN_CLASS)

Latest revision as of 10:16, 15 July 2024

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

local p = {} --package

-- Allowed Arguments:
-- count: The number to elaborate.
-- stacksize: The size of stacks, default 64, but sometimes 16 or other.
-- link: The page to link.
-- nox: Whether to avoid prepending x before the main number.
function p.AddStackBreakout(frame)
    local DEFAULT_STACK_SIZE = 64
    local SPAN_CLASS = 'stack-breakout-text' -- the class attribute on the text span element

    local Count = tonumber(frame:getArgument('count'):expand())
    if not Count then return end

    local StackSize = tonumber(frame:getArgument('stacksize'):expand())
    if not StackSize then StackSize = DEFAULT_STACK_SIZE end

    local FullStacks = math.floor(Count / StackSize)
    local PlusItems = Count - (FullStacks * StackSize)
    local PlusText = ''
    if PlusItems > 0 then PlusText = ' + ' .. PlusItems end

    local LinkDest = frame:getArgument('link'):expand()
    if not LinkDest then LinkDest = '' end

    local TextHTML = mw.html.create('span');
    if frame:getArgument('nox'):expand() == "true" then TextHTML:wikitext(Count);
    else TextHTML:wikitext('x' .. Count); end
    if (FullStacks == 1 and PlusItems > 0) or FullStacks > 1 then
        local StackHTML = TextHTML:tag('span');
        StackHTML:addClass(SPAN_CLASS)
        StackHTML:wikitext(' (' .. FullStacks .. 'x' .. StackSize .. PlusText .. ')');
    end

    if LinkDest ~= '' then
        if LinkDest:find('//') then -- External Link
            return '[' .. LinkDest .. ' ' .. tostring(TextHTML) .. ']'
        else -- Internal Link
            return '[[' .. LinkDest .. '|' .. tostring(TextHTML) .. ']]'
        end
    else
        return tostring(TextHTML)
    end
end

return p