Regel 59: Regel 59:
 
--nothing
 
--nothing
 
else
 
else
local newVal = string.gsub( map, varstr, v )
+
local mapUnstripped = mw.text.unstrip( map )
 +
local newVal = string.gsub( mapUnstripped, varstr, v )
 
--local newVal = mw.text.killMarkers( newVal )
 
--local newVal = mw.text.killMarkers( newVal )
 
--local newVal = frame:preprocess( newVal )
 
--local newVal = frame:preprocess( newVal )
Regel 69: Regel 70:
 
 
 
local res = table.concat( mappedTable, outputsep )
 
local res = table.concat( mappedTable, outputsep )
local res = mw.text.unstrip( res )
+
--local res = mw.text.unstrip( res )
local res = mw.text.killMarkers( res )
+
--local res = mw.text.killMarkers( res )
 
local res = frame:preprocess( res )
 
local res = frame:preprocess( res )
 
-- local res = frame:preprocess( mw.text.unstrip( newVals ) )
 
-- local res = frame:preprocess( mw.text.unstrip( newVals ) )

Versie van 22 sep 2023 16:14

Wiki links

[[apples]], [[pears]], [[peaches]]

  • Re-expanding wiki links after mw.text.nowiki?

Templates

<i class="fa fa-file " ></i>, <i class="fa fa-map-marker " ></i>, <i class="fa fa-circle " ></i>

SMW

, ,


--[[
Module:Arraymap was created with the assumption that some syntax should be masked to avoid parsing

But that may not be necessary

{{#invoke:Arraymap2
|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 vals = mw.text.nowiki( vals )
	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 ""
	local map = mw.text.nowiki( map )
	--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
		else
			local mapUnstripped = mw.text.unstrip( map )
			local newVal = string.gsub( mapUnstripped, varstr, v )
			--local newVal = mw.text.killMarkers( newVal )
			--local newVal = frame:preprocess( newVal )
			--local newVal = html:wikitext( newVal )
			--local newVal = newVal .. "[" .. smwescaped .. "]"
			table.insert( mappedTable, newVal )
		end
	end
	
	local res = table.concat( mappedTable, outputsep )
	--local res = mw.text.unstrip( res )
	--local res = mw.text.killMarkers( res )
	local res = frame:preprocess( res )
	-- local res = frame:preprocess( mw.text.unstrip( newVals ) )
	return res
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