Module:Sandbox/Gnosygnu
Appearance
--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
function p.test_726()
-- mw.ustring.gsub("仰", "()[仰]+()", function(pos1, pos2)
mw.ustring.gsub("a", "()a()", function(pos1, pos2)
mw.log(pos1);
mw.log(pos2);
end)
end
function p.test_732()
mw.log('should not print mw.log');
mw.ustring.gsub("a", "(a)(%f[%s])", function(pos1, pos2)
mw.log('match', "`" .. pos1 .. "`", "`" .. pos2 .. "`");
end)
end
function p.test_732_frontier()
return mw.ustring.gsub("a b c", "%f[%W]", "()")
end
function p.test_726_anypos()
mw.ustring.gsub("abcd", "a(bc)d", function(arg1)
mw.log('basic', arg1, arg2);
end)
mw.ustring.gsub("abcd", "a()bcd", function(arg1)
mw.log('empty', arg1, arg2);
end)
end
function p.test_727(id2)
-- local id2 = frame.args[1];
--P1362's format regex: \p{Lu}[\p{L}\d_',\.\-\(\)\*/–]{3,59} (e.g. Abcd)
-- local class = "[%a%d_',%.%-%(%)%*/–]"
local class = "[%a]"
-- local regex = "^%u"..string.rep(class, 3)..string.rep(class.."?", 56).."$"
local regex = "^%u"..string.rep(class, 3)..string.rep(class.."?", 56).."$"
-- dbg(id2);
-- dbg(regex)
if not mw.ustring.match( id2, regex ) then
return "match:n"
end
return 'match:y'
end
function p.test_775(s)
return tonumber(s)
end
--
-- This module implements [[Template:Percentage]]
--
local math_module = require( "Module:Math" )
local precision = math_module._precision
local sortkey = require( "Module:Sortkey" )
local function rnd(num, digits)
-- This function implements {{rnd}}
return math_module._precision_format(tostring(num), digits)
end
local function oom(num)
-- This function implements {{order of magnitude}}
return math_module._order(tostring(num))
end
function _nonscinote(num)
-- This function undoes scientific notation
if mw.ustring.match(num or '', '^%s*(%d)%.(%d+)<span[^<>]*>×</span>10<sup>([%-−]*)(%d)</sup>%s*$') then
local a,b,c,d = mw.ustring.match(num or '', '^%s*(%d)%.(%d+)<span[^<>]*>×</span>10<sup>([%-−]*)(%d)</sup>%s*$')
d = tonumber(d) or 1
if c ~= '' then
return '0.' .. mw.ustring.rep('0', d - 1) .. a .. b
else
return a .. mw.ustring.sub(b .. mw.ustring.rep('0', d ), 1, d)
end
end
return num
end
local function fmtout(num,snote)
if snote then
return _nonscinote(num)
else
return num
end
end
function p._percentage(n1, n2, prec, suffix, pad, sigfig, sn)
local pct = 100*n1/n2
mw.log('a', pct);
skey = '<span data-sort-value="'
.. sortkey._sortKeyForNumber(pct) .. '♠" style="display:none"></span>'
-- prec = math.floor(prec)
if sigfig ~= '' then
if pct ~= 0 then
return skey .. fmtout(rnd(pct, tonumber(sigfig) - oom(pct) - 1), sn) .. suffix
else
return skey .. fmtout(rnd(pct, tonumber(sigfig) - 3), sn) .. suffix
end
end
if pad ~= '' then
return skey .. fmtout(rnd(pct, prec), sn) .. suffix
end
prec = (prec < 0) and 0 or prec
if pct ~= 0 then
pct = ((pct < 0) and -1 or 1)*math.floor(math.abs(pct * 10^prec) + 0.5) / 10^prec
end
return skey .. fmtout(pct, sn) .. suffix
end
return p