Modulo:mwhineplendu

El Vikivortaro
Salti al navigilo Salti al serĉilo

Dokumentado por ĉi tiu modulo povas esti kreata ĉe Modulo:mwhineplendu/dokumentado

--[===[

MODULE "MWHINEPLENDU" (whine plendu)

"eo.wiktionary.org/wiki/Modulo:mwhineplendu" <!--2019-Jun-26-->
"id.wiktionary.org/wiki/Modul:mwhineplendu"

Purpose: whines about incorrect usage of a template by producing a
         red message and putting the page into a tracking category

Utilo: plendas pri erara uzo de sxablono per eligo de rugxa
       mesagxo kaj aldono de la pagxo al diagnoza kategorio

Manfaat: mengomel tentang penggunaan salah templat melalui buat pesan
         yang merah dan tambahkan halaman ke kategori pelacak

Syfte: gnaeller kring fel anvaendning av en mall genom att producera ett
       roett meddelande och laegga sidan till en spaarningskategori

Used by templates / Uzata far sxablonoj:
- bildodek gamkan

Required submodules / Bezonataj submoduloj:
- none / neniuj

Incoming: - nothing (seized from caller's frame instead)

Returned: - whining message and maybe category string

Parameters seized from the caller's frame:
          - 1 named optional parameter
            - "nocat" (value "true" to suppress categorization,
              anything else is ignored)

To be called from template namespace only.

The caller's name is peeked via ":getParent():getTitle()".

Note that we construct the link with just "<code>{{[[" ...
"]]}}</code>", NOT with "<code><nowiki>{{</nowiki>[[" ...
"]]<nowiki>}}</nowiki></code>", since string coming from a module
is subject to reduced parsing only, thus wiki links and http links
still do work whereas template calls and "nowiki" do NOT.

We do nothing and return an empty string if any problem occurs, for
example caller's name is nil, empty or does not contain a dot.

]===]

local whineplendu = {}

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

---- CONSTANTS ----

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

  -- surrogate transcoding table (only needed for EO)

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

  -- constant strings (generic & misc)

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

  -- constant strings EO vs ID

  local constrpriv = "eo"                    -- EO (site language)
  -- local constrpriv = "id"                    -- ID (site language)
  local constrkatp = "Kategorio:"            -- EO
  -- local constrkatp = "Kategori:"             -- ID

  local constrwhi3 = 'Erara uzo de sxablono'        -- EO (no dots "." here)
  -- local constrwhi3 = 'Penggunaan salah templat'     -- ID (no dots "." here)

  local constrwhi4 = 'Legu gxian dokumentajxon'     -- EO (no dots "." here)
  -- local constrwhi4 = 'Bacalah dokumentasinya'       -- ID (no dots "." here)

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

---- ORDINARY LOCAL LOW LEVEL FUNCTIONS ----

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

-- Local function LFKODEOSG

-- Transcode surrogates to cxapeloj in a string (EO only)

local function lfkodeosg (strsurr)
  local varpeek = 0
  local strcxapeloj = ''
  local numinputl = 0
  local numininx = 0
  local numknark = 0 -- current char (ZERO is NOT valid)
  local numknarp = 0 -- previous char (ZERO is NOT valid)
  local numlow = 0
  local numhaj = 0
  numinputl = string.len(strsurr)
  while (true) do
    if (numininx==numinputl) then
      break
    end--if
    numknark = string.byte(strsurr,(numininx+1),(numininx+1))
    numininx = numininx + 1
    numhaj = 0 -- pre-assume no translation
    if ((numknarp~=0) and ((numknark==88) or (numknark==120))) then -- got "x"
      varpeek = contabtransiltable[numknarp] -- UINT16 or nil
      if (varpeek~=nil) then
        numlow = varpeek % 256 -- MOD operator -- bitwise AND operator lacks
        numhaj = math.floor (varpeek / 256) -- DIV operator lacks in LUA :-(
      end--if
    end--if
    if (numhaj~=0) then
      strcxapeloj = strcxapeloj .. string.char(numhaj) .. string.char(numlow)
      numknark = 0 -- invalidade current char
    else
      if (numknarp~=0) then -- add previous char only if valid
        strcxapeloj = strcxapeloj .. string.char(numknarp) -- add it
      end--if
    end--if
    numknarp = numknark -- copy to previous even if invalid
  end--while
  if (numknarp~=0) then -- add previous and last char only if valid
    strcxapeloj = strcxapeloj .. string.char(numknarp) -- add it
  end--if
  return strcxapeloj
end--function lfkodeosg

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

---- MAIN EXPORTED FUNCTION ----

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

function whineplendu.ek (arxframent)

  -- general unknown type

  local vartmp = 0     -- variable without type

  -- special type "args" AKA "arx"

  local arxcaller = 0  -- metaized "args" from caller's "frame" (NOT our own)

  -- general "str"

  local strtmp   = ""
  local strtpm   = ""
  local strcalpr = ""  -- pagename namespace prefix including the colon ":"
  local strcalco = ""  -- pagename core
  local strret   = ""  -- output string

  -- general "num"

  local numbull   = 0
  local numposcol = 0  -- ONE-based position of colon

  -- general "boo"

  local booerr    = false  -- fatal error flag (we fail silently)
  local boonocat  = false

  ---- SEIZE 1 OPTIONAL NAMED PARAMETER SUBMITTED TO CALLER ----

  arxcaller = arxframent:getParent().args -- "args" from caller's "frame"

  vartmp = arxcaller["nocat"]
  if (type(vartmp)=="string") then
    if (vartmp=="true") then
      boonocat = true
    end--if
  end--if

  ---- SEIZE CALLER'S NAME INTO 2 STRING:S ----

  -- "strcalpr" at least one char + colon ":"
  -- "strcalco" at least one char

  vartmp = arxframent:getParent():getTitle()

  if (type(vartmp)=="string") then
    strtmp = vartmp
    numbull = string.len (strtmp)
    if (numbull>2) then
      vartmp = string.find (strtmp, ':', 1, true) -- plain text search
      if (vartmp~=nil) then -- "not found" is NOT valid
        numposcol = vartmp -- ONE-based position
        if ((numposcol==1) or (numposcol==numbull)) then
          numposcol = 0 -- invalid
        end--if
      end--if
    end--if (numbull>2) then
  end--if

  if (numposcol==0) then
    booerr = true -- damn
  else
    strcalpr = string.sub (strtmp,1,numposcol) -- includes the colon
    strcalco = string.sub (strtmp,(numposcol+1),numbull)
  end--if

  ---- TRANSCODE EO IF NEEDED ----

  if (constrpriv=="eo") then
    constrwhi3 = lfkodeosg(constrwhi3)
    constrwhi4 = lfkodeosg(constrwhi4)
  end--if

  ---- CARRY OUT THE HARD WORK ----

  strtmp = constrelabg .. constrwhi3 .. constrelaen -- before li
  strtpm = constrelabg .. ". " .. constrwhi4 .. "." .. constrelaen -- after li

  if (booerr==false) then

    strret = constrkros .. strtmp .. " <code>{{[["
    strret = strret .. strcalpr .. strcalco .. "|" .. strcalco
    strret = strret .. "]]}}</code>" .. strtpm .. constrkros

    if (boonocat==false) then
      strret = strret .. "[[" .. constrkatp .. constrwhi3 .. " ("
      strret = strret .. strcalco .. ")|" .. strcalco .. "]]"
    end--if

  end--if

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

  return strret -- can be empty

end--function

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

return whineplendu