Jump to content

Module:Example/sandbox

From Wikipedia, the free encyclopedia
---------------------------------------------------------------------------------- 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.--------------------------------------------------------------------------------localp={}-- 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)localgreeting="Hello World!"-- Define the greeting message as a local variable.returngreeting-- 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}}--------------------------------------------------------------------------------functionp.hello_to(frame)localname=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}}--------------------------------------------------------------------------------functionp.count_fruit(frame)-- Convert the named parameters to numbers; default to 0 if conversion fails.localnum_bananas=tonumber(frame.args.bananas)or0localnum_apples=tonumber(frame.args.apples)or0-- Determine the correct word for singular or plural form.localbanana_label=(num_bananas==1)and"banana"or"bananas"localapple_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.--------------------------------------------------------------------------------localfunctionlucky(a,b)ifb=="yeah"thenreturna.." is my lucky number."elsereturnaendend---------------------------------------------------------------------------------- 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.--------------------------------------------------------------------------------functionp.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.localparentArgs=frame:getParent().argslocalargs=frame.args-- Use the first and second unnamed parameters, with a fallback to parent arguments.localM=args[1]orparentArgs[1]localm=args[2]orparentArgs[2]-- Retrieve the named parameter 'lucky' (if provided).localluckyParam=args.luckyorparentArgs.lucky-- Determine the output based on the provided parameters.ifm==nilthenreturn"Lonely"-- If the second parameter is missing, return "Lonely".elseifM>mthen-- If M is greater than m, calculate the difference and use the lucky helper.returnlucky(M-m,luckyParam)elsereturn"Be positive!"endend---------------------------------------------------------------------------------- Return the module table to make the functions accessible via {{#invoke:}}.--------------------------------------------------------------------------------returnpositive
close