Jump to content

Module:TextileTable

Checked
From Wikipedia

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

local p = {}

function p.error ( message )
	return '<strong class="error">エラー:' ..message ..'</strong>'
end

function p.main( frame )
  local args = require( 'Module:Arguments' ).getArgs( frame, {
    wrappers = 'Template:TextileTable', trim = false, removeBlanks = false
  })

  local deb = ''

  local argsOnlyIntKey = {} -- 表データを抽出
  for k, v in pairs(args) do
    local idx = tonumber(k)
    if idx ~= nil then
      argsOnlyIntKey[idx] = v
    end
  end

  local content = ''
  local colIdx = 1
  for k, v in pairs(argsOnlyIntKey) do
    if k == #argsOnlyIntKey and not v:match'%S' then break end
    for val in v:gmatch'\n?%S*' do
      local cell = val
      if val:match'\n' ~= nil then
        content = content..'\n|-\n'
        colIdx = 1
        cell = string.gsub(val, '\n', '')
      end
      if #cell ~= 0 then
        local trimed = cell:gsub('^%s+', ''):gsub('%s+$', '')
        local arg2 = colIdx == 1 and '' or '|'
        content = content..'|'..arg2..trimed
        colIdx = colIdx + 1
      end
    end
  end

  return '{|class="wikitable"\n'..content..'\n|}'..deb
end

return p