View source for Module:ExtractSection
Jump to navigation
Jump to search
You do not have permission to edit this page, for the following reasons:
You can view and copy the source of this page.
local p = {}
-- Helper function to extract content under a specific section header
function p.extractSection(content, header, strict)
-- Define the pattern to find the section header
local patternHeader = string.gsub(header, "[%^%$%(%)%%%.%[%]%*%+%-%?]","%%%0")
-- Find the start of the specified header
local sectionStart, sectionEnd = mw.ustring.find(content, patternHeader .. "%s-=+%s-\n")
-- If the section is not found, return nil
if not sectionStart then
return "Error: Could not find section."
end
-- Get section header level
local headerLevel = mw.ustring.match(content, "=+", sectionStart)
if not strict or strict == "" then
headerLevel = "=" .. mw.ustring.gsub(mw.ustring.sub(headerLevel,2), "=", "=?") .. "%s*[^=]"
end
-- Find the next section header of equal or higher level
local nextHeaderPattern = "\n" .. headerLevel
local nextHeaderStart, nextHeaderEnd = mw.ustring.find(content, nextHeaderPattern, sectionEnd + 1)
-- Extract the content until the next section header or the end of the content
000
1:0
Template used on this page:
Return to Module:ExtractSection.