Modulo:import-loaddata-list-table
MODULO | ||
Memtesto disponeblas sur la paĝo Vikivortaro:Testo-tbllingvoj. |
local exporttable = {}
local function lfifontsizetit (strtexto, numlevel)
local strsize = '200' -- preASSume -- for special level -1
if (numlevel==0) then
strsize = '150'
end--if
if (numlevel==1) then
strsize = '130'
end--if
if (numlevel==2) then
strsize = '110'
end--if
if (numlevel>2) then
strsize = '90'
end--if
strtexto = '<b><span style="font-size:' .. strsize .. '%;">' .. strtexto .. '</span></b>'
return strtexto
end--function lfifontsizetit
------------------------------------------------------------------------
local function lfifont90nbsize (strtexxto)
strtexxto = '<span style="font-size:90%;">' .. strtexxto .. '</span>'
return strtexxto
end--function lfifont90nbsize
------------------------------------------------------------------------
local function lfileftlevel (numleewel)
local strlfandstars = string.char(10)
while true do
strlfandstars = strlfandstars .. '*' -- ONE star at level ZERO needed too
if (numleewel==0) then
break
end--if
numleewel = numleewel - 1
end--while
return strlfandstars
end--function lfileftlevel
------------------------------------------------------------------------
local function lfhmini3ksani (varanytype)
local strtypeofit = ''
local strdescription = ''
local num39len = 0
local num39index = 1 -- ONE-based
local numsigno = 0
local numprevious = 0
local boohtmlenc = false
local boovisienc = false
strtypeofit = type(varanytype)
strdescription = 'type ' .. strtypeofit
if ((strtypeofit=='boolean') or (strtypeofit=='number')) then
strdescription = strdescription .. ' and value ' .. tostring (varanytype)
end--if
if (strtypeofit=='string') then
strdescription = strdescription .. ' and value "' -- begin quot here
num39len = string.len (varanytype)
while true do
boohtmlenc = false -- reset on
boovisienc = false -- every iteration
if (num39index>num39len) then -- ONE-based
break
end--if
numsigno = string.byte (varanytype,num39index,num39index)
if ((numsigno<=42) or (numsigno==58) or (numsigno==60) or (numsigno==62) or (numsigno==91) or (numsigno==93) or (numsigno>122)) then
boohtmlenc = true
end--if
if ((numsigno<32) or (numsigno>126)) then
boovisienc = true -- overrides "boohtmlenc"
end--if
if ((numsigno==32) and ((numprevious==32) or (num39index==1) or (num39index==num39len))) then
boovisienc = true -- overrides "boohtmlenc"
end--if
if (boovisienc) then
strdescription = strdescription .. '{' .. tostring (numsigno) .. '}'
else
if (boohtmlenc) then
strdescription = strdescription .. '&#' .. tostring (numsigno) .. ';'
else
strdescription = strdescription .. string.char (numsigno)
end--if
end--if
if ((num39len>3000) and (num39index==1000)) then
num39index = num39len - 1000
strdescription = strdescription .. '" ... "'
else
num39index = num39index + 1 -- ONE-based
end--if
numprevious = numsigno
end--while
strdescription = strdescription .. '"' -- don't forget final quot
end--if (strtypeofit=='string') then
return strdescription
end--function lfhmini3ksani
------------------------------------------------------------------------
-- Local function LFHALLTOSTRING
-- Only for "lfhcompareidiotsafe".
local function lfhalltostring (varwhatever)
local strtypeofstuff = ''
strtypeofstuff = type(varwhatever)
if (strtypeofstuff=='number') then
strtypeofstuff = '0'
end--if
if (strtypeofstuff=='string') then
strtypeofstuff = '1'
end--if
strtypeofstuff = strtypeofstuff .. tostring(varwhatever)
return strtypeofstuff
end--function lfhalltostring
------------------------------------------------------------------------
-- Local function LFHCOMPAREIDIOTSAFE
-- The sortorder is:
-- * number
-- * string
-- * everything else (boolean -> function -> table)
-- For example 5 will be less than "4".
-- Only for "table.sort".
local function lfhcompareidiotsafe (varaa, varbb)
local booaaisless = false
booaaisless = (lfhalltostring(varaa)<lfhalltostring(varbb))
return booaaisless
end--function lfhcompareidiotsafe
------------------------------------------------------------------------
-- Local function LFHTABLEANAL
-- we must use "pairs" since the incoming table could be metaized
local function lfhtableanal (tabnko)
local taballkeystring = {} -- ONE-based
local taballcetvalues = {} -- ONE-based
local numkatrol = 0
local numkeynumber = 0
local numkeystring = 0
local numkeycetera = 0
local numkey77min = 999999
local numkey77max = -999999
local boodone = false
for kkk,vvv in pairs(tabnko) do
boodone = false
if (type(kkk)=='number') then
if ((kkk>=-999000) and (kkk<=999000)) then -- protect against insanity
numkey77min = math.min (numkey77min,kkk)
numkey77max = math.max (numkey77max,kkk)
numkeynumber = numkeynumber + 1
boodone = true
end--if
end--if
if (type(kkk)=='string') then
numkatrol = string.len(kkk)
if ((numkatrol~=0) and (numkatrol<=50)) then -- protect against insanity
taballkeystring [numkeystring+1] = kkk -- ONE-based for "table.sort"
numkeystring = numkeystring + 1
boodone = true
end--if
end--if
if (not boodone) then
taballcetvalues [numkeycetera+1] = vvv -- ONE-based for "table.sort"
numkeycetera = numkeycetera + 1 -- here we directly store the value !!!
end--if
end--for
table.sort(taballkeystring) -- all are strings -- otherwise the sortorder is messy
table.sort(taballcetvalues,lfhcompareidiotsafe) -- various types -- otherwise the sortorder is messy
return numkeynumber, numkey77min, numkey77max, numkeystring, numkeycetera, taballkeystring, taballcetvalues
end--function lfhtableanal
------------------------------------------------------------------------
local function lfhtablst (tabobscure, numincomlevel)
local varta3mp = 0
local tablisto = {}
local tabvaluo = {}
local strte7mp = ''
local strgg = ''
local strkeystring = ''
local numtitlevel = 0
local numindax = 0
local numjumlahnum = 0
local numqqmin = 0
local numqqmax = 0
local numjumlahstr = 0
local numjumlahcet = 0
numtitlevel = numincomlevel * 2 -- consuming 2 star levels per table level
numjumlahnum, numqqmin, numqqmax, numjumlahstr, numjumlahcet, tablisto, tabvaluo = lfhtableanal(tabobscure)
strte7mp = 'The table (level ' .. tostring(numincomlevel) .. ')'
if ((numjumlahnum==0) and (numjumlahstr==0) and (numjumlahcet==0)) then
strte7mp = strte7mp .. ' is empty'
else
strte7mp = strte7mp .. ' contains '
if (numjumlahnum==0) then
strte7mp = strte7mp .. 'NO numeric keys'
end--if
if (numjumlahnum==1) then
strte7mp = strte7mp .. 'a single numeric key equal ' .. tostring (numqqmin)
end--if
if (numjumlahnum>=2) then
strte7mp = strte7mp .. tostring (numjumlahnum) .. ' numeric keys ranging from ' .. tostring (numqqmin) .. ' to ' .. tostring (numqqmax)
end--if
strte7mp = strte7mp .. ' and ' .. tostring (numjumlahstr) .. ' string keys and ' .. tostring (numjumlahcet) .. ' other keys'
end--if
strgg = lfileftlevel(numtitlevel) .. lfifontsizetit((strte7mp .. '.'),numtitlevel)
if (numjumlahnum~=0) then
strgg = strgg .. lfileftlevel(numtitlevel) .. lfifontsizetit('Numeric keys:',numtitlevel)
numindax = numqqmin
while true do
if (numindax>numqqmax) then
break
end--if
varta3mp = tabobscure[numindax]
if (varta3mp~=nil) then
strte7mp = tostring (numindax) .. ' -> ' .. lfhmini3ksani(varta3mp) -- no quot
strgg = strgg .. lfileftlevel(numtitlevel+1) .. lfifont90nbsize(strte7mp)
if ((type(varta3mp)=='table') and (numincomlevel<=6)) then
strgg = strgg .. lfhtablst (varta3mp,(numincomlevel+1)) -- REKURSI
end--if
end--if (varta3mp~=nil) then
numindax = numindax + 1
end--while
end--if
if (numjumlahstr~=0) then
strgg = strgg .. lfileftlevel(numtitlevel) .. lfifontsizetit('String keys:',numtitlevel)
numindax = 0
while true do
if (numindax>=numjumlahstr) then -- index is ZERO-based
break
end--if
strkeystring = tablisto [numindax+1] -- ONE-based for "table.sort"
if (type(strkeystring)=='string') then
varta3mp = tabobscure[strkeystring]
if (varta3mp~=nil) then
strte7mp = '"' .. strkeystring .. '" -> ' .. lfhmini3ksani(varta3mp)
strgg = strgg .. lfileftlevel(numtitlevel+1) .. lfifont90nbsize(strte7mp)
if ((type(varta3mp)=='table') and (numincomlevel<=6)) then
strgg = strgg .. lfhtablst (varta3mp,(numincomlevel+1)) -- REKURSI
end--if
end--if (varta3mp~=nil) then
end--if
numindax = numindax + 1
end--while
end--if
if (numjumlahcet~=0) then -- no recursion here
strgg = strgg .. lfileftlevel(numtitlevel) .. lfifontsizetit('Other keys:',numtitlevel)
numindax = 0
while true do
if (numindax>=numjumlahcet) then -- index is ZERO-based
break
end--if
varta3mp = tabvaluo [numindax+1] -- ONE-based for "table.sort"
if (varta3mp~=nil) then
strte7mp = '?? -> ' .. lfhmini3ksani(varta3mp)
strgg = strgg .. lfileftlevel(numtitlevel+1) .. lfifont90nbsize(strte7mp)
end--if (varta3mp~=nil) then
numindax = numindax + 1
end--while
end--if
if (numincomlevel==0) then
strgg = strgg .. string.char(10) -- terminate very last line
end--if
return strgg
end--function lfhtablst
------------------------------------------------------------------------
function exporttable.ek (arxframent)
-- general unknown type and special type "args" AKA "arx"
local vartymp = 0
local arxsomons = 0 -- metaized "args" from our own or caller's "frame"
-- general str
local strimptarget = ''
local strimperror = ''
local strreport = ''
-- general num
local numerr = 0
-- general boo
local boogotit = false
------------------------------------------------------------------------
---- ARX ----
arxsomons = arxframent.args -- "args" from our own "frame"
if (type(arxsomons)~='table') then
arxsomons = {} -- guard against indexing error from our own
numerr = 1 -- #E01 internal
end--if
if (arxsomons['caller']=='true') then
arxsomons = arxframent:getParent().args -- "args" from caller's "frame"
end--if
if (type(arxsomons)~='table') then
arxsomons = {} -- guard against indexing error again
numerr = 1 -- #E01 internal
end--if
---- IMPORT ----
strimptarget = arxsomons[1]
if (type(strimptarget)=='string') then
boogotit,vartymp = pcall(mw.loadData,strimptarget) -- could crash here
if (boogotit) then
boogotit = (type(vartymp)=='table') -- unlikely to fail here
else
strimperror = tostring(vartymp) -- seize error message
end--if
end--if
if (not boogotit) then
numerr = 2 -- #E02
end--if
---- LIST ----
if (numerr==0) then
strreport = lfifontsizetit('Imported table from "' .. strimptarget .. '"<br>',-1) .. lfhtablst (vartymp,0)
else
strreport = 'ERR: ' .. tostring(numerr)
if (strimperror~='') then
strreport = strreport .. ' import error: "' .. strimperror .. '"'
end--if
end--if
---- RETURN THE JUNK STRING ----
return strreport
end--function exporttable.ek
---- RETURN THE JUNK LUA TABLE ----
return exporttable