Geen bewerkingssamenvatting |
Geen bewerkingssamenvatting |
||
| Regel 50: | Regel 50: | ||
else | else | ||
local newVal = string.gsub( map, varstr, v ) | local newVal = string.gsub( map, varstr, v ) | ||
local | -- local newVal = frame:preprocess( newVal ) --no effect | ||
table.insert( mappedTable, | local newVal = frame:expandTemplate( newVal ) --no effect | ||
table.insert( mappedTable, newVal ) | |||
end | end | ||
end | end | ||
Versie van 5 sep 2023 20:46
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
}}
Luafout in mw.lua op regel 313: frame:expandTemplate: the first parameter must be a table
Or, using unnamed, sequential approach:
{{#invoke:Arraymap |map |Álvar,Núñez ,, Cabeza, de Vaca,, |, |xxx |[xxx] |;\s}}
Luafout in mw.lua op regel 313: frame:expandTemplate: the first parameter must be a table
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
}}
Luafout in mw.lua op regel 313: frame:expandTemplate: the first parameter must be a table--[[
{{#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 fuzzy = frame.args.fuzzysep or frame.args[6] 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 pairs(valTable) do
local v = mw.text.trim( v )
if v == "" or v == nil then
--nothing
else
local newVal = string.gsub( map, varstr, v )
-- local newVal = frame:preprocess( newVal ) --no effect
local newVal = frame:expandTemplate( newVal ) --no effect
table.insert( mappedTable, newVal )
end
end
local newVals = table.concat( mappedTable, outputsep )
return newVals
end
return p
