Naar inhoud springen

Module:Zandbak

Uit Wikibooks

(MediaWiki:Scribunto-doc-page-show)

Deze pagina dient voor tests en experimenten met Lua-modules.
Nieuwe functies toevoegen kan en mag altijd, maar laat de bestaande functies aub. intact; daar wordt mischien nog mee getest!


local p = {}

function p.coords(frame)
  local test = "35°58'26.999" .. string.char(34) .. "N"
  local input = test  -- frame.args[1]
  local s = ''
  local deg = 0
  local i = 0
  local tmp = 0
  local comma = false

  for i = 1, string.len(input) do
    s = string.sub(input, i, i) 
    -- mw.log(string.byte(s) .. ' ')

    if tonumber(s) ~= nil then 
      tmp = tmp * 10 + s
    end

--  if s == "°" then               -- waarom werkt dit niet???
    if s == string.char(194) then  -- zo dan??
      deg = deg + tmp
      tmp = 0
      comma = false
    end

    if (s == "'") then
      deg = deg + tmp / 60
      tmp = 0
      comma = false
    end

    if (s == '"') then
      deg = deg + tmp / 3600
      tmp = 0
      comma = false
    end

    if s == '.' then
      -- mw.log('!!!')
      comma = true
    end

  end
  return deg
end


function p.links(frame)
  return '[[' .. frame.args[1] ..']] - [[Categorie:' .. frame.args[1] .. ']] - {{' .. frame.args[1] .. '}}'
end


function p.replace(frame)  -- jan 2026
  -- kopie van Module:String2 -> p.replace_all, maar zonder gebruik van p._escape_pattern()
  -- accepteert dus wat Lua verstaat onder "regex"
  -- returns the 1st parameter, with all occurrences of the 2nd parameter replaced with the 3rd parameter
  local r = string.gsub(frame.args[1], frame.args[2], frame.args[3])
  return r
end


function p.decimalToHex(frame)
  -- bron: https://stackoverflow.com/questions/37796287/convert-decimal-to-hex-in-lua-4
  local hexstr = "0123456789ABCDEF"
  local neg = false
  local result = ""
  num = tonumber(frame.args[1])
  if num == 0 then
    return '0'
  end
  if num < 0 then
    neg = true
    num = num * -1
  end
  while num > 0 do
    local n = math.mod(num, 16)
    result = string.sub(hexstr, n + 1, n + 1) .. result
    num = math.floor(num / 16)
  end
  if neg then
    result = '-' .. result
  end
  return result
end


function p.draw(frame)
  local svg = mw.svg.new()
  return svg:setAttribute( 'width',    frame.args['width']   or '' )  -- 320
            :setAttribute( 'height',   frame.args['height']  or '' )  -- 240
            :setAttribute( 'viewBox',  frame.args['viewBox'] or '' )
            :setAttribute( 'style',    frame.args['style']   or '' )
            :setContent(   '\n' ..   ( frame.args['inhoud']  or '\n' ) .. '\n' )       -- spaties/lf invoegen tbv. indent ?
            :setImgAttribute( 'alt',   frame.args['alt']     or 'SVG tekening' )
            :setImgAttribute( 'title', frame.args['title']   or '' )
            :setImgAttribute( 'class', frame.args['class']   or '' )
            :setImgAttribute( 'id',    frame.args['id']      or '' )
            :toImage() -- Gives the image
end

function p.y()
 mw.addWarning("test: bold?")
end

function p.x(frame)
 return mw.site.stats.pagesInNamespace(tonumber(frame.args[1]))
end

function p.test(frame)
 local t = {}
 local retval = ""

 local sX = "0, 100, 200, 300"   -- frame.args[1]
 local sY = "0, 100,  50, 300"   -- frame.args[2]

 sX = string.gsub(sX, "%s", "")
 sY = string.gsub(sY, "%s", "")

 tX = p._split(sX, ",")  -- p._split() returns een array!
 tY = p._split(sY, ",") 

 for i = 2, #(tX), 1 do  -- #(tX) geeft aantal elementen van array tX
  -- {{L|x1=200|y1= 50|x2=400|y2=300}}
  retval = retval .. "{{L" .. "|x1=" .. tX[i-1] .. "|y1=" .. tY[i-1] .. "|x2=" .. tX[i] .. "|y2=" .. tY[i]  .. "}}\n"
 end
 return retval
end

function p._split (inputstr, sep)  
-- NB.: -> local!? hoe?  -> local function _split (inputstr, sep) !!
-- debug: =p._split("aap noot mies")
 if sep == nil then sep = "%s" end
 local t={}
 for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  table.insert(t, str)
 end
 return "1: " .. t[1] .. ", 2: " .. t[2]  -- 2 of meer parameters verwacht; meer mag, maar die doen niets.
end

function p.echo(frame)
 return frame.args[1]
end

function p.link(frame)                  -- {{#Invoke:Zandbak|link|test}} werkt
 return "[[" .. frame.args[1] .. "]]"
end

function p.sjab(frame)                  -- {{#Invoke:Zandbak|sjab|test}} werkt NIET
 return "{{" .. frame.args[1] .. "}}"
end

function p.az_anchors(frame)
 local temp = ""
 for i = 65, 90 do
  temp = temp .. "[[#" .. string.char(i) .. "|" .. string.char(i) .. "]]"
  if i < 90 then temp = temp .. " &middot; " end
 end

 for j = 1, 3 do
  if type(frame.args[j]) == "string" then
   temp = temp .. " &middot; [[#" .. frame.args[j] .. "|" .. frame.args[j] .. "]]"
  end
 end

 return temp
end


function p.az_links(frame)
 -- ...
end

function p._err_msg(txt)
 return '<span style="color: #dd3333; font-size: larger; font-weight: bold;">Error: ' .. txt .. '!</span>'
end

return p
Informatie afkomstig van Wikibooks NL, een onderdeel van de Wikimedia Foundation.