Module:Sandbox/Thoken/Texts

Source: Wikipedia, the free encyclopedia.
local debuglog="\n:debuglog:"
local valid,duty={},{}

local function show(table)
  local r=" table: "
  for k,v in pairs(table) do
    r=r..k..":"..tostring(v).."; "
  end
  return r
end

local function len(table)
  local r=0
  for k,_ in pairs(table) do
    r=r+1
  end
  return r
end

local function Scanner(key,buf) -- returns anonymous function to scan a single table entry
  local pos=0
  return function(beg)
    pos=beg or pos+1
    local k=key[pos]; local v=buf[k]
    if valid[k] then debuglog=debuglog..pos..":"..k..":"..valid[k]..":"..v.." | " end
    return pos, k, v, valid[k]
  end
end

local function class(value)
  return type(value)  -- to be replaced
end
 
local function TemplArgs(table)  -- initialising tables key, buf - InitArgsClasses to be launched first
  local key,buf={},{}; local pos=0; local d=len(duty)
  debuglog=debuglog.." #duty:"..d.." "..show(duty)
  for k,v in pairs(table) do
    if not valid[k] then debuglog=debuglog.." <key unknown> "..k.."; "
    else 
      if valid[k]~=class(v) then debuglog=debuglog.." <key has illegal value> "..k.."; "
      else pos=pos+1
        key[pos]=k; buf[k]=v
      end
      if duty[k] and v then d=d-1 end
    end
  end
  if 0<d then debuglog=debuglog.." <mandatory key(s) undefined or missing> "..d.." " end
  debuglog=debuglog.." - buffered args: "..pos.."\n"
  return key,buf
end

local function InitArgsClasses(table)  -- initialising tables valid, duty - todo: get argument classes from template page
  valid={[1]='string', [3]='string', ng='number', ngs='number', nn='number', np='number', nk='number', ne='number', Hauptstadt='string', [5]='string', Insel1='string', Insel2='string', Insel3='string', Insel5='string', Insel6='string', Insel7='string', Journal1='string', Journal2='string', Journal3='number'}
  duty={[1]=true, Hauptstadt=true, [3]=true, Insel1=true, undefined=true}
end

local p={}
function p.main(frame)
  InitArgsClasses(frame.args)
  local key,buf=TemplArgs(frame:getParent().args)
  local S=Scanner(key,buf)
  local p,k,v,c=S(); local a=""
  while c do
    a=a..p..":"..k..":"..v.."; " -- ...handle... -> wikitext[k] (table of strings)
    p,k,v,c=S()
  end
  return a..debuglog
end
return p