Module:CodeToText: Difference between revisions
m MarkWD moved page Module:TranscludeModuleCode to Module:CodeToText without leaving a redirect |
// via Wikitext Extension for VSCode |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
-- | --[[ | ||
* Name: CodeToText | |||
* Author: Mark W. Datysgeld | |||
* Description: Utility for extracting content snippets from wiki pages using emoji hook markers placed at the beginning and end of desired content sections | |||
* Notes: Example usage: {{#invoke:CodeToText|showSnippet|page=Module:Example|hook=⭐}}; Supports emoji hooks like 🪝, 📌, 🔖, ⭐ for both inline and block content extraction | |||
]] | |||
local p = {} | local p = {} | ||
function p.showSnippet(frame) | function p.showSnippet(frame) | ||
| Line 22: | Line 13: | ||
return "Error: 'page' parameter is required" | return "Error: 'page' parameter is required" | ||
end | end | ||
local hook = frame.args.hook | local hook = frame.args.hook | ||
if not hook then | if not hook then | ||
return "Error: 'hook' parameter is required" | return "Error: 'hook' parameter is required" | ||
end | end | ||
| Line 44: | Line 29: | ||
end | end | ||
-- Try inline hooks first | |||
local inlineSnippet = content:match(hook .. "(.-)" .. hook) | |||
-- | if inlineSnippet then | ||
local | return inlineSnippet | ||
end | end | ||
-- | -- Find block hooks | ||
local s, e = content:find(hook, 1, true) | |||
return | if not s then | ||
return "Error: Need at least 2 instances of hook '" .. hook .. "' in '" .. pageName .. "'" | |||
end | |||
local s2, e2 = content:find(hook, e + 1, true) | |||
if not s2 then | |||
return "Error: Need at least 2 instances of hook '" .. hook .. "' in '" .. pageName .. "'" | |||
end | |||
-- Extract text between the two hooks, excluding the hook lines | |||
local snippet = content:sub(e + 1, s2 - 1) | |||
-- Trim leading and trailing newlines | |||
if snippet:sub(1,1) == "\n" then | |||
snippet = snippet:sub(2) | |||
end | |||
if snippet:sub(-1) == "\n" then | |||
snippet = snippet:sub(1, -2) | |||
end | |||
return snippet | |||
end | end | ||
return p | return p | ||