Geen bewerkingssamenvatting |
Imported by PageSync Label: Handmatige ongedaanmaking |
||
| (38 tussenliggende versies door 3 gebruikers niet weergegeven) | |||
| Regel 13: | Regel 13: | ||
p.map = function(frame) | p.map = function(frame) | ||
--mandatory | |||
local vals = frame.args.vals or mw.text.trim(frame.args[1]) or "" | local vals = frame.args.vals or mw.text.trim(frame.args[1]) or "" | ||
local sep = frame.args.sep or mw.text.trim(frame.args[2]) or "," | local sep = frame.args.sep or mw.text.trim(frame.args[2]) or "," | ||
local varstr = frame.args.varstr or mw.text.trim(frame.args[3]) or "@@@" | local varstr = frame.args.varstr or mw.text.trim(frame.args[3]) or "@@@" | ||
local map = frame.args.map or mw.text.trim(frame.args[4]) or "" | local map = frame.args.map or mw.text.trim(frame.args[4]) or "" | ||
local outputsep = frame.args.outputsep or | --optional | ||
local outputsep = string.gsub( outputsep, [[\s]], " " ) --allow for spaces with \s | local outputsep = frame.args.outputsep or frame.args[5] or "" | ||
local outputsep = string.gsub( mw.text.trim(outputsep), [[\s]], " " ) --allow for spaces with \s | |||
local escaped = frame.args.escaped or frame.args[6] or "false" | |||
local fuzzy = frame.args.fuzzysep or frame.args[7] or "false" | |||
-- fuzzysep (need a better name) lets you use a series of single characters (OR not AND) | -- fuzzysep (need a better name) lets you use a series of single characters (OR not AND) | ||
--[[ | --[[ | ||
if fuzzy == "true" then | if mw.text.trim(fuzzy) == "true" then | ||
local sepPattern = '[^' .. sep .. ')+' | local sepPattern = '[^' .. sep .. ')+' | ||
local valTable = {} | local valTable = {} | ||
| Regel 43: | Regel 45: | ||
local valTable = mw.text.split( vals, sep ) | local valTable = mw.text.split( vals, sep ) | ||
local mappedTable = {} | local mappedTable = {} | ||
for k,v in | |||
for k,v in ipairs(valTable) do | |||
local v = mw.text.trim( v ) | local v = mw.text.trim( v ) | ||
if v == "" or v == nil then | if v == "" or v == nil then | ||
--nothing | --nothing | ||
elseif ( mw.text.trim(escaped) == "true" ) then | |||
local newVal = string.gsub( map, varstr, v ) | |||
local newVal = p.convertescaped( newVal ) | |||
local newVal = frame:preprocess( newVal ) | |||
--local newVal = newVal .. " (smwconverted)" | |||
table.insert( mappedTable, newVal ) | |||
else | else | ||
local newVal = string.gsub( map, varstr, v ) | local newVal = string.gsub( map, varstr, v ) | ||
--local newVal = newVal .. "[" .. smwescaped .. "]" | |||
table.insert( mappedTable, newVal ) | table.insert( mappedTable, newVal ) | ||
end | end | ||
| Regel 56: | Regel 66: | ||
return newVals | return newVals | ||
end | |||
--[[ | |||
fetch item by index number (in the way Lua counts them, beg. with 1) | |||
{{#invoke:Arraymap|arrayindex|apples;pears|;|1}} | |||
]]-- | |||
p.arrayindex =function (frame) | |||
local str = frame.args[1] or "" | |||
if str == "" then return "" end | |||
local sep = frame.args[2] or ";" | |||
local index = tonumber( frame.args[3] or "1" ) | |||
local items = mw.text.split( str, sep ) | |||
if type(items) ~= "table" or items[index] == nil then return "" end | |||
return mw.text.trim( items[index] ) | |||
end | |||
--[[ | |||
Convert escaped syntax, esp. for SMW | |||
]]-- | |||
p.convertescaped = function( str ) | |||
-- example %{%{#ask: ((Has name::...)) %Has name %link=none %}%} | |||
-- for ((Has name::...)), alternative %[%[ ... %]%] | |||
local conversionTable = { | |||
["%%{%%{"] = "{{", -- %{%{ | |||
["%%}%%}"] = "}}", -- %}%} | |||
["%%[%%["] = "[[", -- %[%[ | |||
["%%]%%]"] = "]]", -- %]%] | |||
["%(%("] = "[[", -- (( alternative | |||
["%)%)"] = "]]", -- )) alternative | |||
["%%"] = "|" | |||
} | |||
--str = string.gsub( str, "%S+", conversionTable ) | |||
--for k,v in ipairs( conversionTable ) do | |||
-- str = string.gsub( str, k, v ) | |||
--end | |||
local str = string.gsub( str, "%%{%%{", "{{" ) | |||
local str = string.gsub( str, "%%}%%}", "}}" ) | |||
--local str = string.gsub( str, "%[%[", "[[" ) | |||
--local str = string.gsub( str, "%]%]", "]]" ) | |||
--local str = string.gsub( str, "%(%(", "[[" ) | |||
--local str = string.gsub( str, "%)%)", "]]" ) | |||
local str = string.gsub( str, "%%", "|" ) | |||
return str | |||
end | end | ||
return p | return p | ||
Huidige versie van 5 mrt 2026 09:20
Module:Arraymap
Summary
Module similar in functionality to PageForms's arraymap.
==Simple==
{{#invoke:Arraymap |map
|vals=Álvar,Núñez ,, Cabeza,de Vaca,,|sep=,,
|varstr=xxx |map=(xxx) |outputsep=;\s
}}
[Álvar,Núñez]; [Cabeza, de Vaca]
Or, using unnamed, sequential approach:
{{#invoke:Arraymap |map |Álvar,Núñez ,, Cabeza, de Vaca,, |, |xxx |[xxx] |;\s}}
[Álvar]; [Núñez]; [Cabeza]; [de Vaca]
Special use cases
If sep needs to be a space, use %s?
Escaped syntax for curly braces and pipes
Escape {{... }} as %{%{ ... %}%}
Escape | as %
{{#invoke:Arraymap |map
|vals=Persoon/-1002367743,Persoon/400
|sep=,
|varstr=xxx
|map=%{%{#ask: [[xxx]] %mainlabel=- %?Has name= %}%}
|outputsep=;\s
|escaped=true
|fuzzy=false
}}
Nelly Kruize; Ralph Blum--[[
{{#invoke:Arraymap
|vals=
|sep=
|varstr=
|map=
|outputsep=
}}
]]--
local p = {}
p.map = function(frame)
--mandatory
local vals = frame.args.vals or mw.text.trim(frame.args[1]) or ""
local sep = frame.args.sep or mw.text.trim(frame.args[2]) or ","
local varstr = frame.args.varstr or mw.text.trim(frame.args[3]) or "@@@"
local map = frame.args.map or mw.text.trim(frame.args[4]) or ""
--optional
local outputsep = frame.args.outputsep or frame.args[5] or ""
local outputsep = string.gsub( mw.text.trim(outputsep), [[\s]], " " ) --allow for spaces with \s
local escaped = frame.args.escaped or frame.args[6] or "false"
local fuzzy = frame.args.fuzzysep or frame.args[7] or "false"
-- fuzzysep (need a better name) lets you use a series of single characters (OR not AND)
--[[
if mw.text.trim(fuzzy) == "true" then
local sepPattern = '[^' .. sep .. ')+'
local valTable = {}
for val in string.gmatch( vals, sepPattern ) do
-- replace varstr in map with val
local val = mw.text.trim( val )
local newVal = string.gsub( map, varstr, val )
table.insert( valTable, newVal )
end
local newVals = table.concat( valTable, outputsep )
return newVals
end
--]]
--Standard approach
local valTable = {}
local valTable = mw.text.split( vals, sep )
local mappedTable = {}
for k,v in ipairs(valTable) do
local v = mw.text.trim( v )
if v == "" or v == nil then
--nothing
elseif ( mw.text.trim(escaped) == "true" ) then
local newVal = string.gsub( map, varstr, v )
local newVal = p.convertescaped( newVal )
local newVal = frame:preprocess( newVal )
--local newVal = newVal .. " (smwconverted)"
table.insert( mappedTable, newVal )
else
local newVal = string.gsub( map, varstr, v )
--local newVal = newVal .. "[" .. smwescaped .. "]"
table.insert( mappedTable, newVal )
end
end
local newVals = table.concat( mappedTable, outputsep )
return newVals
end
--[[
fetch item by index number (in the way Lua counts them, beg. with 1)
{{#invoke:Arraymap|arrayindex|apples;pears|;|1}}
]]--
p.arrayindex =function (frame)
local str = frame.args[1] or ""
if str == "" then return "" end
local sep = frame.args[2] or ";"
local index = tonumber( frame.args[3] or "1" )
local items = mw.text.split( str, sep )
if type(items) ~= "table" or items[index] == nil then return "" end
return mw.text.trim( items[index] )
end
--[[
Convert escaped syntax, esp. for SMW
]]--
p.convertescaped = function( str )
-- example %{%{#ask: ((Has name::...)) %Has name %link=none %}%}
-- for ((Has name::...)), alternative %[%[ ... %]%]
local conversionTable = {
["%%{%%{"] = "{{", -- %{%{
["%%}%%}"] = "}}", -- %}%}
["%%[%%["] = "[[", -- %[%[
["%%]%%]"] = "]]", -- %]%]
["%(%("] = "[[", -- (( alternative
["%)%)"] = "]]", -- )) alternative
["%%"] = "|"
}
--str = string.gsub( str, "%S+", conversionTable )
--for k,v in ipairs( conversionTable ) do
-- str = string.gsub( str, k, v )
--end
local str = string.gsub( str, "%%{%%{", "{{" )
local str = string.gsub( str, "%%}%%}", "}}" )
--local str = string.gsub( str, "%[%[", "[[" )
--local str = string.gsub( str, "%]%]", "]]" )
--local str = string.gsub( str, "%(%(", "[[" )
--local str = string.gsub( str, "%)%)", "]]" )
local str = string.gsub( str, "%%", "|" )
return str
end
return p
