Jump to content

Module:CodeToText: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:TranscludeModuleCode
--[[
-- Extracts a snippet of lines from a specified page using a defined line range. Optionally removes a given token (like a leading comment --) from each line.
* 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)
     local pageName = frame.args.page or 'Module:Sandbox'
     local pageName = frame.args.page
     local fromLine = tonumber(frame.args.from) or 1
    if not pageName then
     local toLine  = tonumber(frame.args.to)  or fromLine
        return "Error: 'page' parameter is required"
    end
 
     local hook = frame.args.hook
    if not hook then
        return "Error: 'hook' parameter is required"
     end


     local titleObj = mw.title.new(pageName)
     local titleObj = mw.title.new(pageName)
Line 19: Line 29:
     end
     end


     local lines = {}
    -- Try inline hooks first
    for line in content:gmatch("[^\r\n]+") do
     local inlineSnippet = content:match(hook .. "(.-)" .. hook)
         table.insert(lines, line)
    if inlineSnippet then
         return inlineSnippet
     end
     end


     local snippet = {}
     -- Find block hooks
     for i = fromLine, math.min(toLine, #lines) do
     local s, e = content:find(hook, 1, true)
         table.insert(snippet, lines[i])
    if not s then
         return "Error: Need at least 2 instances of hook '" .. hook .. "' in '" .. pageName .. "'"
     end
     end


     if frame.args.remove then
     local s2, e2 = content:find(hook, e + 1, true)
        local removeToken = frame.args.remove
    if not s2 then
        local escaped = removeToken:gsub("([^%w])", "%%%1")
        return "Error: Need at least 2 instances of hook '" .. hook .. "' in '" .. pageName .. "'"
        local pattern = "^" .. escaped .. "%s*"
    end
        for i, line in ipairs(snippet) do
 
              snippet[i] = line:gsub(pattern, "")
    -- Extract text between the two hooks, excluding the hook lines
        end
    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
     end


     return table.concat(snippet, "\n")
     return snippet
end
end


return p
return p

Latest revision as of 02:56, 25 August 2025

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

--[[
* 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 = {}

function p.showSnippet(frame)
    local pageName = frame.args.page
    if not pageName then
        return "Error: 'page' parameter is required"
    end

    local hook = frame.args.hook
    if not hook then
        return "Error: 'hook' parameter is required"
    end

    local titleObj = mw.title.new(pageName)
    if not titleObj then
        return "Failed to create mw.title object for '" .. pageName .. "'"
    end

    local content = titleObj:getContent()
    if not content then
        return "No content for '" .. pageName .. "'"
    end

    -- Try inline hooks first
    local inlineSnippet = content:match(hook .. "(.-)" .. hook)
    if inlineSnippet then
        return inlineSnippet
    end

    -- Find block hooks
    local s, e = content:find(hook, 1, true)
    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

return p