More actions
Content deleted Content added
No edit summary Tag: Reverted |
No edit summary |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
-- Function to adjust headings with recursion control |
|||
function p. |
function p.incrementHeadings(frame) |
||
-- Get the page name from the argument |
|||
⚫ | |||
⚫ | |||
local content = frame.args.content or "No content provided." |
|||
if not pageName or pageName == "" then |
|||
⚫ | |||
return "Error: No page name provided." |
|||
local maxDepth = 3 -- Limit recursion depth to 3 |
|||
end |
|||
⚫ | |||
local title = mw.title.new(pageName) |
|||
if not title or not title.exists then |
|||
return "Error: Page does not exist." |
|||
end |
|||
-- Check if recursion limit is reached |
|||
local content = title:getContent() |
|||
if |
if depth > maxDepth then |
||
return " |
return "Recursion limit reached." |
||
end |
end |
||
-- |
-- Increment heading levels |
||
local incrementedContent = content:gsub("(=%s*)(.-)(%s*=)", function(leadingEquals, text, trailingEquals) |
|||
local headingLevel = #leadingEquals -- Count the number of `=` signs |
|||
if |
if headingLevel < 6 then |
||
⚫ | |||
local newEquals = string.rep("=", headingLevel + 1) |
|||
⚫ | |||
else |
else |
||
return |
return leadingEquals .. text .. trailingEquals |
||
end |
end |
||
end) |
end) |
||
-- Return adjusted content with an incremented depth |
|||
return incrementedContent:gsub("{{H|", "{{H|depth=" .. (depth + 1) .. "|") |
|||
end |
end |
||
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