Jump to content
Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:IncreaseHeadings: Difference between revisions

From Teriock
Created page with "local p = {} function p.increaseHeadings(frame) local content = frame.args[1] or '' -- Replace headings with one level higher (e.g., == becomes ===) content = mw.ustring.gsub(content, '^(=+)(.-)(=+)$', function(eq1, text, eq2) if #eq1 == #eq2 then local newLevel = eq1 .. '=' return newLevel .. text .. newLevel else return eq1 .. text .. eq2 end end) return content end return p"
 
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


function p.increaseHeadings(frame)
-- Function to adjust headings with recursion control
     local content = frame.args[1] or ''
function p.incrementHeadings(frame)
     -- Replace headings with one level higher (e.g., == becomes ===)
    -- Get the content and recursion depth
     content = mw.ustring.gsub(content, '^(=+)(.-)(=+)$', function(eq1, text, eq2)
     local content = frame.args.content or "No content provided."
         if #eq1 == #eq2 then
     local depth = tonumber(frame.args.depth) or 1
             local newLevel = eq1 .. '='
     local maxDepth = 3 -- Limit recursion depth to 3
             return newLevel .. text .. newLevel
 
    -- Check if recursion limit is reached
    if depth > maxDepth then
        return "Recursion limit reached."
    end
 
    -- Increment heading levels
    local incrementedContent = content:gsub("(=%s*)(.-)(%s*=)", function(leadingEquals, text, trailingEquals)
         local headingLevel = #leadingEquals -- Count the number of `=` signs
        if headingLevel < 6 then
             local newEquals = string.rep("=", headingLevel + 1)
             return newEquals .. text .. newEquals
         else
         else
             return eq1 .. text .. eq2
             return leadingEquals .. text .. trailingEquals
         end
         end
     end)
     end)
     return content
 
    -- Return adjusted content with an incremented depth
     return incrementedContent:gsub("{{H|", "{{H|depth=" .. (depth + 1) .. "|")
end
end


return p
return p

Latest revision as of 02:40, 26 December 2024

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

local p = {}

-- Function to adjust headings with recursion control
function p.incrementHeadings(frame)
    -- Get the content and recursion depth
    local content = frame.args.content or "No content provided."
    local depth = tonumber(frame.args.depth) or 1
    local maxDepth = 3 -- Limit recursion depth to 3

    -- Check if recursion limit is reached
    if depth > maxDepth then
        return "Recursion limit reached."
    end

    -- Increment heading levels
    local incrementedContent = content:gsub("(=%s*)(.-)(%s*=)", function(leadingEquals, text, trailingEquals)
        local headingLevel = #leadingEquals -- Count the number of `=` signs
        if headingLevel < 6 then
            local newEquals = string.rep("=", headingLevel + 1)
            return newEquals .. text .. newEquals
        else
            return leadingEquals .. text .. trailingEquals
        end
    end)

    -- Return adjusted content with an incremented depth
    return incrementedContent:gsub("{{H|", "{{H|depth=" .. (depth + 1) .. "|")
end

return p