Module:Sandbox/User:Sparky Kitty/GE

From the RuneScape Wiki, the wiki for all things RuneScape
Jump to: navigation, search
Module documentation
This documentation is transcluded from Template:Module sandbox/doc. [edit] [purge]
Module:Sandbox/User:Sparky Kitty/GE requires Module:Paramtest.

This module is a sandbox for Sparky Kitty. It can be used to test changes to existing modules, prototype new modules, or just experimenting with lua features.

Invocations of this sandbox should be kept in userspace; if the module is intended for use in other namespaces, it should be moved out of the sandbox into a normal module and template.

This default documentation can be overridden by creating the /doc subpage of this module, as normal.

-- <-- this denotes a comment (two hyphens), from wherever it is to the end of the line
-- we can also do block comments by following the -- by [=[, and close with ]=]

--[=[ eg
this is a block comment
]=]

-- it is good to have a nowiki or a pre at the start of the module to stop nonsense wanted pages
-- <nowiki>


-- FIRST MOST IMPORTANT THING
-- define a master object
-- you can call it whatever, but convention is to call it 'p' and have it be the first line (though it doesn't have to be)
-- we're going to put functions inside the object
-- SECOND MOST IMPORTANT THING IS AT THE END
local p = {}

-- imports - other modules that have already been written with useful functions
-- this takes the 'has_content' function from [[Module:Paramtest]], and saves it here as 'hc'
--   this function is basically the same as the #if parser function
local hc = require('Module:Paramtest').has_content


-- the main function, with the parameter frame
-- again, conventionally called 'main' and 'frame', as this is the main function which takes a Frame object
function p.main(frame)
	--[=[ a bit on frames:
	 using a module is done with #invoke
	 the call to #invoke creates a frame, which is passed to the function specified, and you can access arguments there
	 eg
	     {{#invoke:Sandbox/User:Gaz Lloyd|main|hello|goodbye|name=cheese|number=1337}}    
	 we access the args of this call using `frame.args`
	 which contains an object of form:
	 {
	   1 = 'hello',
	   2 = 'goodbye',
	   name = 'cheese',
	   number = '1337'
	 }
	 
	 (note that everything is a string, so you'll have to use tonumber(x) to convert that string '1337' to a number 1337, if needed
	 
	 
	 however, this does not allow access to the parameters of a template call, eg
	     {{User:Gaz Lloyd/dpl17/t|hi|bye|name=chedder|number=9001}}
	 to get these, we need the parent frame of the frame passed to the function:
	     frame:getParent().args
	 
	 this is a very common construct, so expect to see it a lot and understand what it means
	 
	 there are other things that can be done with frames, but we don't need them at the mo
	]=]

	local args = frame:getParent().args
	
	-- pagename from the DPL
	local page = args['%PAGE%']
	
	-- storage variables
	local link = ''
	local str = ''
	
	-- number of versions
	local i = 1
	
	if hc(args.version1) or hc(args.id1) then
		-- if version1 is defined, we have a switchfobox, so we have to account for that here
		local ids = {}
		while hc(args['version'..i]) or hc(args['id' .. i]) do
			-- what you actually do here may vary
			-- i'm going to collect all the IDs and put them into a table, in order
			
			-- this is a library function that adds the second param to the end of the first param (a table)
			table.insert(ids, {args['id' .. i], args['version'..i]})		    --[=[ eg
Needs fixing
]=]
			
			-- don't forget to increment i
			i = i + 1
		end
		local rows = {}
        for _,v in ipairs(ids) do
            table.insert(rows, string.format('\n|-\n| [[%s|%s <small>%s</small>]]\n| %s', page, page, v[2], v[1]))
        end
        str = table.concat(rows)
		
    else
        -- not a switchfobox, don't need to do anything special
        str = string.format('\n|-\n| [[%s]] \n| %s', page, args.id)
    end
	
	-- quick bit of error checking
	if str == nil then
		str = ''
	end
	
	if link == nil or link == '' then
		link="''no id(s) specified''"
	end
	
	
	
	-- now we construct the table row to be returned
	-- string.format takes the first param (a string) and substitutes %s for each other parameter in order
	-- (often called printf or sprintf in other languages)

	
	--return the table row string
	return str
	
end --remember to end the function!




-- SECOND MOST IMPORTANT THING
-- the object with the functions we defined is returned
-- this should always be the last line - no code that appears after it will be evaluated
return p