MODULO
Memtesto ne disponeblas.

--[===[

MODULE "MFARADO" (kategorio vortfarado vortgrupigado)

"eo.wiktionary.org/wiki/Modulo:mfarado" <!--2024-Nov-xx-->

Purpose: brews visible content (box) and 2 or more cat insertions
         from pagename

Utilo: kreas videblan enhavon (skatolon) kaj 2 aux pli kategorienmetojn
       el pagxonomo

Manfaat: membutat isi terlihat (kotak) dan 2 atau lebih masukan kategori
         dari nama halaman

Syfte: skapar synligt innehaall (ruta) och 2 eller fler kategoriinlaeggningar
       fraan sidnamn

Used by templates / Uzata far sxablonoj / Digunakan oleh templat:
* kat-farado (EO)

Required submodules / Bezonataj submoduloj / Submodul yang diperlukan:
* "loaddata-tbllingvoj" T76 in turn requiring template "tbllingvoj" (EO)
* "loaddata-tblbahasa" T76 in turn requiring template "tblbahasa" (ID)
* "mrooteo" in turn requiring template "radikoj" (only if activated)

Required images:
* "File:Hidden icon.png"

Required templates:
* YES, see above

This module can accept parameters whether sent to itself (own frame) or
to the caller (caller's frame). If there is a parameter "caller=true"
on the own frame then that own frame is discarded in favor of the
caller's one.

Incoming: * 1 ordinary anonymous and optional parameter
            * only permitted value is "K" (uppercase letter) to indicate
              "compound quasi-root", otherwise it must be omitted altogether,
              crucial for -eo-, permitted for other languages, gives an extra
              category "Kunmetita kvazauxelemento" only for -eo- (without lang
              name, consistently with other categories as named by "mrooteo")
          * 3 hidden named and optional parameters
            * "pagenameoverridetestonly=" can directly cause #E01
            * "nsnumberoverridetestonly=" can indirectly cause #E10
            * "detrc=" no error possible

Returned: * one string with visible part (HTML box) and category part

Most of the relevant information is peeked from the pagename and thus
ordinary parameters are barely needed.

To be called from category namespace only.

Errors:
* we generate errors #E01 ... #E16
* we import errors from two submodules

]===]

local exporttable = {}

require('strict')

------------------------------------------------------------------------

---- CONSTANTS [O] ----

------------------------------------------------------------------------

-- uncommentable EO vs ID constant strings (core site-related features)

      local constrpriv = "eo"                            -- EO (privileged site language)
        -- local constrpriv = "id"                          -- ID (privileged site language)
      local constringvoj = "Modulo:loaddata-tbllingvoj"  -- EO
        -- local constringvoj = "Modul:loaddata-tblbahasa"  -- ID
      local constrrteo   = "Modulo:mrooteo"              -- EO
        -- local constrrteo   = "Modul:mrooteo"             -- ID
      local constrapxq = "Aldono"               -- EO (used only via "lfikataldigu") !!!FIXME!!! peek this
        -- local constrapxq = "Lampiran"           -- ID (used only via "lfikataldigu")
      local constrkatq = "Kategorio"            -- EO (used only via "lfikataldigu")
        -- local constrkatq = "Kategori"           -- ID (used only via "lfikataldigu")

-- constant table -- ban list -- add obviously invalid access codes (2-letter or 3-letter) only

  -- length of the list is NOT stored anywhere, the processing stops
  -- when type "nil" is encountered, used by "lfivalidatelnkoadv" only

  -- controversial codes (sh sr hr), (zh cmn)
  -- "en.wiktionary.org/wiki/Wiktionary:Language_treatment" excluded languages
  -- "en.wikipedia.org/wiki/Spurious_languages"
  -- "iso639-3.sil.org/code/art" only valid in ISO 639-2
  -- "iso639-3.sil.org/code/gem" only valid in ISO 639-2 and 639-5, "collective"
  -- "iso639-3.sil.org/code/zxx" "No linguistic content"

  local contabisbanned = {}
  contabisbanned = {'by','dc','ll','jp','art','deu','eng','epo','fra','gem','ger','ido','lat','por','rus','spa','swe','tup','zxx'} -- 1...19

-- surrogate transcoding table (only needed for EO)

  local contabtransluteo = {}
  contabtransluteo[ 67] = 0xC488 -- CX
  contabtransluteo[ 99] = 0xC489 -- cx
  contabtransluteo[ 71] = 0xC49C -- GX
  contabtransluteo[103] = 0xC49D -- gx
  contabtransluteo[ 74] = 0xC4B4 -- JX
  contabtransluteo[106] = 0xC4B5 -- jx
  contabtransluteo[ 83] = 0xC59C -- SX
  contabtransluteo[115] = 0xC59D -- sx
  contabtransluteo[ 85] = 0xC5AC -- UX breve
  contabtransluteo[117] = 0xC5AD -- ux breve

-- constant strings (error circumfixes)

  local constrelabg = '<span class="error"><b>'  -- lagom whining begin
  local constrelaen = '</b></span>'              -- lagom whining end
  local constrlaxhu = '&nbsp;&#42;&#42;&nbsp;'   -- lagom -> huge circumfix " ** "

-- uncommentable EO vs ID string (caller name for error messages)

  local constrkoll = 'sxablono "kat-farado"'  -- EO augmented name of the caller (semi-hardcoded, we do NOT peek it) !!!FIXME!!!
  -- local constrkoll = 'templat "kat-farado"'   -- ID augmented name of the caller (semi-hardcoded, we do NOT peek it)

-- uncommentable EO vs ID constant table (error messages)

  -- #E02...#E99, note that #E00 and #E01 are NOT supposed to be included here

  local contaberaroj = {}
  contaberaroj[ 8] = 'Maksimume unu parametro estas transprenata far \\@, legu gxian dokumentajxon'   -- EO #E08 !!!FIXME!!! change to unknown param #E06
  -- contaberaroj[ 8] = 'Tidak lebih dari satu parameter bisa dimakan oleh \\@, bacalah dokumentasinya'  -- ID #E08
  contaberaroj[12] = 'Submodulo "mrooteo" ne redonis tabelon'                             -- EO #E12 !!!FIXME!!! change to #E04
  -- contaberaroj[12] = 'Submodul "mrooteo" gagal memberi kembali tabel'                     -- EO #E12

  contaberaroj[10] = 'Erara nomspaco (estu 14, "Kategorio:") por \\@'                     -- EO #E10
  -- contaberaroj[10] = 'Ruang nama salah (sebaiknya 14, "Kategori:") untuk \\@'             -- ID #E10
  contaberaroj[11] = 'Evidente nevalida lingvokodo en \\@'                                -- EO #E11
  -- contaberaroj[11] = 'Kode bahasa jelas-jelas salah dalam \\@'                            -- ID #E11
  contaberaroj[14] = 'Erara pagxonomo (neniu el 3) por \\@'                               -- EO #E14
  -- contaberaroj[14] = 'Nama halaman salah (tidak satu dari 3) untuk \\@'                   -- ID #E14
  contaberaroj[15] = 'Erara pagxonomo (cetere) por \\@'                                   -- EO #E15
  -- contaberaroj[15] = 'Nama halaman salah (secara lain) untuk \\@'                         -- ID #E15
  contaberaroj[16] = 'La sola permesita valoro de anonima parametro por \\@ estas "K"'    -- EO #E16
  -- contaberaroj[16] = 'Isi satu-satunya parameter anonim yang benar untuk \\@ adalah "K"'  -- ID #E16

  -- uncommentable EO vs ID constant tables (boasting)

  -- "numowntyp" -- 0 invalid | 1 vorto 2 frazo 3 vortgrupo

  local contabboasting = {}
  contabboasting[1] = "En cxi tiu kasxita kategorio trovigxas vortoj enhavantaj la morfemon" -- follows morpheme type by letter and name
  contabboasting[2] = "En cxi tiu kasxita kategorio trovigxas frazoj enhavantaj la vorton"
  contabboasting[3] = "En cxi tiu kasxita kategorio trovigxas vortgrupoj enhavantaj la vorton"
  contabboasting[10] = "Kunmetita kvazauxelemento"

  -- uncommentable EO vs ID constant tables (dual use: mortyp:s and scripts via "lfhseizetext")

  -- "nummortyp" -- 0 invalid | 67 C 73 I 77 M 78 N 80 P 85 U

  local contabmosc = {}
  contabmosc["C"]   = "cirkumfikson"    -- EO
  contabmosc["I"]   = "infikson"        -- EO
  contabmosc["M"]   = "memstaran"       -- EO
  contabmosc["N"]   = "nememstaran"     -- EO
  contabmosc["P"]   = "prefikson"       -- EO
  contabmosc["U"]   = "sufikson"        -- EO
  contabmosc["zhT"] = "tradicia"        -- EO
  contabmosc["zhS"] = "simpligita"      -- EO
  contabmosc["zhK"] = "komuna"          -- EO
  contabmosc["zhP"] = "pinjina"         -- EO
  contabmosc["jaK"] = "kangxia"         -- EO
  contabmosc["jaH"] = "hiragana"        -- EO
  contabmosc["jaR"] = "romanigita"      -- EO
  contabmosc["hrC"] = "cirila"          -- EO
  contabmosc["hrL"] = "latina"          -- EO

  -- constant table (HTML)

  local contabhtml = {}
  contabhtml[0] = '<table style="border:1px solid #A0A0A0; background:#F0F0F0; width:95%; margin:0.6em auto 0.6em auto; padding:0.6em; text-align:justify;">'
  contabhtml[1] = '<tr><td style="width:30px;">[[File:Hidden icon.png|50px]]</td><td style="padding-left:1em;">'
  contabhtml['lowend'] = '</td></tr></table>'

  -- constants to control behaviour from source AKA semi-hardcoded parameters

  local conboouseeox  = true   -- "true" to enable eo-specific submodule
  local conbookodlng  = false  -- "true" to allow long codes like "zh-min-nan"
  local conboomiddig  = false  -- "true" to allow middle digit "s7a"

------------------------------------------------------------------------

---- SPECIAL STUFF OUTSIDE MAIN [B] ----

------------------------------------------------------------------------

  -- SPECIAL VAR:S

local qldingvoj = {}     -- type "table" and nested
local qeorooteo = {}     -- type "table" with type "function" inside
local qboodetrc = true   -- from "detrc=true" but default is "true" !!!
local qstrtrace = '<br>' -- for main & sub:s, debug report request by "detrc="
local qbooguard = false  -- only for the guard test, pass to other var ASAP

  -- GUARD AGAINST INTERNAL ERROR & TWO IMPORTS

qbooguard = ((type(constrpriv)~='string') or (type(constringvoj)~='string') or (type(constrrteo)~='string') or (type(constrapxq)~='string') or (type(constrkatq)~='string') or (type(constrkoll)~='string'))
if (not qbooguard) then
  qldingvoj = mw.loadData(constringvoj) -- can crash here
  qbooguard = (type(qldingvoj)~='table') -- seems to be always false
end--if
if ((not qbooguard) and conboouseeox) then
  qeorooteo = require(constrrteo) -- can crash here despite guarding ??
  qbooguard = (type(qeorooteo)~='table')
end--if

------------------------------------------------------------------------

---- DEBUG FUNCTIONS [D] ----

------------------------------------------------------------------------

-- Local function LFDTRACEMSG

-- Enhance upvalue "qstrtrace" with fixed text.

-- for variables the other sub "lfdshowvar" is preferable but in exceptional
-- cases it can be justified to send text with values of variables to this sub

-- no size limit

-- upvalue "qstrtrace" must NOT be type "nil" on entry (is inited to "<br>")

-- uses upvalue "qboodetrc"

local function lfdtracemsg (strshortline)
  if (qboodetrc and (type(strshortline)=='string')) then
    qstrtrace = qstrtrace .. strshortline .. '.<br>' -- dot added !!!
  end--if
end--function lfdtracemsg

------------------------------------------------------------------------

-- Local function LFDIMPORTREPORT

-- Enhance upvalue "qstrtrace" with imported report.

-- use this one to import detrc text from submodule

-- upvalue "qstrtrace" must NOT be type "nil" on entry (is inited to "<br>")

-- uses upvalue "qboodetrc"

local function lfdimportreport (strshortlineorbigtext)
  local strseparator = ''
  if (qboodetrc and (type(strshortlineorbigtext)=='string')) then
    strseparator = '<br>&#35;######################&#35;<br>'
    qstrtrace = qstrtrace .. strseparator .. strshortlineorbigtext .. strseparator .. '<br>'
  end--if
end--function lfdimportreport

------------------------------------------------------------------------

-- Local function LFDMINISANI

-- Input  : * strdangerous -- must be type "string", empty legal
--          * numlimitdivthree

-- Output : * strsanitized -- can happen to be quasi-empty with <<"">>

-- To be called from "lfdshowvcore" <- "lfdshowvar" only.

-- * we absolutely must disallow: cross "#" 35 | apo "'" 39 |
--   star "*" 42 | dash 45 | colon 58 | "<" 60 | ">" 62 | "[" 91 | "]" 93
-- * spaces are showed as "{32}" if repetitive or at begin or at end

local function lfdminisani (strdangerous, numlimitdivthree)

  local strsanitized = '"' -- begin quot
  local num38len = 0
  local num38index = 1 -- ONE-based
  local num38signo = 0
  local num38prev = 0
  local boohtmlenc = false
  local boovisienc = false

  num38len = string.len (strdangerous)
  while true do
    boohtmlenc = false -- % reset on
    boovisienc = false -- % every iteration
    if (num38index>num38len) then -- ONE-based
      break -- done string char after char
    end--if
    num38signo = string.byte (strdangerous,num38index,num38index)
    if ((num38signo<43) or (num38signo==45) or (num38signo==58) or (num38signo==60) or (num38signo==62) or (num38signo==91) or (num38signo==93) or (num38signo>122)) then
      boohtmlenc = true
    end--if
    if ((num38signo<32) or (num38signo>126)) then
      boovisienc = true -- overrides "boohtmlenc"
    end--if
    if ((num38signo==32) and ((num38prev==32) or (num38index==1) or (num38index==num38len))) then
      boovisienc = true -- overrides "boohtmlenc"
    end--if
    if (boovisienc) then
      strsanitized = strsanitized .. '{' .. tostring (num38signo) .. '}'
    else
      if (boohtmlenc) then
        strsanitized = strsanitized .. '&#' .. tostring (num38signo) .. ';'
      else
        strsanitized = strsanitized .. string.char (num38signo)
      end--if
    end--if
    if ((num38len>(numlimitdivthree*3)) and (num38index==numlimitdivthree)) then
      num38index = num38len - numlimitdivthree -- jump forwards
      strsanitized = strsanitized .. '" ... "'
    else
      num38index = num38index + 1 -- ONE-based
    end--if
    num38prev = num38signo
  end--while
  strsanitized = strsanitized .. '"' -- don't forget final quot

  return strsanitized

end--function lfdminisani

------------------------------------------------------------------------

-- Local function LFDSHOWVCORE

-- Prebrew report about content of a variable including optional full
-- listing of a table with numeric and string keys.                             !!!FIXME!!!

-- Input  : * vardubious  -- content (any type including "nil" is acceptable)
--          * str77name   -- name of the variable (string)
--          * vardescri   -- optional comment, default empty, begin with "@" to
--                           place it before name of the variable, else after
--          * varlim77tab -- optional limit, limits both string keys and
--                           numeric keys, default ZERO no listing

-- Depends on functions :
-- [D] lfdminisani

local function lfdshowvcore (vardubious, str77name, vardescri, varlim77tab)

  local taballkeystring = {}
  local strtype = ''
  local strreport = ''
  local numindax = 0
  local numlencx = 0
  local numkeynumber = 0
  local numkeystring = 0
  local numkeycetera = 0
  local numkey77min = 999999
  local numkey77max = -999999
  local boobe77fore = false

  if (type(str77name)~='string') then
    str77name = '??' -- bite the bullet
  else
    str77name = '"' .. str77name .. '"'
  end--if

  if (type(vardescri)~='string') then
    vardescri = '' -- omit comment
  end--if
  if (string.len(vardescri)>=2) then
    boobe77fore = (string.byte(vardescri,1,1)==64) -- prefix "@"
    if (boobe77fore) then
      vardescri = string.sub(vardescri,2,-1) -- CANNOT become empty
    end--if
  end--if

  if (type(varlim77tab)~='number') then
    varlim77tab = 0 -- deactivate listing of a table
  end--if

  if ((vardescri~='') and (not boobe77fore)) then
    str77name = str77name .. ' (' .. vardescri .. ')' -- now a combo
  end--if

  strtype = type(vardubious)

  if (strtype=='table') then

    for k2k,v2v in pairs(vardubious) do
      if (type(k2k)=='number') then
        numkey77min = math.min (numkey77min,k2k)
        numkey77max = math.max (numkey77max,k2k)
        numkeynumber = numkeynumber + 1
      else
        if (type(k2k)=='string') then
          taballkeystring [numkeystring] = k2k
          numkeystring = numkeystring + 1
        else
          numkeycetera = numkeycetera + 1
        end--if
      end--if
    end--for

    strreport = 'Table ' .. str77name
    if ((numkeynumber==0) and (numkeystring==0) and (numkeycetera==0)) then
      strreport = strreport .. ' is empty'
    else
      strreport = strreport .. ' contains '
      if (numkeynumber==0) then
        strreport = strreport .. 'NO numeric keys'
      end--if
      if (numkeynumber==1) then
        strreport = strreport .. 'a single numeric key equal ' .. tostring (numkey77min)
      end--if
      if (numkeynumber>=2) then
        strreport = strreport .. tostring (numkeynumber) .. ' numeric keys ranging from ' .. tostring (numkey77min) .. ' to ' .. tostring (numkey77max)
      end--if
      strreport = strreport .. ' and ' .. tostring (numkeystring) .. ' string keys and ' .. tostring (numkeycetera) .. ' other keys'
    end--if

    if ((numkeynumber~=0) and (varlim77tab~=0)) then -- !!!FIXME!!!
      strreport = strreport .. ' ### content num keys :'
      numindax = numkey77min
        while true do
          if ((numindax>varlim77tab) or (numindax>numkey77max)) then
            break -- done table
          end--if
          strreport = strreport .. ' ' .. tostring(numindax) .. ' -> ' .. lfdminisani(tostring(vardubious[numindax]),30)
          numindax = numindax + 1
        end--while
    end--if

    if ((numkeystring~=0) and (varlim77tab~=0)) then -- !!!FIXME!!!
      strreport = strreport .. ' ### content string keys :'
    end--if

  else

      strreport = 'Variable ' .. str77name .. ' has type "' .. strtype .. '"'
      if (strtype=='string') then
        numlencx = string.len (vardubious)
        strreport = strreport .. ' and length ' .. tostring (numlencx)
        if (numlencx~=0) then
          strreport = strreport .. ' and content ' .. lfdminisani (vardubious,30)
        end--if
      else
        if (strtype~='nil') then
          strreport = strreport .. ' and content "' .. tostring (vardubious) .. '"'
        end--if
      end--if (strtype=='string') else

  end--if (strtype=='table') else

  if ((vardescri~='') and boobe77fore) then
    strreport = vardescri .. ' : ' .. strreport -- very last step
  end--if

  return strreport

end--function lfdshowvcore

------------------------------------------------------------------------

-- Local function LFDSHOWVAR

-- Enhance upvalue "qstrtrace" with report about content of a variable
-- including optional full listing of a table with numeric and string keys.     !!!FIXME!!!

-- Depends on functions :
-- [D] lfdminisani lfdshowvcore

-- upvalue "qstrtrace" must NOT be type "nil" on entry (is inited to "<br>")

-- uses upvalue "qboodetrc"

local function lfdshowvar (varduubious, strnaame, vardeskkri, vartabljjm)

  if (qboodetrc) then
    qstrtrace = qstrtrace .. lfdshowvcore (varduubious, strnaame, vardeskkri, vartabljjm) .. '.<br>' -- dot added !!!
  end--if

end--function lfdshowvar

------------------------------------------------------------------------

---- MATH FUNCTIONS [E] ----

------------------------------------------------------------------------

local function mathisintrange (numzjinput, numzjmin, numzjmax)
  local booisclean = false -- preASSume guilt
  if (type(numzjinput)=='number') then -- no non-numbers, thanks
    if (numzjinput==math.floor(numzjinput)) then -- no transcendental
      booisclean = ((numzjinput>=numzjmin) and (numzjinput<=numzjmax)) -- rang
    end--if
  end--if
  return booisclean
end--function mathisintrange

local function mathdiv (xdividens, xdivisero)
  local resultdiv = 0 -- DIV operator lacks in LUA :-(
  resultdiv = math.floor (xdividens / xdivisero)
  return resultdiv
end--function mathdiv

local function mathmod (xdividendo, xdivisoro)
  local resultmod = 0 -- MOD operator is "%" and bitwise AND operator lack too
  resultmod = xdividendo % xdivisoro
  return resultmod
end--function mathmod

------------------------------------------------------------------------

-- Local function MATHBITWRIT

-- Write bit selected by ZERO-based index assigning it to "1" or "0".

-- Depends on functions :
-- [E] mathdiv mathmod

local function mathbitwrit (numinkoming, numbityndex, boowrite)
  local numpatched = 0
  local numcountup = 0
  local numweight = 1 -- single bit value 1 -> 2 -> 4 -> 8 ...
  local boosinglebit = false
  while true do
    if ((numinkoming==0) and (numcountup>numbityndex)) then
      break -- we have run out of bits on BOTH possible sources
    end--if
    if (numcountup==numbityndex) then
      boosinglebit = boowrite -- overwrite bit
    else
      boosinglebit = (mathmod(numinkoming,2)==1) -- pick bit
    end--if
    numinkoming = mathdiv(numinkoming,2) -- shift right
    if (boosinglebit) then
      numpatched = numpatched + numweight -- add one bit rtl only if true
    end--if
    numcountup = numcountup + 1 -- count up here until we run out of bits
    numweight = numweight * 2
  end--while
  return numpatched
end--function mathbitwrit

------------------------------------------------------------------------

---- NUMBER CONVERSION FUNCTIONS [N] ----

------------------------------------------------------------------------

-- Local function LFNUMTO2DIGIT

-- Convert integer 0...99 to decimal ASCII string always 2 digits "00"..."99".

-- Depends on functions :
-- [E] mathisintrange mathdiv mathmod

local function lfnumto2digit (numzerotoninetynine)
  local strtwodig = '??' -- always 2 digits
  if (mathisintrange(numzerotoninetynine,0,99)) then
    strtwodig = tostring(mathdiv(numzerotoninetynine,10)) .. tostring(mathmod(numzerotoninetynine,10))
  end--if
  return strtwodig
end--function lfnumto2digit

------------------------------------------------------------------------

---- LOW LEVEL STRING FUNCTIONS [G] ----

------------------------------------------------------------------------

-- Local function LFGSTRINGRANGE

local function lfgstringrange (varvictim, nummini, nummaxi)
  local nummylengthofstr = 0
  local booveryvalid = false -- preASSume guilt
  if (type(varvictim)=='string') then
    nummylengthofstr = string.len(varvictim)
    booveryvalid = ((nummylengthofstr>=nummini) and (nummylengthofstr<=nummaxi))
  end--if
  return booveryvalid
end--function lfgstringrange

------------------------------------------------------------------------

-- Local function LFGPOKESTRING

-- Replace single octet in a string.

-- Input  : * strinpokeout -- empty legal
--          * numpokepoz   -- ZERO-based, out of range legal
--          * numpokeval   -- new value

-- This is inefficient by design of LUA. The caller is responsible to
-- minimize the number of invocations of this, in particular, not to
-- call if the new value is equal the existing one.

local function lfgpokestring (strinpokeout, numpokepoz, numpokeval)
  local numpokelen = 0
  numpokelen = string.len(strinpokeout)
  if ((numpokelen==1) and (numpokepoz==0)) then
    strinpokeout = string.char(numpokeval) -- totally replace
  end--if
  if (numpokelen>=2) then
    if (numpokepoz==0) then
      strinpokeout = string.char(numpokeval) .. string.sub (strinpokeout,2,numpokelen)
    end--if
    if ((numpokepoz>0) and (numpokepoz<(numpokelen-1))) then
      strinpokeout = string.sub (strinpokeout,1,numpokepoz) .. string.char(numpokeval) .. string.sub (strinpokeout,(numpokepoz+2),numpokelen)
    end--if
    if (numpokepoz==(numpokelen-1)) then
      strinpokeout = string.sub (strinpokeout,1,(numpokelen-1)) .. string.char(numpokeval)
    end--if
  end--if (numpokelen>=2) then
  return strinpokeout
end--function lfgpokestring

------------------------------------------------------------------------

-- test whether char is an ASCII digit "0"..."9", return boolean

local function lfgtestnum (numkaad)
  local boodigit = false
  boodigit = ((numkaad>=48) and (numkaad<=57))
  return boodigit
end--function lfgtestnum

------------------------------------------------------------------------

-- test whether char is an ASCII uppercase letter, return boolean

local function lfgtestuc (numkode)
  local booupperc = false
  booupperc = ((numkode>=65) and (numkode<=90))
  return booupperc
end--function lfgtestuc

------------------------------------------------------------------------

-- test whether char is an ASCII lowercase letter, return boolean

local function lfgtestlc (numcode)
  local boolowerc = false
  boolowerc = ((numcode>=97) and (numcode<=122))
  return boolowerc
end--function lfgtestlc

------------------------------------------------------------------------

local function lfgcmpsub (strbig, strtiny, numpos5sta)
  local numlen5big = 0
  local numlen5tiny = 0
  local boof5ound = false
  numlen5big = string.len (strbig)
  numlen5tiny = string.len (strtiny)
  if ((numlen5tiny>0) and ((numpos5sta+numlen5tiny)<=numlen5big)) then
    boof5ound = ( strtiny == (string.sub (strbig,(numpos5sta+1),(numpos5sta+numlen5tiny))) )
  end--if
  return boof5ound
end--function lfgcmpsub

------------------------------------------------------------------------

---- UTF8 FUNCTIONS [U] ----

------------------------------------------------------------------------

-- Local function LFULNUTF8CHAR

-- Evaluate length of a single UTF8 char in octet:s.

-- Input  : * numbgoctet  -- beginning octet of a UTF8 char

-- Output : * numlen1234x -- unit octet, number 1...4, or ZERO if invalid

-- Does NOT thoroughly check the validity, looks at ONE octet only.

local function lfulnutf8char (numbgoctet)
  local numlen1234x = 0
    if (numbgoctet<128) then
      numlen1234x = 1 -- $00...$7F -- ANSI/ASCII
    end--if
    if ((numbgoctet>=194) and (numbgoctet<=223)) then
      numlen1234x = 2 -- $C2 to $DF
    end--if
    if ((numbgoctet>=224) and (numbgoctet<=239)) then
      numlen1234x = 3 -- $E0 to $EF
    end--if
    if ((numbgoctet>=240) and (numbgoctet<=244)) then
      numlen1234x = 4 -- $F0 to $F4
    end--if
  return numlen1234x
end--function lfulnutf8char

------------------------------------------------------------------------

-- Local function LFUCASEREST

-- Adjust (restricted) case of a single letter (from ASCII + selectable
-- extra set from UTF8) or longer string. (this is REST)

-- Input  : * strinco6cs : single unicode letter (1 or 2 octet:s) or
--                         longer string
--          * booup6cas  : for desired output uppercase "true" and for
--                         lowercase "false"
--          * boodo6all  : "true" to adjust all letters, "false"
--                         only beginning letter
--          * strsel6set : "ASCII" (default, empty string or type "nil"
--                         will do too) "eo" "sv" (value "GENE" NOT here)

-- Output : * strinco6cs

-- Depends on functions : (this is REST)
-- [U] lfulnutf8char
-- [G] lfgpokestring lfgtestuc lfgtestlc
-- [E] mathdiv mathmod mathbitwrit

-- This process never changes the length of a string in octet:s. Empty string
-- on input is legal and results in an empty string returned. When case is
-- adjusted, a 1-octet or 2-octet letter is replaced by another letter of same
-- length. Unknown valid char:s (1-octet ... 4-octet) are copied. Broken UTF8
-- stream results in remaining part of the output string (from 1 char to
-- complete length of the incoming string) filled by "Z".

-- Defined sets:
-- "eo" 2 x 6 uppercase and lowercase (CX GX HX JX SX UX cx gx hx jx sx ux)
--      upper CX $0108 GX $011C HX $0124 JX $0134 SX $015C UX $016C lower +1
-- "sv" 2 x 4 uppercase and lowercase (AE AA EE OE ae aa ee oe)
--      upper AE $00C4 AA $00C5 EE $00C9 OE $00D6 lower +$20

-- We peek max 2 values per iteration, and change the string in-place, doing
-- so strictly only if there indeed is a change. This is important for LUA
-- where the in-place write access must be emulated by means of a less
-- efficient function.

local function lfucaserest (strinco6cs, booup6cas, boodo6all, strsel6set)

  local numlong6den = 0 -- actual length of input string
  local numokt6index = 0
  local numlong6bor = 0 -- expected length of single char

  local numdel6ta = 0 -- quasi-signed +32 or -32 or +1 or -1 or ZERO

  local numcha6r = 0 -- UINT8 beginning char
  local numcha6s = 0 -- UINT8 later char (BIG ENDIAN, lower value here above)
  local numcxa6rel = 0 -- UINT8 code relative to beginning of block $00...$FF

  local numtem6p = 0

  local boowan6tlowr = false
  local boois6uppr = false
  local boois6lowr = false

  local boodo6adj = true -- preASSume innocence -- continue changing
  local boobotch6d = false -- preASSume innocence -- NOT yet botched

  booup6cas = not (not booup6cas)
  boowan6tlowr = (not booup6cas)
  numlong6den = string.len (strinco6cs)

  while true do -- genuine loop over incoming string (this is REST)

    if (numokt6index>=numlong6den) then
      break -- done complete string
    end--if
    if ((not boodo6all) and (numokt6index~=0)) then -- loop can skip index ONE
      boodo6adj = false
    end--if
    boois6uppr  = false -- preASSume on every iteration
    boois6lowr  = false -- preASSume on every iteration
    numdel6ta   = 0 -- preASSume on every iteration
    numlong6bor = 1 -- preASSume on every iteration

    while true do -- fake loop (this is REST)

      numcha6r = string.byte (strinco6cs,(numokt6index+1),(numokt6index+1))
      if (boobotch6d) then
        numdel6ta = 90 - numcha6r -- "Z" -- delta must be non-ZERO to write
        break -- fill with "Z" char:s
      end--if
      if (not boodo6adj) then
        break -- copy octet after octet
      end--if
      numlong6bor = lfulnutf8char(numcha6r)
      if ((numlong6bor==0) or ((numokt6index+numlong6bor)>numlong6den)) then
        numlong6bor = 1 -- reassign to ONE !!!
        numdel6ta = 90 - numcha6r -- "Z" -- delta must be non-ZERO to write
        boobotch6d = true
        break -- truncated char or broken stream
      end--if
      if (numlong6bor>=3) then
        break -- copy UTF8 char, no chance for adjustment
      end--if

      if (numlong6bor==1) then
        boois6uppr = lfgtestuc(numcha6r)
        boois6lowr = lfgtestlc(numcha6r)
        if (boois6uppr and boowan6tlowr) then
          numdel6ta = 32 -- ASCII UPPER->lower
        end--if
        if (boois6lowr and booup6cas) then
          numdel6ta = -32 -- ASCII lower->UPPER
        end--if
        break -- success with ASCII and one char almost done
      end--if

      numcha6s = string.byte (strinco6cs,(numokt6index+2),(numokt6index+2)) -- only $80 to $BF
      numcxa6rel = (mathmod(numcha6r,4)*64) + (numcha6s-128) -- 4 times 64

    if ((strsel6set=='eo') and ((numcha6r==196) or (numcha6r==197))) then
      numtem6p = mathbitwrit (numcxa6rel,0,false) -- bad way to do AND $FE
      if ((numtem6p==8) or (numtem6p==28) or (numtem6p==36) or (numtem6p==52) or (numtem6p==92) or (numtem6p==108)) then
        boois6uppr = (numtem6p==numcxa6rel) -- UC below, block of 1
        boois6lowr = not boois6uppr
        if (boois6uppr and boowan6tlowr) then
          numdel6ta = 1 -- UPPER->lower
        end--if
        if (boois6lowr and booup6cas) then
          numdel6ta = -1 -- lower->UPPER
        end--if
        break -- success with -eo- and one char almost done
      end--if
    end--if ((strsel6set=='eo') and ...

    if ((strsel6set=='sv') and (numcha6r==195)) then
      numtem6p = mathbitwrit (numcxa6rel,5,false) -- bad way to do AND $DF
      if ((numtem6p==196) or (numtem6p==197) or (numtem6p==201) or (numtem6p==214)) then
        boois6uppr = (numtem6p==numcxa6rel) -- UC below, block of 32
        boois6lowr = not boois6uppr
        if (boois6uppr and boowan6tlowr) then
          numdel6ta = 32 -- UPPER->lower
        end--if
        if (boois6lowr and booup6cas) then
          numdel6ta = -32 -- lower->UPPER
        end--if
        break -- success with -sv- and one char almost done
      end--if
    end--if ((strsel6set=='sv') and ...

      break -- finally to join mark -- unknown non-ASCII char is a fact :-(
    end--while -- fake loop -- join mark (this is REST)

    if ((numlong6bor==1) and (numdel6ta~=0)) then -- no risk of carry here
      strinco6cs = lfgpokestring (strinco6cs,numokt6index,(numcha6r+numdel6ta))
    end--if
    if ((numlong6bor==2) and (numdel6ta~=0)) then -- no risk of carry here
      strinco6cs = lfgpokestring (strinco6cs,(numokt6index+1),(numcha6s+numdel6ta))
    end--if
    numokt6index = numokt6index + numlong6bor -- advance in incoming string

  end--while -- genuine loop over incoming string (this is REST)

  return strinco6cs

end--function lfucaserest

------------------------------------------------------------------------

---- HIGH LEVEL STRING FUNCTIONS [I] ----

------------------------------------------------------------------------

-- Local function LFIKATALDIGU

-- Brew cat insertion (no extra colon ":") or link to
-- appendix from 3 elements.

local function lfikataldigu (strprefixx, strkataldnomo, strhintvisi)
  local strrbkma = ''
  if (type(strhintvisi)=='string') then
    strrbkma = '[[' .. strprefixx .. ':' .. strkataldnomo .. '|' .. strhintvisi .. ']]'
  else
    strrbkma = '[[' .. strprefixx .. ':' .. strkataldnomo .. ']]'
  end--if
  return strrbkma
end--function lfikataldigu

------------------------------------------------------------------------

-- Local function LFIVALIDATELNKOADV

-- Advanced test whether a string (intended to be a language code) is valid
-- containing only 2 or 3 lowercase letters, or 2...10 char:s and with some
-- dashes, or maybe a digit in middle position or maybe instead equals to "-"
-- or "??" and maybe additionally is not included on the ban list.

-- Input  : * strqooq -- string (empty is useless and returns
--                       "true" ie "bad" but cannot cause any major harm)
--          * booyesdsh -- "true" to allow special code dash "-"
--          * booyesqst -- "true" to allow special code doublequest "??"
--          * booloonkg -- "true" to allow long codes such as "zh-min-nan"
--          * boodigit -- "true" to allow digit in middle position
--          * boonoban -- (inverted) "true" to skip test against ban table

-- Output : * booisvaladv -- true if string is valid

-- Depends on functions :
-- [G] lfgtestnum lfgtestlc

-- Depends on constants :
-- * table "contabisbanned"

-- Incoming empty string is safe but type "nil" is NOT.

-- Dashes are tolerable (except in special code "-") only ("and" applies):
-- * if length is at least 4 char:s (if this is permitted at all)
-- * in inner positions
-- * NOT adjacent
-- * maximally TWO totally
-- There may be maximally 3 adjacent letters, this makes at least ONE dash
-- obligatory for length 4...7, and TWO dashes for length 8...10.

local function lfivalidatelnkoadv (strqooq, booyesdsh, booyesqst, booloonkg, boodigit, boonoban)

  local varomongkosong = 0 -- for check against the ban list
  local numchiiar = 0
  local numukurran = 0
  local numindeex = 0 -- ZERO-based -- two loops
  local numadjlet = 0 -- number of adjacent letters (max 3)
  local numadjdsh = 0 -- number of adjacent dashes (max 1)
  local numtotdsh = 0 -- total number of dashes (max 2)
  local booislclc = false
  local booisdigi = false
  local booisdash = false
  local booisvaladv = true -- preASSume innocence -- later final verdict here

  while true do -- fake (outer) loop

    if (strqooq=="-") then
      booisvaladv = booyesdsh
      break -- to join mark -- good or bad
    end--if
    if (strqooq=="??") then
      booisvaladv = booyesqst
      break -- to join mark -- good or bad
    end--if
    numukurran = string.len (strqooq)
    if ((numukurran<2) or (numukurran>10)) then
      booisvaladv = false
      break -- to join mark -- evil
    end--if
    if (not booloonkg and (numukurran>3)) then
      booisvaladv = false
      break -- to join mark -- evil
    end--if

    numindeex = 0
    while true do -- inner genuine loop over char:s
      if (numindeex>=numukurran) then
        break -- done -- good
      end--if
      numchiiar = string.byte (strqooq,(numindeex+1),(numindeex+1))
      booisdash = (numchiiar==45)
      booisdigi = lfgtestnum(numchiiar)
      booislclc = lfgtestlc(numchiiar)
      if (not (booislclc or booisdigi or booisdash)) then
        booisvaladv = false
        break -- to join mark -- inherently bad char
      end--if
      if (booislclc) then
        numadjlet = numadjlet + 1
      else
        numadjlet = 0
      end--if
      if (booisdigi and ((numukurran~=3) or (numindeex~=1) or (not boodigit))) then
        booisvaladv = false
        break -- to join mark -- illegal digit
      end--if
      if (booisdash) then
        if ((numukurran<4) or (numindeex==0) or ((numindeex+1)==numukurran)) then
          booisvaladv = false
          break -- to join mark -- illegal dash
        end--if
        numadjdsh = numadjdsh + 1
        numtotdsh = numtotdsh + 1 -- total
      else
        numadjdsh = 0 -- do NOT zeroize the total !!!
      end--if
      if ((numadjlet>3) or (numadjdsh>1) or (numtotdsh>2)) then
        booisvaladv = false
        break -- to join mark -- evil
      end--if
      numindeex = numindeex + 1 -- ZERO-based
    end--while -- inner genuine loop over char:s

    if (not boonoban) then -- if "yesban" then
      numindeex = 0
      while true do -- lower inner genuine loop
        varomongkosong = contabisbanned[numindeex+1] -- number of elem unknown
        if (type(varomongkosong)~="string") then
          break -- abort inner loop (then outer fake loop) due to end of table
        end--if
        numukurran = string.len (varomongkosong)
        if ((numukurran<2) or (numukurran>3)) then
          break -- abort inner loop (then outer fake loop) due to faulty table
        end--if
        if (strqooq==varomongkosong) then
          booisvaladv = false
          break -- abort inner loop (then outer fake loop) due to violation
        end--if
        numindeex = numindeex + 1 -- ZERO-based
      end--while -- lower inner genuine loop
    end--if (not boonoban) then

    break -- finally to join mark
  end--while -- fake loop -- join mark

  return booisvaladv

end--function lfivalidatelnkoadv

------------------------------------------------------------------------

-- Local function LFIFILLINX

-- Replace placeholders "\@" "\\@" or "\~" "\\~" by given substitute string.

-- Input  : * strbeforfill -- request string with placeholders to be filled
--                            in, no placeholders or empty input is useless
--                            but cannot cause major harm
--          * numaskikodo  -- ASCII code of placeholder, 64 for "@" or
--                            126 for "~"
--          * varsupstitu  -- substitute, either string (same content reused
--                            if multiple placeholders), or ZERO-based table
--                            (with one element per placeholder such as
--                            {[0]="none","neniu"}), length 1...60

-- Output : * strafterfill

-- Depends on functions :
-- [G] lfgstringrange

local function lfifillinx (strbeforfill, numaskikodo, varsupstitu)

  local varpfiller    = 0  -- risky picking
  local strufiller    = '' -- final validated filler
  local strafterfill  = ''
  local numlenbigtext = 0  -- len of strbeforfill
  local numsfrcindex  = 0  -- char index ZERO-based
  local numinsrtinde  = 0  -- index in table ZERO-based
  local numtecken0d   = 0
  local numtecken1d   = 0

  numlenbigtext = string.len (strbeforfill)

  while true do
    if (numsfrcindex>=numlenbigtext) then
      break -- empty input is useless but cannot cause major harm
    end--if
    numtecken0d = string.byte(strbeforfill,(numsfrcindex+1),(numsfrcindex+1))
    numsfrcindex = numsfrcindex + 1 -- INC here
    numtecken1d = 0 -- preASSume none
    if (numsfrcindex<numlenbigtext) then -- pick but do NOT INC
      numtecken1d = string.byte(strbeforfill,(numsfrcindex+1),(numsfrcindex+1))
    end--if
    if ((numtecken0d==92) and (numtecken1d==numaskikodo)) then -- "\@" "\~"
      numsfrcindex = numsfrcindex + 1 -- INC more, now totally + 2
      varpfiller = 0 -- preASSume nothing available
      strufiller = '??' -- preASSume nothing available
      if (type(varsupstitu)=='string') then
        varpfiller = varsupstitu -- take it as-is (length check below)
      end--if
      if (type(varsupstitu)=='table') then
        varpfiller = varsupstitu [numinsrtinde] -- risk of type "nil"
        numinsrtinde = numinsrtinde + 1 -- INC tab index on every placeholder
      end--if
      if (lfgstringrange(varpfiller,1,60)) then -- !!!FIXME!!! nowiki and other sanitization
        strufiller = varpfiller -- now the substitute is finally accepted
      end--if
    else
      strufiller = string.char (numtecken0d) -- no placeholder -> copy octet
    end--if
    strafterfill = strafterfill .. strufiller -- add one of 4 possible cases
  end--while

  return strafterfill

end--function lfifillinx

------------------------------------------------------------------------

-- Local function LFIKODEOSG

-- Transcode eo X-surrogates to cxapeloj in a single string (eo only).

-- Input  : * streosurr -- ANSI string (empty is useless but cannot
--                                      cause major harm)

-- Output : * strutf8eo -- UTF8 string

-- Depends on functions :
-- [E] mathdiv mathmod

-- Depends on constants :
-- * table "contabtransluteo" inherently holy

-- To be called ONLY from "lfhfillsurrstrtab".

-- * the "x" in a surr pair is case insensitive,
--   for example both "kacxo" and "kacXo" give same result
-- * avoid "\", thus for example "ka\cxo" would get converted but the "\" kept
-- * double "x" (both case insensitive) prevents conversion and becomes
--   reduced to single "x", for example "kacxxo" becomes "kacxo"

local function lfikodeosg (streosurr)

  local vareopeek = 0
  local strutf8eo = ''
  local numeoinplen = 0
  local numinpinx = 0 -- ZERO-based source index
  local numknar0k = 0 -- current char
  local numknaf1x = 0 -- next char (ZERO is NOT valid)
  local numknaf2x = 0 -- post next char (ZERO is NOT valid)
  local boonext1x = false
  local boonext2x = false
  local boosudahdone = false

  numeoinplen = string.len(streosurr)

  while true do

    if (numinpinx>=numeoinplen) then
      break
    end--if

    numknar0k = string.byte(streosurr,(numinpinx+1),(numinpinx+1))
    numknaf1x = 0 -- preASSume no char
    numknaf2x = 0 -- preASSume no char
    if ((numinpinx+1)<numeoinplen) then
      numknaf1x = string.byte(streosurr,(numinpinx+2),(numinpinx+2))
    end--if
    if ((numinpinx+2)<numeoinplen) then
      numknaf2x = string.byte(streosurr,(numinpinx+3),(numinpinx+3))
    end--if

    boonext1x = ((numknaf1x==88) or (numknaf1x==120)) -- case insensitive
    boonext2x = ((numknaf2x==88) or (numknaf2x==120)) -- case insensitive
    boosudahdone = false
    if (boonext1x and boonext2x) then -- got "xx"
      strutf8eo = strutf8eo .. string.char(numknar0k,numknaf1x) -- keep one "x" only
      numinpinx = numinpinx + 3 -- eaten 3 written 2
      boosudahdone = true
    end--if
    if (boonext1x and (not boonext2x)) then -- got yes-"x" and no-"x"
      vareopeek = contabtransluteo[numknar0k] -- UINT16 or type "nil"
      if (type(vareopeek)=='number') then
        strutf8eo = strutf8eo .. string.char(mathdiv(vareopeek,256),mathmod(vareopeek,256)) -- add UTF8 char
        numinpinx = numinpinx + 2 -- eaten 2 written 2
        boosudahdone = true
      end--if
    end--if
    if (not boosudahdone) then
      strutf8eo = strutf8eo .. string.char(numknar0k) -- copy char
      numinpinx = numinpinx + 1 -- eaten 1 written 1
    end--if

  end--while

  return strutf8eo

end--function lfikodeosg

------------------------------------------------------------------------

---- HIGH LEVEL FUNCTIONS [H] ----

------------------------------------------------------------------------

-- Local function LFHVALI1STATUS99CODE

-- Depends on functions :
-- [E] mathisintrange

local function lfhvali1status99code (varvalue)
  local boovalid = false -- preASSume guilt
  while true do -- fake loop
    if (varvalue==0) then
      break -- success thus keep false since no valid error code ;-)
    end--if
    if (mathisintrange(varvalue,1,99)) then
      boovalid = true -- got an error and valid error code returned
    else
      varvalue = 255 -- failed to return valid status code
    end--if
    break -- finally to join mark
  end--while -- fake loop -- join mark
  return varvalue, boovalid
end--function lfhvali1status99code

------------------------------------------------------------------------

-- Local function LFHCONSTRUCTERAR

-- Construct error message maybe peeking description.

-- Input  : * numerar3code -- 1 ... 99 or 2 ... 99 (resistant against invalid
--                            data type, giving "??" on such)
--          * boopeek3it -- do peek description #E02...#E99 from table

-- Depends on functions :
-- [N] lfnumto2digit
-- [E] mathisintrange mathdiv mathmod

-- Depends on constants :
-- * maybe table contaberaroj TWO-based (holes permitted)

-- To be called ONLY from lfhbrewerror, lfhbrewerrsm,
-- lfhbrewerrsvr, lfhbrewerrinsi.

local function lfhconstructerar (numerar3code, boopeek3it)
  local vardes3krip = 0
  local numbottom3limit = 1
  local stryt3sux = '#E'
  if (boopeek3it) then
    numbottom3limit = 2 -- #E01 is a valid code for submodule only
  end--if
  if (mathisintrange(numerar3code,numbottom3limit,99)) then
    stryt3sux = stryt3sux .. lfnumto2digit(numerar3code)
    if (boopeek3it) then
      vardes3krip = contaberaroj[numerar3code] -- risk of type "nil"
      if (type(vardes3krip)=='string') then
        stryt3sux = stryt3sux .. ' ' .. vardes3krip
      else
        stryt3sux = stryt3sux .. ' ??' -- no text found
      end--if
    end--if (boopeek3it) then
  else
    stryt3sux = stryt3sux .. '??' -- no valid error code
  end--if
  return stryt3sux

end--function lfhconstructerar

------------------------------------------------------------------------

-- Local function LFHBREWERRSM

-- Input  : * numerar7code -- TWO-based error code 2 ... 99 (resistant
--                            against invalid data type, giving "??" on such)
--          * numsubkodo   -- 1 ... 99 (resistant against invalid data
--                            type, send type "nil" if no submodule involved)

-- Depends on functions :
-- [H] lfhconstructerar
-- [N] lfnumto2digit
-- [E] mathisintrange mathdiv mathmod

-- Depends on constants :
-- * 3 strings constrelabg constrelaen constrlaxhu
-- * table contaberaroj TWO-based (holes permitted)

-- We DO NOT eat the name of the guilty submodule since it is included
-- in the error code #E03 #E05 and its message.

local function lfhbrewerrsm (numerar7code, numsubkodo)
  local stryt7sux = ''
  local strfromsubo = ''
  stryt7sux = constrlaxhu .. constrelabg .. lfhconstructerar (numerar7code,true) .. constrelaen .. constrlaxhu
  if (mathisintrange(numsubkodo,1,99)) then
    strfromsubo = 'Submodule reports ' .. lfhconstructerar (numsubkodo,false)
    stryt7sux = stryt7sux .. '<br>' .. constrlaxhu .. constrelabg .. strfromsubo .. constrelaen .. constrlaxhu
  end--if
  return stryt7sux
end--function lfhbrewerrsm

------------------------------------------------------------------------

-- Local function LFHFILLSURRSTRTAB

-- Process (fill in, transcode surr) either a single string, or all string
-- items in a table (even nested) using any type of keys/indexes (such as
-- a holy number sequence and non-numeric ones). Items with a non-string
-- value are kept as-is. For filling in own name, and converting eo and
-- NOPE sv surrogates (via 3 separate sub:s).

-- Input  : * varinkommen -- type "string" or "table"
--          * varfyllo -- string, or type "nil" if no filling-in desired
--          * strlingkod -- "eo" or NOPE "sv" to convert surrogates, anything
--                          else (preferably type "nil") to skip this

-- Depends on functions :
-- [I] lfifillinx
-- [I] lfikodeosg (only if converting of eo X-surrogates desired)
-- [I] NOPE lfikodsvsg
-- [E] mathdiv mathmod (via "lfikodeosg" and NOPE "lfikodsvsg")

-- Depends on constants :
-- * table "contabtransluteo" inherently holy (via "lfikodeosg")
-- * NOPE table "contabtranslutsv"

local function lfhfillsurrstrtab (varinkommen, varfyllo, strlingkod)

  local varkey = 0 -- variable without type
  local varele = 0 -- variable without type
  local varutmatning = 0
  local boodone = false

  if (type(varinkommen)=='string') then
    if (type(varfyllo)=='string') then
      varinkommen = lfifillinx (varinkommen,64,varfyllo)
    end--if
    if (strlingkod=='eo') then
      varinkommen = lfikodeosg (varinkommen) -- surr
    end--if
    varutmatning = varinkommen
    boodone = true
  end--if

  if (type(varinkommen)=='table') then
    varutmatning = {} -- brew new table
    varkey = next (varinkommen) -- try to pick 0:th (in no order) key/index
    while true do
      if (varkey==nil) then
        break -- empty table or end reached
      end--if
      varele = varinkommen[varkey] -- pick element of unknown type
      if ((type(varele)=='string') or (type(varele)=='table')) then
        varele = lfhfillsurrstrtab (varele, varfyllo, strlingkod) -- RECURSION
      end--if
      varutmatning[varkey] = varele -- write at same place in dest table
      varkey = next (varinkommen, varkey) -- try to pick next key/index
    end--while
    boodone = true
  end--if

  if (not boodone) then
    varutmatning = varinkommen -- copy as-is whatever it is
  end--if

  return varutmatning

end--function lfhfillsurrstrtab

------------------------------------------------------------------------

-- Local function LFHSEIZETEXT

-- seize text from "contabmosc", return "??" if index (string) unknown

local function lfhseizetext (straccesskey)
  local vartext = 0
  local strgoodtext = ''
  vartext = contabmosc[straccesskey]
  if (type(vartext)=='string') then
    strgoodtext = vartext
  else
    strgoodtext = '??'
  end--if
  return strgoodtext
end--function lfhseizetext

------------------------------------------------------------------------

---- VARIABLES [R] ----

------------------------------------------------------------------------

function exporttable.ek (arxframent)

  -- general unknown type

  local vartymp = 0      -- variable without type multipurpose

  -- special type "args" AKA "arx"

  local arxsomons = 0    -- metaized "args" from our own or caller's "frame"

  -- general tab

  local tablg76yleft = {}
  local tabrooteo    = {} -- from submodule

  -- general str

  local strpikparent = ''
  local strpagenam   = '' -- "{{PAGENAME}}" o "pagenameoverridetestonly"
  local strmorfwrd  = "" -- morpheme or word extracted from pagename
  local strmorfrnd  = "" -- morpheme or word but possible leading dash removed
  local strruangna  = "" -- "{{NAMESPACENUMBER}}" o "nsnumberoverridetestonly"
  local strkodbah   = ""
  local strnambah   = "" -- language name (short without prefix "Bahasa")
  local strdetaleo  = "" -- from submodule, -eo- only so far
  local strkatojeo  = "" -- from submodule, -eo- only so far

  local strmorphdesk = "" -- description of morpheme (empty if word instead)
  local strmorfio    = "" -- with quotes and maybe link
  local strlingvoo   = "" -- big speech and cannot be empty
  local strskripto   = "" -- big speech but can also be empty

  local strtump      = "" -- temp

  local strviserr    = "" -- visible error
  local strvisgud    = "" -- visible good output
  local strinvkat    = "" -- invisible category part
  local strret       = "" -- final result string

  -- general num

  local numerr       = 0 -- 1 inter
  local num2statcode = 0
  local numowntyp    = 0 -- 0 invalid | 1 vorto 2 frazo 3 vortgrupo
  local nummortyp    = 0 -- 0 invalid | 67 C 73 I 77 M 78 N 80 P 85 U
  local numskript    = 0 -- script code, all uppercase ASCII accepted

  -- general boo

  local bootimp   = false
  local booiskay  = false -- from "K"
  local booknown  = false -- from submodule

------------------------------------------------------------------------

---- MAIN [Z] ----

------------------------------------------------------------------------

  ---- GUARD AGAINST INTERNAL ERROR AGAIN ----

  -- later reporting of #E01 may NOT depend on uncommentable strings

  lfdtracemsg ('This is "mfarado", requested "detrc" report')

  if (qbooguard) then
    numerr = 1 -- #E01 internal
  end--if

  ---- PEEK STUFF ----

  strpikparent = constrkoll -- !!!FIXME!!!

  ---- PROCESS MESSAGES, FILL IN ALWAYS, SURR ONLY IF NEEDED ----

  -- placeholder "\@" "\\@" is replaced by augmented name of the caller
  -- from "strpikparent" in any case, for example 'sxablono "test"' or
  -- 'templat "test"'

  -- only for some languages the surr-transcoding is subsequently performed

  if (numerr==0) then
    contaberaroj   = lfhfillsurrstrtab (contaberaroj, strpikparent, constrpriv)
    contabboasting = lfhfillsurrstrtab (contabboasting, nil, constrpriv) -- no filling
    contabmosc     = lfhfillsurrstrtab (contabmosc, nil, constrpriv) -- no filling
  end--if

  ---- PICK ONE SUBTABLE T76 ----

  -- here risk of #E02 #E03

  -- on error we assign "numerr" and maybe "num2statcode" both used far below

  while true do -- fake loop

    if (numerr~=0) then -- #E01 possible
      break -- to join mark
    end--if

    num2statcode, bootimp = lfhvali1status99code (qldingvoj[2])
    if (num2statcode~=0) then
      if (bootimp) then
        numerr = 3 -- #E03 nombrigita
      else
        numerr = 2 -- #E02 malica
      end--if
      break -- to join mark
    end--if

    vartymp = qldingvoj['T76'] -- from "loaddata-tbllingvoj"
    if (type(vartymp)~='table') then -- important check
      numerr = 2 -- #E02 malica
      break -- to join mark
    end--if
    tablg76yleft = vartymp -- y-index -> leftmost column (langcode to langname)

    break -- finally to join mark
  end--while -- fake loop -- join mark

  lfdshowvar (numerr,'numerr','@Picked subtable T76')
  lfdshowvar (num2statcode,'num2statcode')

  ---- GET THE ARX (ONE OF TWO) ----

  -- must be seized independently on "numerr" even if we already suck

  -- give a f**k in possible params other than "caller=true"

  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

  ---- PROCESS 3 HIDDEN NAMED PARAMS ----

  -- this may override "mw.title.getCurrentTitle().text" and
  -- stipulate content in "strpagenam", empty is NOT valid

  -- bad "pagenameoverridetestonly=" can give #E01
  -- no error is possible from other hidden parameters

  -- "detrc=" must be seized independently on "numerr"
  -- even if we already suck, but type "table" must be ensured above !!!

  strpagenam = '' -- using vartymp here
  if (numerr==0) then -- get pagename (error if bad, silent if absent)
    vartymp = arxsomons['pagenameoverridetestonly']
    if (type(vartymp)=='string') then
      if (lfgstringrange(vartymp,1,200)) then -- empty or too long NOT legal
        strpagenam = vartymp
      else
        numerr = 1 -- #E01 internal
      end--if
    end--if
  end--if

  strruangna = '' -- using vartymp here
  if (numerr==0) then -- get namespace (silent if bad, silent if absent)
    vartymp = arxsomons['nsnumberoverridetestonly']
    if (lfgstringrange(vartymp,1,4)) then -- empty or too long NOT legal
      strruangna = vartymp
    end--if
  end--if

  if (arxsomons['detrc']=='true') then
    lfdtracemsg ('Param "detrc=true" seized')
  else
    qboodetrc = false -- was preassigned to "true"
    qstrtrace = '' -- shut up now
  end--if

  lfdshowvar (numerr,'numerr','@Done with hidden params')

  ---- SEIZE PAGENAME AND NS FROM MW ----

  -- "pagenameoverridetestonly=" "nsnumberoverridetestonly=" processed above

  -- later reporting of #E01 must NOT depend on uncommentable
  -- or peekable stuff

  -- pagename must be 1...200 octet:s keep consistent
  -- with "pagenameoverridetestonly="

  if ((numerr==0) and (strpagenam=='')) then -- get pagename (error if bad)
    vartymp = mw.title.getCurrentTitle().text -- without namespace prefix
    if (lfgstringrange(vartymp,1,200)) then -- empty or too long NOT legal
      strpagenam = vartymp -- cannot be left empty
    else
      numerr = 1 -- #E01 internal
    end--if
  end--if

  if ((numerr==0) and (strruangna=='')) then -- get namespace (silent if bad)
    vartymp = mw.title.getCurrentTitle().namespace -- type is "number"
    if (mathisintrange(vartymp,0,9999)) then -- negative NOT legal but silent
      strruangna = tostring(vartymp) -- can be left empty, check below required
    end--if
  end--if

  lfdshowvar (numerr,'numerr','@Pagename and namespace seized')
  lfdshowvar (strpagenam,'strpagenam')
  lfdshowvar (strruangna,'strruangna')

  ---- CHECK NS ----

  if ((numerr==0) and (strruangna~='14')) then -- must be "Category:"
    numerr = 10 -- #E10 called from wrong ns
  end--if

  ---- WHINE IF YOU MUST #E01 ----

  -- reporting of this error #E01 may NOT depend on
  -- uncommentable strings as "strpikparent" and "contaberaroj"

  if (numerr==1) then
    strtump = '#E01 Internal error in module "mfarado".'
    strviserr = constrlaxhu .. constrelabg .. strtump .. constrelaen .. constrlaxhu
  end--if

  ---- PROCESS ANON PARAMS ----

  if ((numerr==0) and (arxsomons[2]~=nil)) then
    numerr = 8 -- #E08 too many
  end--if
  if (numerr==0) then
    vartymp = arxsomons[1]
    booiskay = (vartymp=='K')
    if ((not booiskay) and (vartymp~=nil)) then
      numerr = 16 -- #E16 only "K" is legal
    end--if
  end--if

  lfdshowvar (numerr,'numerr','@Processed anon params')
  lfdshowvar (booiskay,'booiskay')

  ---- DISSECT THE PAGENAME ----

  -- There are 3 possible types:
  -- * "Vorto -en- enhavanta morfemon U (-ist)" 1
  -- * "Frazo -en- enhavanta vorton (nope)" 2
  -- * "Vortgrupo -en- enhavanta (nope)" 3
  -- There can follow 2 more char:s namely space and script code.

  -- All cat:s will later receive the same sorting hint ... if the morpheme
  -- or word begins with a dash then remove it so that "-o" falls under "O"
  -- instead of under "-" ... still keep possible irrelevant trailing dash.

  nummortyp = 0
  numskript = 0

  do -- scope
    local numlong   = 0 -- length of pagename
    local numposnam = 0 -- pos in pagename
    local numoct    = 0
    local numodt    = 0
    local numlenko  = 0 -- 0...10 only but ZERO and ONE invalid soon later
    local numtymp   = 0

    while true do -- outer fake loop

      if (numerr~=0) then
        break
      end--if

    numlong = string.len (strpagenam)
    numowntyp = 0 -- 0 invalid | 1 vorto 2 frazo 3 vortgrupo
    if (lfgcmpsub(strpagenam,"Vorto -",0)) then
      numowntyp = 1
      numposnam = 7
    end--if
    if (lfgcmpsub(strpagenam,"Frazo -",0)) then
      numowntyp = 2
      numposnam = 7
    end--if
    if (lfgcmpsub(strpagenam,"Vortgrupo -",0)) then
      numowntyp = 3
      numposnam = 11
    end--if
    if (numowntyp==0) then
      numerr = 14 -- #E14 none of 3 supported types
      break
    end--if

    lfdshowvar (numowntyp,'numowntyp','after first ONE-of-THREE validation step')

    strkodbah = '' -- read char by char until "- " DASHSPACE reached or error
    numlenko = 0 -- valid values are 2...10 but later further checks follow
    while true do -- inner genuine loop
      if ((numposnam+1)>=numlong) then -- "numposnam" is ZERO-based
        numerr = 15 -- #E15 truncated incoming string
        break
      end--if
      numposnam = numposnam + 1 -- INC-before-pick compensates for ONE-based
      numoct = string.byte (strpagenam,(numposnam  ),(numposnam  ))
      numodt = string.byte (strpagenam,(numposnam+1),(numposnam+1))
      if ((numoct==45) and (numodt==32)) then
        numposnam = numposnam + 1 -- jump over the space too
        break -- found "- "
      end--if
      if (numlenko>=10) then
        numerr = 15 -- #E15 too long monstercode "zh-min-nan-max" :-D
        break
      end--if
      strkodbah = strkodbah .. string.char(numoct)
      numlenko = numlenko + 1
    end--while -- inner genuine loop
    if (numerr~=0) then
      break -- do not forget to exit outer loop too
    end--if

    lfdtracemsg ('Lang code (extraction up to 10, not yet validated) is "' .. strkodbah .. '"')

    if (not lfivalidatelnkoadv(strkodbah,false,false,conbookodlng,conboomiddig,false)) then
      numerr = 11 -- #E11 bad lang code
      break
    end--if

    lfdtracemsg ('The lang code is valid')

    numtymp = 0
    if (numowntyp==1) then
      if (lfgcmpsub(strpagenam,"enhavanta morfemon ",numposnam)) then
        numtymp = 19
      end--if
    end--if
    if (numowntyp==2) then
      if (lfgcmpsub(strpagenam,"enhavanta vorton ",numposnam)) then
        numtymp = 17
      end--if
    end--if
    if (numowntyp==3) then
      if (lfgcmpsub(strpagenam,"enhavanta ",numposnam)) then
        numtymp = 10
      end--if
    end--if
    if (numtymp==0) then
      numerr = 15 -- #E15 not found
      break
    end--if
    numposnam = numposnam + numtymp -- jump over it

    lfdtracemsg ('After second ONE-of-THREE validation step')

    if (numowntyp==1) then
      if ((numposnam+2)>numlong) then -- "numposnam" is ZERO-based
        numerr = 15 -- #E15 truncated
        break
      end--if
      nummortyp = string.byte (strpagenam,(numposnam+1),(numposnam+1))
      numodt = string.byte (strpagenam,(numposnam+2),(numposnam+2))
      if ((nummortyp~=67) and (nummortyp~=73) and (nummortyp~=77) and (nummortyp~=78) and (nummortyp~=80) and (nummortyp~=85)) then
        numerr = 15 -- #E15 invalid code
        break
      end--if
      if (numodt~=32) then
        numerr = 15 -- #E15 no space char
        break
      end--if
      numposnam = numposnam + 2 -- jump over uppercase code and space

      lfdtracemsg ('Seized "mortyp", it is ' .. tostring(nummortyp))

    end--if

    if ((numposnam+3)>numlong) then -- "numposnam" is ZERO-based
      numerr = 15 -- #E15 truncated (two brackets plus min length one -> 3)
      break
    end--if
    numoct = string.byte (strpagenam,(numlong-1),(numlong-1))
    numodt = string.byte (strpagenam, numlong,    numlong   )
    if ((numoct==32) and lfgtestuc(numodt)) then
      numskript = numodt
      numlong = numlong - 2 -- trim it off

      lfdtracemsg ('Seized script code, it is ' .. tostring(numskript))

    end--if

    numoct = string.byte (strpagenam,(numposnam+1),(numposnam+1))
    numodt = string.byte (strpagenam,numlong,numlong)
    if ((numoct~=40) or (numodt~=41)) then
      numerr = 15 -- #E15 need "(" and ")"
      break
    end--if
    strmorfwrd = string.sub (strpagenam,(numposnam+2),(numlong-1)) -- extract

    numoct = string.byte (strmorfwrd,1,1)
    if (numoct==45) then
      strmorfrnd = string.sub (strmorfwrd,2,-1) -- remove Boulder Dash
    else
      strmorfrnd = strmorfwrd -- copy
    end--if

      lfdshowvar (strmorfwrd,'strmorfwrd','extracted morpheme or word')
      lfdshowvar (strmorfrnd,'strmorfrnd','without dash')

      break -- finally to join mark
    end--while -- outer fake loop -- join mark

  end--do scope

  lfdshowvar (numerr,'numerr','after dissection')

  ---- PEEK LANG NAME ----

  -- x-index is /c0/ for lang name in site language but no need for it in T76

  if (numerr==0) then
    strnambah = tablg76yleft[strkodbah]
    booknown = (type(strnambah)=='string') and (strnambah~='-') and (strnambah~='=') -- "false" is criminal
  end--if

  lfdshowvar (numerr,'numerr','after peeking lang name')
  lfdshowvar (strnambah,'strnambah')

  ---- PEEK DETAILS VIA OTHER SUBMODULE IF ONLY 4 + 1 CONDITIONS ARE MET ----

  -- submodule "rooteo" requires two named
  -- parameters "in=" and "givetable=true"

  -- returned table has elements 0...4 :
  -- 0 error code (ZERO OK)
  -- 1 strvisgud
  -- 2 strinvkat
  -- 3 strvisred -- we use this reduced text
  -- 4 qstrtrace

  -- Examples of full and reduced text:
  -- * <<La elemento "mov" (tipo N) troveblas en BRO5 kaj OA6.>>
  --                              <<troveblas en BRO5 kaj OA6>>
  -- * <<La elemento "-unt-" (tipo
  --   I) ne troveblas en iu listo kaj do estas neoficiala.>>
  --    <<ne troveblas en iu listo kaj do estas neoficiala>>

  strdetaleo = '' -- later concat:ed if non-empty
  strkatojeo = '' -- later concat:ed in any case, but can be empty

  if ((numerr==0) and (numowntyp==1) and (strkodbah=='eo') and conboouseeox) then

    if (booiskay) then

      strkatojeo = lfikataldigu(constrkatq,contabboasting[10],strmorfrnd) -- got bad "K" but remaining 4 are met

    else

      strtump = '' -- preASSume
      if (qboodetrc) then
        strtump = 'true' -- also from submodule
      end--if
      tabrooteo = qeorooteo.ek { args = { ['in']=strpagenam , ['givetable']='true' , ['detrc']=strtump } }
      if (type(tabrooteo)=='table') then
        num2statcode = tabrooteo[0]
        if (num2statcode==0) then
          strdetaleo = tabrooteo[3] -- reduced
          strkatojeo = tabrooteo[2]
        else
          numerr = 5 -- #E05 nombrigita !!!FIXME!!! use LFHVALI1STATUS99CODE
        end--if
        if (qboodetrc) then
          lfdimportreport (tabrooteo[4]) -- import it
        end--if
      else
        numerr = 12 -- #E12 -> #E04 "ne redonis tabelon"
      end--if

    end--if (booiskay) else

  end--if ((numerr==0) and (numowntyp==1) and ...

  lfdshowvar (numerr,'numerr','submodule "rooteo" maybe called')
  lfdshowvar (strdetaleo,'strdetaleo')
  lfdshowvar (strkatojeo,'strkatojeo')

  ---- WHINE IF YOU MUST #E02...#E99 WITH TWO SUBMODULES ----

  -- reporting of errors #E02...#E99 depends on uncommentable or peekable
  -- stuff and on name of the caller filled in from "strpikparent"

  while true do -- fake loop
    if ((numerr==0) or (numerr==1)) then
      break -- success or #E01
    end--if
    if ((numerr==3) or (numerr==5)) then -- #E03 #E05
      strviserr = lfhbrewerrsm(numerr,num2statcode) -- 2 lines
      break
    end--if
    strviserr = lfhbrewerrsm(numerr,nil) -- no code from submo, one line only
    break -- finally to join mark
  end--while -- fake loop -- join mark

  ---- PREBREW DESCRIPTION OF MORPHEME ----

  -- "nummortyp" -- 0 invalid | 67 C 73 I 77 M 78 N 80 P 85 U

  -- incoming from above "strdetaleo" (empty if not applicable)

  -- note that "booiskay" from "K" and "strdetaleo" exclude
  -- each other but they can even be both absent

  -- after this block the text fragment in "strmorphdesk" will either
  -- * begin with a space and end with either a word or a
  --   closing arc bracket ")" (NOT a space in any case)
  -- or
  -- * be empty

  strmorphdesk = ''
  if ((numerr==0) and (numowntyp==1)) then

    do -- scope
    local strpriskribo = '' -- 1 of 6 values from "nummortyp"
    local strmortypletter = ''

    strmortypletter = string.char (nummortyp)
    strpriskribo = lfhseizetext (strmortypletter) -- letter to word
    if (booiskay) then
      strpriskribo = strpriskribo .. ' kunmetitan' -- "contabmosc" also accusa
    end--if
    if (strdetaleo~='') then
      strpriskribo = strpriskribo .. ', ' .. strdetaleo
    end--if
    strmorphdesk = ' ' .. strmortypletter .. ' (' .. strpriskribo .. ')'

    end--do scope

  end--if

  ---- PREBREW STUFF AND LINK TO APPENDIX PAGE AND SCRIPT DETAILS ----

  -- "strmorfwrd" contains the raw morpheme or word but dashes can persist
  -- "strmorfrnd" is without leading dash

  -- string "constrapxq" is appendix prefix and excludes the colon ":"

  -- this outputs 3 fragments:
  -- * trivial "strmorfio" no spaces
  -- * intricate "strlingvoo" begins with space ends with arc bracket ")"
  -- * medium "strskripto" begins with space ends with arc bracket ")"

  if (numerr==0) then

    do -- scope
      local straldono = '' -- link or not and cannot be empty
      local strlngandskr = '' -- "jaH"

      if (nummortyp==78) then -- "N"
        strmorfio = '"' .. strmorfwrd .. '"' -- do NOT link nonstandalone roots
      else
        strmorfio = '"[[' .. strmorfwrd .. ']]"'
      end--if
      if (booknown) then
        straldono = 'la lingvo ' .. lfikataldigu(constrapxq,(lfucaserest(strnambah,true,false,'eo')),strnambah)
      else
        straldono = 'nekonata lingvo'
      end--if
      strlingvoo = ' en '.. straldono .. ' (lingvokodo -' .. strkodbah .. '-)'
      if (booknown and (numskript~=0)) then
        strlngandskr = strkodbah .. string.char (numskript) -- gives fe "jaH"
        strskripto = ' per la skribo ' .. string.char (numskript) .. ' (' .. lfhseizetext (strlngandskr) .. ')'
      else
        strskripto = ''
      end--if

    end--do scope

  end--if

  ---- BREW THE VISIBLE BOX PART ----

  -- "numowntyp" -- 0 invalid | 1 vorto 2 frazo 3 vortgrupo

  -- text in "contabboasting" ends with a word (not space)

  if (numerr==0) then
    strvisgud = contabhtml[0] .. contabhtml[1] .. contabboasting [numowntyp] .. strmorphdesk .. ' ' .. strmorfio .. strlingvoo .. strskripto .. '.' .. contabhtml['lowend']
  end--if

  ---- BREW THE INVISIBLE CATEGORY PART ----

  -- incoming from above "strkatojeo" (empty if not applicable)

  -- "strmorfwrd" contains the raw morpheme or word but dashes can persist
  -- "strmorfrnd" is without leading dash

  -- string "constrkatq" is cat prefix and excludes the colon ":"

  -- brew "__HIDDENCAT__" as well as 2 cat insertions (not if lang code
  -- is unknown) and possibly further ones from submodule or "K" (if -eo-)

  -- the cat:s are for example "Vortfarado|"... and "Vortfarado (angla)|"...

  if (numerr==0) then
    strinvkat = '__HIDDENCAT__' .. strkatojeo
    if (booknown) then
      strinvkat = strinvkat .. lfikataldigu(constrkatq,'Vortfarado',strmorfrnd)
      strinvkat = strinvkat .. lfikataldigu(constrkatq,('Vortfarado (' .. strnambah .. ')'),strmorfrnd)
    end--if
  end--if

  ---- RETURN THE JUNK STRING ----

  strret = strviserr .. strvisgud .. strinvkat
  if (qboodetrc) then -- "qstrtrace" declared separately outside main function
    strret = "<br>" .. qstrtrace .. "<br><br>" .. strret
  end--if
  return strret

end--function

  ---- RETURN THE JUNK LUA TABLE ----

return exporttable