Jump to content

Module:Example/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
better documentation
Tags: Mobile edit Mobile web edit Advanced mobile edit
 
Line 1: Line 1:
--------------------------------------------------------------------------------
local p = {}; --All Lua modules on Wikipedia must begin by defining a variable
-- Module:Example
--that will hold their externally accessible functions.
-- Demonstration of a Lua Module for Wikipedia
--Such variables can have whatever name you want and may
--
--also contain various data as well as functions.
-- This module serves as an example and guide for creating Lua modules on
p.hello = function( frame ) --Add a function to "p".
-- Wikipedia. It defines several functions that can be invoked via the
--Such functions are callable in Wikipedia
-- {{#invoke:}} mechanism. Note that this module is for demonstration purposes
--via the #invoke command.
-- only and is not intended for actual production use.
--"frame" will contain the data that Wikipedia
--------------------------------------------------------------------------------
--sends this function when it runs.

-- 'Hello' is a name of your choice. The same name needs to be referred to when the module is used.
local p = {} -- Table to hold the module's externally accessible functions.

local str = "Hello World!" --Declare a local variable and set it equal to
--------------------------------------------------------------------------------
--"Hello World!".
-- Function: p.hello
-- Description: Returns a simple, fixed greeting "Hello World!".
return str --This tells us to quit this function and send the information in
-- Usage: {{#invoke:Example|hello}}
--"str" back to Wikipedia.
--------------------------------------------------------------------------------
end -- end of the function "hello"
p.hello = function(frame)
local greeting = "Hello World!" -- Define the greeting message as a local variable.
function p.hello_to(frame) -- Add another function
return greeting -- Return the greeting to Wikipedia.
local name = frame.args[1] -- To access arguments passed to a module, use `frame.args`
-- `frame.args[1]` refers to the first unnamed parameter
-- given to the module
return "Hello, " .. name .. "!" -- `..` concatenates strings. This will return a customized
-- greeting depending on the name given, such as "Hello, Fred!"
end
end

function p.count_fruit(frame)
--------------------------------------------------------------------------------
-- Function: p.hello_to
local num_bananas = tonumber(frame.args.bananas) or 0 -- Named arguments ({{#invoke:Example|count_fruit|foo=bar}})
-- Description: Returns a personalized greeting using the first unnamed parameter.
local num_apples = tonumber(frame.args.apples) or 0 -- are likewise accessed by indexing `frame.args` by name (`frame.args["bananas"]`,
-- Usage: {{#invoke:Example|hello_to|YourName}}
-- or equivalently `frame.args.bananas`.
--------------------------------------------------------------------------------
function p.hello_to(frame)
local conj_bananas = num_bananas == 1 and 'banana' or 'bananas'
local name = frame.args[1] -- Retrieve the first unnamed parameter.
local conj_apples = num_apples == 1 and 'apple' or 'apples'
return "Hello, " .. name .. "!" -- Concatenate and return the personalized greeting.
-- Ternary operators assign values based on a condition in a compact way.
-- Here, `conj_bananas` gets `'banana'` if `num_bananas` is 1, else `'bananas'`.
-- Similarly, `conj_apples` gets `'apple'` if `num_apples` is 1, else `'apples'`.
return 'I have ' .. num_bananas .. ' ' .. conj_bananas .. ' and ' .. num_apples .. ' ' .. conj_apples
-- Like above, concatenate a bunch of strings together to produce
-- a sentence based on the arguments given.
end
end


--------------------------------------------------------------------------------
local function lucky(a, b) -- One can define custom functions for use. Here we define a function 'lucky' that has two inputs a and b. The names are of your choice.
-- Function: p.count_fruit
if b == 'yeah' then -- Condition: if b is the string 'yeah'. Strings require quotes. Remember to include 'then'.
-- Description: Constructs and returns a sentence indicating the count of bananas
return a .. ' is my lucky number.' -- Outputs 'a is my lucky number.' if the above condition is met. The string concatenation operator is denoted by 2 dots.
-- and apples, using proper singular/plural forms.
else -- If no conditions are met, i.e. if b is anything else, output specified on the next line. 'else' should not have 'then'.
-- Usage: {{#invoke:Example|count_fruit|bananas=5|apples=6}}
return a -- Simply output a.
--------------------------------------------------------------------------------
end -- The 'if' section should end with 'end'.
function p.count_fruit(frame)
end -- As should 'function'.
-- Convert the named parameters to numbers; default to 0 if conversion fails.
local num_bananas = tonumber(frame.args.bananas) or 0
local num_apples = tonumber(frame.args.apples) or 0


-- Determine the correct word for singular or plural form.
function p.Name2(frame)
local banana_label = (num_bananas == 1) and "banana" or "bananas"
-- The next five lines are mostly for convenience only and can be used as is for your module. The output conditions start on line 50.
local apple_label = (num_apples == 1) and "apple" or "apples"
local pf = frame:getParent().args -- This line allows template parameters to be used in this code easily. The equal sign is used to define variables. 'pf' can be replaced with a word of your choice.

local f = frame.args -- This line allows parameters from {{#invoke:}} to be used easily. 'f' can be replaced with a word of your choice.
-- Construct and return the complete sentence.
local M = f[1] or pf[1] -- f[1] and pf[1], which we just defined, refer to the first parameter. This line shortens them as 'M' for convenience. You could use the original variable names.
return "I have " .. num_bananas .. " " .. banana_label ..
local m = f[2] or pf[2] -- Second shortened as 'm'.
" and " .. num_apples .. " " .. apple_label .. "."
local l = f.lucky or pf.lucky -- A named parameter 'lucky' is shortend as l. Note that the syntax is different from unnamed parameters.
if m == nil then -- If the second parameter is not used.
return 'Lonely' -- Outputs the string 'Lonely' if the first condition is met.
elseif M > m then -- If the first condition is not met, this line tests a second condition: if M is greater than m.
return lucky(M - m, l) -- If the condition is met, the difference is calculated and passed to the self defined function along with l. The output depends on whether l is set to 'yeah'.
else
return 'Be positive!'
end
end
end


--------------------------------------------------------------------------------
function p.eval(frame)
-- Local Helper Function: lucky
return loadstring(frame[1])(frame)
-- Description: Returns a message stating that the given number is "lucky" if the
-- second parameter is the string "yeah"; otherwise, it simply returns
-- the number.
--------------------------------------------------------------------------------
local function lucky(a, b)
if b == "yeah" then
return a .. " is my lucky number."
else
return a
end
end
end


--------------------------------------------------------------------------------
return p --All modules end by returning the variable containing their functions to Wikipedia.
-- Function: p.Name2
-- Now we can use this module by calling {{#invoke: Example | hello }},
-- Description: Demonstrates the use of both unnamed and named parameters from the
-- {{#invoke: Example | hello_to | foo }}, or {{#invoke:Example|count_fruit|bananas=5|apples=6}}
-- frame object. It accesses parameters from the current frame as well as
-- Note that the first part of the invoke is the name of the Module's wikipage,
-- from the parent frame, and returns a message based on the provided values.
-- and the second part is the name of one of the functions attached to the
-- Usage: Can be invoked with parameters directly or via a parent template.
-- variable that you returned.
--------------------------------------------------------------------------------
function p.Name2(frame)
-- Retrieve parameters from both the parent frame and the current frame.
-- The parent frame allows template parameters to be used in this code easily.
local parentArgs = frame:getParent().args
local args = frame.args

-- Use the first and second unnamed parameters, with a fallback to parent arguments.
local M = args[1] or parentArgs[1]
local m = args[2] or parentArgs[2]

-- Retrieve the named parameter 'lucky' (if provided).
local luckyParam = args.lucky or parentArgs.lucky

-- Determine the output based on the provided parameters.
if m == nil then
return "Lonely" -- If the second parameter is missing, return "Lonely".
elseif M > m then
-- If M is greater than m, calculate the difference and use the lucky helper.
return lucky(M - m, luckyParam)
else
return "Be positive!"
end
end


--------------------------------------------------------------------------------
-- The "print" function is not allowed in Wikipedia. All output is accomplished
-- Return the module table to make the functions accessible via {{#invoke:}}.
-- via strings "returned" to Wikipedia.
--------------------------------------------------------------------------------
return positive

Latest revision as of 05:31, 24 February 2025

--------------------------------------------------------------------------------
-- Module:Example
-- Demonstration of a Lua Module for Wikipedia
--
-- This module serves as an example and guide for creating Lua modules on
-- Wikipedia. It defines several functions that can be invoked via the
-- {{#invoke:}} mechanism. Note that this module is for demonstration purposes
-- only and is not intended for actual production use.
--------------------------------------------------------------------------------

local p = {}  -- Table to hold the module's externally accessible functions.

--------------------------------------------------------------------------------
-- Function: p.hello
-- Description: Returns a simple, fixed greeting "Hello World!".
-- Usage: {{#invoke:Example|hello}}
--------------------------------------------------------------------------------
p.hello = function(frame)
    local greeting = "Hello World!"  -- Define the greeting message as a local variable.
    return greeting                  -- Return the greeting to Wikipedia.
end

--------------------------------------------------------------------------------
-- Function: p.hello_to
-- Description: Returns a personalized greeting using the first unnamed parameter.
-- Usage: {{#invoke:Example|hello_to|YourName}}
--------------------------------------------------------------------------------
function p.hello_to(frame)
    local name = frame.args[1]       -- Retrieve the first unnamed parameter.
    return "Hello, " .. name .. "!"   -- Concatenate and return the personalized greeting.
end

--------------------------------------------------------------------------------
-- Function: p.count_fruit
-- Description: Constructs and returns a sentence indicating the count of bananas
--              and apples, using proper singular/plural forms.
-- Usage: {{#invoke:Example|count_fruit|bananas=5|apples=6}}
--------------------------------------------------------------------------------
function p.count_fruit(frame)
    -- Convert the named parameters to numbers; default to 0 if conversion fails.
    local num_bananas = tonumber(frame.args.bananas) or 0
    local num_apples  = tonumber(frame.args.apples) or 0

    -- Determine the correct word for singular or plural form.
    local banana_label = (num_bananas == 1) and "banana" or "bananas"
    local apple_label  = (num_apples == 1) and "apple" or "apples"

    -- Construct and return the complete sentence.
    return "I have " .. num_bananas .. " " .. banana_label ..
           " and " .. num_apples .. " " .. apple_label .. "."
end

--------------------------------------------------------------------------------
-- Local Helper Function: lucky
-- Description: Returns a message stating that the given number is "lucky" if the
--              second parameter is the string "yeah"; otherwise, it simply returns
--              the number.
--------------------------------------------------------------------------------
local function lucky(a, b)
    if b == "yeah" then
        return a .. " is my lucky number."
    else
        return a
    end
end

--------------------------------------------------------------------------------
-- Function: p.Name2
-- Description: Demonstrates the use of both unnamed and named parameters from the
--              frame object. It accesses parameters from the current frame as well as
--              from the parent frame, and returns a message based on the provided values.
-- Usage: Can be invoked with parameters directly or via a parent template.
--------------------------------------------------------------------------------
function p.Name2(frame)
    -- Retrieve parameters from both the parent frame and the current frame.
    -- The parent frame allows template parameters to be used in this code easily.
    local parentArgs = frame:getParent().args
    local args = frame.args

    -- Use the first and second unnamed parameters, with a fallback to parent arguments.
    local M = args[1] or parentArgs[1]
    local m = args[2] or parentArgs[2]

    -- Retrieve the named parameter 'lucky' (if provided).
    local luckyParam = args.lucky or parentArgs.lucky

    -- Determine the output based on the provided parameters.
    if m == nil then
        return "Lonely"  -- If the second parameter is missing, return "Lonely".
    elseif M > m then
        -- If M is greater than m, calculate the difference and use the lucky helper.
        return lucky(M - m, luckyParam)
    else
        return "Be positive!"
    end
end

--------------------------------------------------------------------------------
-- Return the module table to make the functions accessible via {{#invoke:}}.
--------------------------------------------------------------------------------
return positive