Module:HeaderIncrementer: Difference between revisions
From Teriock
More actions
No edit summary |
No edit summary |
||
| Line 2: | Line 2: | ||
function p.incrementHeadings(frame) | function p.incrementHeadings(frame) | ||
-- Get the content and | -- Get the content and next template name | ||
local content = frame.args.content or "No content provided." | local content = frame.args.content or "No content provided." | ||
local | local nextTemplate = frame.args.template or "H1" | ||
-- Increment heading levels | -- Increment heading levels | ||
| Line 23: | Line 17: | ||
end) | end) | ||
-- | -- Replace {{H| with the next template in the cycle | ||
incrementedContent = incrementedContent:gsub("{{H|", "{{" .. nextTemplate .. "|") | |||
return incrementedContent | return incrementedContent | ||
end | end | ||
return p | return p | ||
Revision as of 03:02, 26 December 2024
Documentation for this module may be created at Module:HeaderIncrementer/doc
local p = {}
function p.incrementHeadings(frame)
-- Get the content and next template name
local content = frame.args.content or "No content provided."
local nextTemplate = frame.args.template or "H1"
-- 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)
-- Replace {{H| with the next template in the cycle
incrementedContent = incrementedContent:gsub("{{H|", "{{" .. nextTemplate .. "|")
return incrementedContent
end
return p