Jump to content

Module:Foreach

Unchecked
From Wikipedia

Documentation for this module may be created at Module:Foreach/doc

local p = {}

function p.o(frame)
  local args = frame.args
  local p_args = frame:getParent().args
  local output = ''
  local r = args.r
  local value = r:match("^.'\"`UNIQ--") and mw.text.unstrip(r) or r
  if value == nil then return "" end

  -- nowikiの囲みがあれば削除
  if value:match("<nowiki>") then output = output .. '[no]' end
  value = value:gsub("^<nowiki>(.-)</nowiki>$", "%1")
  
  -- 引数の繰り返し変数を置換
  for arg_name, arg_v in pairs(p_args) do
    if tonumber(arg_name) ~= nil then
      output = output .. value:gsub("($.-X)", function(match)
        local tmp_name = match:sub(2, -2) -- 先頭の$、末尾のXを削除
        if #tmp_name == 0 then return arg_v end
        if p_args[tmp_name] == nil then return "" end
        return p_args[tmp_name]
      end)
    end
  end

  return mw.text.unstrip(output)
end

return p