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

Module:Lead: Difference between revisions

From Teriock
Content deleted Content added
No edit summary
No edit summary
Line 7: Line 7:
end
end


-- Fetch the raw wikitext of the page
-- Create a new title object
local content = mw.title.new(title):getContent()
local pageTitle = mw.title.new(title)
if not content then
if not pageTitle then
return "Error: Could not fetch content for page '" .. title .. "'."
return "Error: Invalid page title."
end
end


-- Transclude and expand templates from the page
-- Find position of the first heading (e.g., '== Something ==')
local heading_start = string.find(content, "\n==+[^=]")
local fullContent = frame:expandTemplate{ title = title }

if heading_start then
-- Extract only up to the heading (excluding it)
-- Find the position of the first heading (== Heading ==)
content = string.sub(content, 1, heading_start - 1)
local headingStart = string.find(fullContent, "\n==+[^=]")
local leadContent

if headingStart then
leadContent = string.sub(fullContent, 1, headingStart - 1)
else
leadContent = fullContent
end
end


return leadContent
-- Return the parsed wikitext
-- return frame:preprocess(content)
return content
end
end



Revision as of 20:18, 29 March 2025

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

local p = {}

function p.lead(frame)
    local title = frame.args[1]
    if not title then
        return "Error: No page title provided."
    end

    -- Create a new title object
    local pageTitle = mw.title.new(title)
    if not pageTitle then
        return "Error: Invalid page title."
    end

    -- Transclude and expand templates from the page
    local fullContent = frame:expandTemplate{ title = title }

    -- Find the position of the first heading (== Heading ==)
    local headingStart = string.find(fullContent, "\n==+[^=]")
    local leadContent

    if headingStart then
        leadContent = string.sub(fullContent, 1, headingStart - 1)
    else
        leadContent = fullContent
    end

    return leadContent
end

return p