Module:HeaderIncrementer: Difference between revisions
From Teriock
More actions
Created page with "local p = {} -- Function to adjust headings function p.incrementHeadings(frame) -- Get the content from named argument "content" local content = frame.args.content or "No content provided." -- 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 newEq..." |
No edit summary |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Function to adjust headings | -- Function to adjust headings with recursion control | ||
function p.incrementHeadings(frame) | function p.incrementHeadings(frame) | ||
-- Get the content | -- Get the content and recursion depth | ||
local content = frame.args.content or "No content provided." | local content = frame.args.content or "No content provided." | ||
local depth = tonumber(frame.args.depth) or 1 | |||
local maxDepth = 3 -- Set a reasonable recursion limit | |||
-- Check if recursion limit is reached | |||
if depth > maxDepth then | |||
return "Recursion limit reached." | |||
end | |||
-- Increment heading levels | -- Increment heading levels | ||
| Line 17: | Line 24: | ||
end) | end) | ||
return incrementedContent | -- Return adjusted content with an incremented depth | ||
return incrementedContent:gsub("{{H|", "{{H|depth=" .. (depth + 1) .. "|") | |||
end | end | ||
return p | return p | ||
Revision as of 02:50, 26 December 2024
Documentation for this module may be created at Module:HeaderIncrementer/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 -- Set a reasonable recursion limit
-- 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