Module:inline: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
No edit summary
Line 4: Line 4:
     local args = {}
     local args = {}
     for inline in mw.ustring.gmatch(term, "<([^>]+)>") do
     for inline in mw.ustring.gmatch(term, "<([^>]+)>") do
     if mw.ustring.find(inline, "%:") then
     local s = mw.text.split(inline, ":", true)
    local arg, def = mw.ustring.match(inline, "^([^%:]+)%:([^%:]+)$")
    args[s[1]] = s[2] or true
    args[arg] = def
    else
    args[inline] = true
    end
     end
     end
     return mw.ustring.gsub(term, "<[^>]+>", ""), args
     return mw.ustring.gsub(term, "<[^>]+>", ""), args

Revision as of 14:08, 31 May 2024

local m_inline = require("Module:inline")
local term, args = m_inline.parse("hello<key1:value1><key2>")

Yields:

term = "hello"
args = {["key1"] = "value1", ["key2"] = true}

local export = {}

function export.parse(term)
    local args = {}
    for inline in mw.ustring.gmatch(term, "<([^>]+)>") do
    	local s = mw.text.split(inline, ":", true)
    	args[s[1]] = s[2] or true
    end
    return mw.ustring.gsub(term, "<[^>]+>", ""), args
end

return export