模块:螺旋/sandbox

来自魔法纪录中文Wiki
跳转至: 导航 搜索


循环输出模块

{{#invoke:螺旋/sandbox|list_format|txt={{素材图标{{!}}$1|$2}}|separator=、|json=[["嫌悪の欠片+","3"],["嫌悪の欠片++","6"]]}}

嫌悪の欠片+(嫌恶的碎片+) × 3嫌悪の欠片++(嫌恶的碎片++) × 6

{{#invoke:螺旋/sandbox|list_format|txt={{素材图标{{!}}$1|$2}}$s$n|separator=、|json=[["嫌悪の欠片","3"],["嫌悪の欠片+","2"],["嫌悪の欠片++","1"]]}}

嫌悪の欠片(嫌恶的碎片) × 3嫌悪の欠片+(嫌恶的碎片+) × 2嫌悪の欠片++(嫌恶的碎片++) × 1


直接无效,请在使用在模板中

{{#invoke:螺旋/sandbox|list_format|txt={{素材图标{{!}}$1|$2}}|separator=、|json=Template|嫌悪の欠片++,3|嫌悪の欠片+,6}}

local p = {}

function p._list_format( format_string, separator, list )
  --mw.logObject(format_string , '[format_string]:' .. type(format_string))
  --mw.logObject(list , '[list]:' .. type(list))
  if type(list) ~= 'table' then
    return '[Error]list type is not table'
  end
  if separator == nil then
  	separator = ''
  end
  txt = ''
  separator_type = (string.find(format_string, '$s') == nil)
  list_len =  table_leng(list)
  separator_index = 0
  -- mw.log('list:' .. (#list))
  for i, v in pairs(list) do
    -- mw.log('item[' .. i .. ']:' .. type(v) )
    
    format_str = format_string
    format_str = string.gsub(format_str, '$n' , '\n' )
    
    separator_index = separator_index + 1
    if separator_type then
		if separator_index > 1 then
			txt = txt .. separator
		end
	else
		if list_len > separator_index then
			format_str = string.gsub(format_str, '$s' , separator )
		else
			format_str = string.gsub(format_str, '$s' , '' )
		end
	end
    
    txt2 = ''
    if type(v) == 'table' then
      -- mw.log('item[' .. i .. ']:' .. type(v) .. ' -> len:' .. (#v))
      txt2 = format_str
      for k2, v2 in pairs(v) do
        -- txt2 = string.gsub(txt2, '$'..i , v[i] );
        txt2 = string.gsub(txt2, '$'..k2 , v2 );
      end
      --txt = txt .. txt2
    else
      -- mw.log('item[' .. i .. ']:' .. type(v) .. ' -> ' .. v)
      --txt = txt .. string.gsub(format_str,'$1' , v );
      txt2 = string.gsub(format_str,'$1' , v )
    end
    txt = txt .. txt2
  end
  --mw.log('return txt: - - - - -\n' .. txt .. '\n- - - - -')
  return txt
end

function split( str, reps )
  local r = {}
  if string.find(str, reps) == nil then
    table.insert(r,str)
  else
    string.gsub(str,'[^'..reps..']+',function ( w )
      table.insert(r,w)
    end)
  end
  return r
end
function table_leng(t)
  local leng=0
  for k, v in pairs(t) do
    leng=leng+1
  end
  return leng;
end

function p.list_format(frame)
  --mw.log()
  --mw.logObject(frame , '[frame]')
  --mw.logObject(frame.args , '[frame-args]')
  arg_json_txt = frame.args.json
  if arg_json_txt == nil then
  	return '[Error]json is null'
  else
  	if arg_json_txt == 'Template' then
  		
      cfp_arg_txt = frame.args.txt
      cfp_arg_separator = frame.args.separator
	  
	  --mw.logObject(mw.getCurrentFrame():getParent():getArgument('txt') , '[Template-arg:txt]')
	  
  	  cfp_args = mw.getCurrentFrame():getParent().args;
  	  --mw.logObject(cfp_args , '[Template-args]')
  	  arg2 = {}
  	  skip_index = 2
  	  if type(cfp_args.txt) == 'string' or type(cfp_args['文本']) == 'string' then
  	  	skip_index = 1
  	  end
  	  for k, v in pairs(cfp_args) do
  	  	if (type(k) == 'number') and (k > skip_index) then
	      -- mw.log("k=" .. k .. ", v=" .. v)
  	      arg2[k] = split(v , ',')
  	    end
      end
	  --mw.log('[Template-txt(str)]: ' .. cfp_arg_txt)
	  --mw.log('[Template-separator(str)]: ' .. cfp_arg_separator)
	  --mw.logObject(arg2,'[Template-json(json)]')
	  return frame:preprocess( p._list_format(cfp_arg_txt , cfp_arg_separator , arg2) )
  	else
	  --mw.log('[json(str)]: ' .. arg_json_txt)
	  arg2 = mw.text.jsonDecode(arg_json_txt)
	  --mw.log('[txt(str)]: ' .. frame.args.txt)
	  --mw.logObject(arg2 , '[json(json)]')
	  return frame:preprocess( p._list_format(frame.args.txt, frame.args.separator, arg2) )
	end
  end
end

return p