Module:Johnuniq/Investigate

From Wikipedia

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

-- Extremely cut down code from Module:Convert to investigate result
-- "Script error" when the module attempts to return error text
-- that was formatted by mw.text.tag.
-- Code like this used to work, but some change in Scribunto means the
-- mw global variable used in Module:Mw is not available elsewhere.
-- Script error due to mw.text.tag() call ('text' is nil):
--     Lua error in Module:Convert at line 1272:
--     attempt to index field 'text' (a nil value).
--     Backtrace:
--       Module:Convert:1272: in function "chunk"
--       mw.lua:401: in function "chunk"

local function process(parms, in_unit_table)
    if in_unit_table == 'good' then
        return true, 'Very good'
    end
    return false, 'Bad "' .. (in_unit_table or 'nil') .. '"'
end

local p = {}

function p.convert(frame)
    local parms = {}
    local pframe = frame:getParent()
    local in_unit_table = pframe.args[1]
    local success, result = process(parms, in_unit_table)
    if not success then
        local params = {style="color:black; background-color:orange;"}
        result = mw.text.tag({name="span", contents="Conversion error: " .. result, params=params})
    end
    return result
end

-- Following fails ('uri' in mw is nil; also 'urlencode' in string is nil).
-- Test suspicion that mw.lua in Scribunto exposes urlencode.
-- Argument 1 to template is urlencoded.
-- Argument 2 determines how spaces are encoded:
--     'QUERY' --> '+' (default)
--     'PATH'  --> '%20'
--     'WIKI'  --> '_'
function p.urlencode(frame)
    local pframe = frame:getParent()
    local s = pframe.args[1] or ''
    local how = pframe.args[2]
    return '<code>urlencode("' .. s .. '") = ' .. mw.uri.urlencode(s, how) .. '</code>'
end

return p