Module:S

From Wikipedia

Documentation for this module may be created at Module:S/doc

--[[ 20120601 18:16 fr, Module:S scribunto start functions
Lua scripting in http://scribunto.wmflabs.org/
 
https://www.mediawiki.org/wiki/Extension:Scribunto/Parser_interface_design
function p.funcname(frame)
    local arg1, arg2 = frame.args[1], frame.args[2]
    local name1, name2 = frame.args.name1, frame.args.name2
return arg1, name2
]]--
 
-- require( 'ustring_data' ) -- big bug, forbiden
-- http://en.wikipedia.org/wiki/User:Mxn/serendipity.js
-- if (mw.config.get("wgAction") == "view" && mw.config.get("wgNamespaceNumber") == 0) {
--     mw.loader.using("mediawiki.api", function () {
--         var api = new mw.Api();
-- require( 'mediawiki.api' ) -- for mw.page, mw.time, mw.site ...
-- require( 'mw.time' ) -- for mw.page, mw.time, mw.site ...
--------------------------------------------------------------------
-- References
-- Interface http://scribunto.wmflabs.org/
local p = {}
 
function p.hello()
	-- {{#invoke:S|hello}}
	return 'Hello, world!'
end
 
function p.getAllArgs( frame ) -- reference from Tim Starling
    buf = {}
    for name, value in frame:argumentPairs() do
        if #buf ~= 0 then
            table.insert( buf, ', ' )
        end
        table.insert( buf, name .. '=' .. value )
    end
    return table.concat( buf )
end
 
-- http://www.mediawiki.org/wiki/Extension:Scribunto/API_specification
-- MediaWiki Lua API
-- func{a = "whatever", b = "argument", c = "trolling"}.
function p.wikiState(f)
    local x = f.args[1]
    local i = tonumber(x) -- base 10
    if ( i == nil ) then i = 2 end
    local s = ' i=' .. i .. ' - '
    if ( i == 1 ) or (i==0) then s = s .. ' Time=' .. mw.time.hour .. ':' .. mw.time.min .. ':' .. mw.time.sec end
    if ( i == 2 ) or (i==0) then s = s .. ' Revision=' .. mw.page.getCurrentRevision() end
    if ( i == 3 ) or (i==0) then s = s .. ' SiteName=' .. mw.site.getSiteName() end
    if ( i == 4 ) or (i==0) then s = s .. ' Version=' .. mw.site.getVersion() end
    if ( i == 5 ) or (i==0) then s = s .. ' Namespaces=' .. mw.site.getNamespaces() end
    if ( i == 6 ) or (i==0) then s = s .. ' ContentLanguage=' .. mw.lang.getContentLanguage() end
    if ( i == 7 ) or (i==0) then s = s .. ' UILanguage=' .. mw.lang.getUILanguage() end
    if ( i > 7) or (i==0) then s = s .. ' i > 7.' end
    if ( i > 8) then s = s .. ' i > 8.' end
    --local s = s .. ' Time=' .. mw.time.hour .. ':' .. mw.time.min .. ':' .. mw.time.sec
    --local s = s .. ' Revision=' .. mw.page.getCurrentRevision()
    --local s = s .. ' SiteName=' .. mw.site.getSiteName()
    --local s = s .. ' Version=' .. mw.site.getVersion()
    --local s = s .. ' Namespaces=' .. mw.site.getNamespaces()
    --local s = s .. ' ContentLanguage=' .. mw.lang.getContentLanguage()
    --local s = s .. ' UILanguage=' .. mw.lang.getUILanguage()
    return s
end
 
-- generate a list of 3 lines
function p.listLines(line1, line2, line3)
	-- {{#invoke:S|listLines|line one|line two|line three}}
   if ( line1 == nil ) then
	   line1 = 'line1'
   end
   local s = '* ' .. line1 .. '<br/>'
   if ( line2 ~= nil ) then
	   s = s .. '* ' .. line2 .. '<br/>'
   end
   if ( line3 ~= nil ) then
	   s = s .. '* ' .. line3 .. '<br/>'
   end
   return s
end
 
-- draw a color bar for stat
function p.colorStat(r)
	-- { {#invoke:S|colorStat| 24 | 51 | 103 | 47 | 19 } }
	-- to develop
	local s = '<div width="77" > a b c d </div>'
	return s
end
 
-- search a page in the wiki of elsewhere
function p.searchPage(name)
	-- { {#invoke:S|searchPages| name } }
	local s = "name of page"
	return s
end
 
-- text to call a template
function p.template(name, param)
	-- Call a template by frame:expandTemplate{ title = 'tpl', args = {foo = arg1} }
	local s = frame:expandTemplate{ name, {param} }
	return s
end
 
--------------------------------------------------------------------
-- List tables
function p.tableLister(t)
	-- List any table
	-- { {#invoke:S|tableLister| { 11, firstName='John', 22, lastName='Smith' } } }
	local s = " + "
	local x = { }
	local mt = { __newindex = x, __index = _G }
	local met = setmetatable(t, mt)
	for name,value in pairs(met) do
	    s = s..name..":"..value.." + "
	end
	return s
end
function p.list_table(t)
	-- List numeric table
	-- { {#invoke:S|test_table| { 1, 22, 333 } } }
	local i, s = 1, " - "
	while t[i] do -- for empty table
	    s = s..i..":"..t[i].." - " -- string.format("%s", t[i]) for various formats
	    i = i + 1
	end
	return s
end
function p.test_table()
	-- { {#invoke:S|test_table} }
	-- return p.list_table( { 1, 22, 333 } )
	return p.tableLister( { 11, firstName='John', 22, lastName='Smith' } )
end
 
--------------------------------------------------------------------
-- tag, span, style, color
function p.styleColor(frame) -- <span> text and style
	-- build the color option from the color value
	-- {{#invoke:S|styleColor| #EE8899 }}
	local col = frame.args[1]
	local s = ''
	if ( col == nil ) then col = '' end
	if ( col ~= '' ) then s = 'background-color:' .. col .. ';' end
	return s
end
 
function p.styleWhole(frame)
	-- build the whole style from any list of style options, else none
	-- { {#invoke:S|styleWhole|background-color:#8899EE;} }
	local opt = frame.args[1]
	if ( opt == nil ) then opt = '' end
	if ( opt == '' ) then
	    return ''
	else
	    return ' style="' .. opt .. '" '
	end
end
 
function p.spanColor(frame)
    -- build a span with style color
	-- {{#invoke:S|spanColor| text to color | #EE8899 }}
	local txt, col = frame.args[1], frame.args[2]
	local s = ''
	s = p.styleColor({args = {col}})
	s = p.styleWhole({args = {s}})
	local s = '<span ' .. s .. '>' .. txt .. '</span>'
	return s
end
 
function p.tagStyle(frame)
    -- build a span with style color
	-- {{#invoke:S|spanColor| text to color | #EE8899 }}
	local tag, txt, styles = frame.args[1], frame.args[2], frame.args[3]
    -- if ( txt == nil ) then txt = '' end
    -- if ( styles == nil ) then styles = '' end
	local s = ''
	s = p.styleWhole({args = {styles}})
	local s = '<' .. tag .. ' ' .. styles .. ' >' .. txt .. '</' .. tag .. '>'
	return s
end
 
function p.col3(frame)
	-- replace only 1 text in 3 spans
	-- { {#invoke:S|col3|green text} }
	local t = frame.args[1]
	local s = ''
	if ( t == nil ) then t = '' end
	if ( t ~= '' ) then s = '' end
    s = s .. p.spanColor({args = {'- red -', '#EE8899'}})
    s = s .. p.spanColor({args = {t, '#88EE99'}})
    s = s .. p.spanColor({args = {'- blue -', '#8899EE'}})
	return s
end
 
--------------------------------------------------------------------
-- names about {{FULLPAGENAME}}
function p.tpl2doc(t)
	-- generate /documentation page name from template {{FULLPAGENAME}}
	-- { {#invoke:s|tpl2doc| {{FULLPAGENAME}} } }
	local t = frame.args[1]
	local s = t .. '/documentation'
	return s
end
function p.doc2tpl(t, repl)
	-- generate template page name from /documentation {{FULLPAGENAME}}
	-- { {#invoke:s|doc2tpl| {{FULLPAGENAME}} } }
	local x = repl -- to delete end sub page
	if ( repl == nil ) then x = '/documentation' end
	local s = t -- to delete end sub page
	local t1, t2, tx = string.find(t, x, 1) 
	if ( t1 ~= nil ) then s = string.sub(t, 1, t1-1) end
	return s
end
function p.namespace(t)
	-- to extract NAMESPACE from {{FULLPAGENAME}}
	-- { {#invoke:s|namespace| {{FULLPAGENAME}} } }
	local s = '' -- to extract NAMESPACE
	local t1, t2 = string.find (t, ':', 1) -- search : from begin
	if ( t1 ~= nil ) then s = string.sub(t, 1, t1-1 ) end
	return s
end
 
return p