Module:Transclude: Difference between revisions
From Teriock
More actions
No edit summary |
No edit summary |
||
| Line 8: | Line 8: | ||
-- Main function to transclude pages | -- Main function to transclude pages | ||
function p.transclude(frame) | function p.transclude(frame) | ||
-- Get the raw input | -- Get the raw input and preprocess it first | ||
local input = frame.args[1] or '' | local input = frame:preprocess(frame.args[1] or '') | ||
local output = {} | local output = {} | ||
-- Split | -- Split the input into lines | ||
for | for line in input:gmatch('[^\r\n]+') do | ||
line = trim(line) | |||
if | if line ~= '' then | ||
-- Only add the transclusion brackets if they're not already present | |||
if not line:match('^{{.*}}$') then | |||
line = string.format('{{%s}}', line) | |||
end | |||
table.insert(output, line) | |||
end | end | ||
end | end | ||
Revision as of 11:46, 14 January 2025
Documentation for this module may be created at Module:Transclude/doc
local p = {}
-- Helper function to trim whitespace from a string
local function trim(s)
return s:match('^%s*(.-)%s*$')
end
-- Main function to transclude pages
function p.transclude(frame)
-- Get the raw input and preprocess it first
local input = frame:preprocess(frame.args[1] or '')
local output = {}
-- Split the input into lines
for line in input:gmatch('[^\r\n]+') do
line = trim(line)
if line ~= '' then
-- Only add the transclusion brackets if they're not already present
if not line:match('^{{.*}}$') then
line = string.format('{{%s}}', line)
end
table.insert(output, line)
end
end
-- Join all transclusions with newlines
return table.concat(output, '\n')
end
-- Function to handle direct template calls
function p.transcludeFromTemplate(frame)
local parent = frame:getParent()
return p.transclude(parent)
end
return p