Jump to content

Module:Sandbox/Gnosygnu

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 69.126.182.12 (talk) at 04:04, 22 July 2019. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
--This Module is for Lua experimentation. No other pages refer to it.

local p = {}

function p.siteinfo(frame)
  return mw.site.siteName .. '--' .. '(' .. mw.site.server .. ')'
end

function p.getEntity(frame)
  local v = mw.wikibase.getEntity();
  if v == nil then
  	return 'nil'
  else
  	return v.id
  end
end

--[[
Test with:
* Debug console
** Code:
:: =p.ustring_find({args={[1]='abab', [2]='b', [3]=3, [4]='true'}})
** Returns -> 4 (2nd b, not 1st b)
* https://en.wikipedia.org/wiki/Project:Sandbox
** Code:
:: {{#invoke:Sandbox/Gnosygnu|ustring_find|abab|b|3|true}}
** Returns -> 4 (2nd b, not 1st b)
--]]
function p.ustring_find(frame)
  local args = frame.args;
  local rslt = {mw.ustring.find(args[1], args[2], tonumber(args[3]), args[4] == 'true')};

  local rv = '';
  local rslt_len = #rslt;
  for i=1,rslt_len do
    if i ~= 1 then
      rv = rv .. ';'
    end
    rv = rv .. rslt[i]
  end
  return rv;
end

function p.ustring_gsub(frame)
  local args = frame.args;
  local rslt = {mw.ustring.gsub(args[1], args[2], args[3])};

  local rv = '';
  local rslt_len = #rslt;
  for i=1,rslt_len do
    if i ~= 1 then
      rv = rv .. ';'
    end
    rv = rv .. rslt[i]
  end
  return rv;
end

function p.test18(frame)
  return mw.language.fetchLanguageName(frame.args[1], 'en');	
--  return mw.language.fetchLanguageName(frame.args[1]);
end

--=p.resolvePropertyId({args={[1]='P123'}})
function p.resolvePropertyId(frame)
  return mw.wikibase.resolvePropertyId(frame.args[1]);
end

function p.getParentTitle(frame)
  return frame:getParent():getTitle()	
end
function p.getCurrentTitle(frame)
  return frame:getTitle()
end

function p.wikiencode( s )
	local ret = string.gsub( s, '([^a-zA-Z0-9!$()*,./:;@~_-])', function ( c )
		if c == ' ' then
			return '_'
		else
			return string.len(c) .. ":" .. string.byte( c, 1, 1 ) .. ";"
--			return string.format( '%%%02X', string.byte( c, 1, 1 ) )
		end
	end );
	return ret
end

function p.test19()
	return string.gsub("aæb", '([^a-zA-Z0-9!$()*,./:;@~_-])', "x")
end

return p