Module:inline: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
No edit summary
 
(6 intermediate revisions by 2 users not shown)
Line 3: Line 3:
function export.parse(term)
function export.parse(term)
     local args = {}
     local args = {}
     local i, key, value = 0
     for inline in mw.ustring.gmatch(term, "<([^>]+)>") do
    mw.log(type(i))
    local s = mw.text.split(inline, ":", true)
    mw.log(i)
    args[s[1]] = s[2] or true
    mw.log(type(term))
    if #s > 2 then mw.addWarning("multiple : found in inline string") end
    mw.log(term)
    mw.log(#term)
    while i < #term do
        key, value, i = mw.ustring.match(term, "<([a-zA-Z]+):([^>]+)>()")
        if key and value then args[key] = value end  
     end
     end
     return mw.ustring.gsub(term, "<[a-zA-Z]+:[^>]+>", ""), args
     return mw.ustring.gsub(term, "<[^>]+>", ""), args
end
end


return export
return export

Latest revision as of 14:10, 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
    	if #s > 2 then mw.addWarning("multiple : found in inline string") end
    end
    return mw.ustring.gsub(term, "<[^>]+>", ""), args
end

return export