Jump to content

Module:CodeToText

Revision as of 18:05, 7 February 2025 by MarkWD (talk | contribs) (MarkWD moved page Module:Test to Module:TrascludeModuleCode without leaving a redirect)

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

local p = {}

function p.showSnippet(frame)
    -- Get the page to load, defaulting to "Module:Sandbox" if none is specified
    local pageName = frame.args.page or 'Module:Sandbox'
    local fromLine = tonumber(frame.args.from) or 1
    local toLine   = tonumber(frame.args.to)   or fromLine

    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

    -- Split by line
    local lines = {}
    for line in content:gmatch('[^\r\n]+') do
        table.insert(lines, line)
    end

    -- Extract only the requested line range
    local snippet = {}
    for i = fromLine, math.min(toLine, #lines) do
        table.insert(snippet, lines[i])
    end

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

return p