Module:CopyEmbeddedStories: Difference between revisions
From The Seven Sages of Rome
No edit summary |
No edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- | -- Function to fetch EmbeddedStory templates from a given page | ||
function p.fetchEmbeddedStories(frame) | |||
local | local pageTitle = frame.args["sourcePage"] | ||
if not pageTitle then | |||
return "Error: No source page specified." | |||
end | end | ||
-- Get the wikitext content of the source page | |||
local content = mw.title.new(pageTitle):getContent() | |||
if not content then | |||
return "Error: Source page not found or empty." | |||
local | |||
if not | |||
return "Error: | |||
end | end | ||
-- Find all EmbeddedStory templates | |||
local templates = {} | |||
for embeddedStory in content:gmatch("{{EmbeddedStory.-}}") do | |||
table.insert(templates, embeddedStory) | |||
-- | |||
local | |||
for | |||
end | end | ||
return | -- Return templates as a single concatenated string | ||
return table.concat(templates, "\n") | |||
end | end | ||
return p | return p |
Latest revision as of 20:17, 13 November 2024
This module extracts all instances of the `EmbeddedStory` template from a specified source page. It is useful for retrieving and reusing embedded story elements across different pages in a MediaWiki environment.
Usage
The module can be invoked using `#invoke` in a template or wiki page.
{{#invoke:CopyEmbeddedStories|fetchEmbeddedStories | sourcePage=ExamplePage }}
Parameters
- `sourcePage` - The title of the wiki page from which `EmbeddedStory` templates should be retrieved.
Behavior
1. The module reads the raw wikitext content of the specified `sourcePage`. 2. It searches for all instances of `Template:EmbeddedStory ...` within the page. 3. The found templates are returned as a newline-separated list. 4. If the page is not found or empty, an error message is returned.
Example Output
If `ExamplePage` contains:
{{EmbeddedStory|Title=Story 1}} Some text here. {{EmbeddedStory|Title=Story 2}}
Then the module will return:
{{EmbeddedStory|Title=Story 1}} {{EmbeddedStory|Title=Story 2}}
Notes
- The module does not modify the extracted templates; it simply returns them as raw wikitext.
- If no `EmbeddedStory` templates are found, it returns an empty string.
- If `sourcePage` is not specified, an error message is displayed.
local p = {}
-- Function to fetch EmbeddedStory templates from a given page
function p.fetchEmbeddedStories(frame)
local pageTitle = frame.args["sourcePage"]
if not pageTitle then
return "Error: No source page specified."
end
-- Get the wikitext content of the source page
local content = mw.title.new(pageTitle):getContent()
if not content then
return "Error: Source page not found or empty."
end
-- Find all EmbeddedStory templates
local templates = {}
for embeddedStory in content:gmatch("{{EmbeddedStory.-}}") do
table.insert(templates, embeddedStory)
end
-- Return templates as a single concatenated string
return table.concat(templates, "\n")
end
return p