ᱣᱤᱠᱤᱯᱤᱰᱤᱭᱟ ᱨᱚᱲ:Lua

 WP:Lua
WPtalk:Lua
 Help
 
 To do
 
 Resources
mw:reference manual
 
{{#if:ClueBot III |

Is it possible in a template to extract data from a module rather than a function? ᱥᱟᱯᱲᱟᱣ

For example, if module was:

local str = "Hello World!"
local p = {["string"] = str};
p.hello = function(frame)
    return str
end

return p

and the template code {{#invoke:moduleName|string}} returned Hello World!. Or do I have to create a function if I want to achieve that? —⁠andrybak (talk) 08:34, 5 July 2019 (UTC)

A module may have a module-wide “global” variables although they are (rather jarringly) declared with local outside functions. The main concern is performance – too much global stuff slows execution down; it is better to resort to require(modulename) in the case where heavy initialized data are desirable. Incnis Mrsi (talk) 09:56, 5 July 2019 (UTC)
The question concerns whether X in {{#invoke:moduleName|X}} has to be a function name, or whether it could be a code (such as "string") to get the code's value (such as "Hello World!"). The answer is yes it can be a code, but it's pretty ugly and involves setting a metatable on p such that X is interpreted at runtime. A lot less hair would be lost if the call were {{#invoke:moduleName|get|X}} where the function get returns the value for X. See Module:Data for how magic metatable code can be written. Johnuniq (talk) 10:43, 5 July 2019 (UTC)

Question from a newbie ᱥᱟᱯᱲᱟᱣ

Hello, I have few connoissances into Lua. I have (one) (or multiple words) into a given string. Example : <CDG LAX JFK> (string without the < and the >) I would need this string transformed into %22CDG%20LAX%20JFK%22 so that this being understandable by a sparql query. I have written a short code but it doesn't come the fashion I need. Any tip would be greatly appreciated. --Bouzinac (talk) 20:16, 23 July 2019 (UTC)

Can the URI library help you here? --Gonnym (talk) 20:18, 23 July 2019 (UTC)
Thanks. I was building this function
local p = {}
s=frame.args[1]
function p.recode(frame) 
    return "%22".. s:gsub(" ","%20").. "%22"
end
return p
Where am I wrong ?
OK, got it with
local p = {}

function p.recode(frame) 
	return "%22" .. mw.uri.encode(frame.args[1],"PATH") ..  "%22"
end
return p
 :) --Bouzinac (talk) 20:43, 23 July 2019 (UTC)
No, it does not work when multiples code, works only when one code. I need a transformation being string other_string other_string be "string" "other_string" "other_string"
and then be urlencoded. Bouzinac (talk) 20:52, 23 July 2019 (UTC)
If I understand you right, maybe something like this would work:
local function transform(s)
	return mw.uri.encode(s:gsub("%S+", '"%1"'), "PATH")
end

function p.transform(frame)
	return transform(frame.args[1])
end
It encloses sequences of non-ASCII-whitespace characters in quotes and then percent-encodes the string. — Eru·tuon 21:13, 23 July 2019 (UTC)
That's exactly what I wished. Many thanks --Bouzinac (talk) 21:18, 23 July 2019 (UTC)

mw.ext.ParserFunctions.expr ᱥᱟᱯᱲᱟᱣ

I'm trying to use the ParserFunctions library in lua for it's trunc() calculation, but Help:Extension:ParserFunctions has no documentation at all how its used other than mw.ext.ParserFunctions.expr. The most I was able to get is mw.ext.ParserFunctions.expr("trunc", value) but that gives me the error "Lua error: Expression error: Missing operand for trunc..". Any ideas? --Gonnym (talk) 07:43, 24 July 2019 (UTC)

Gonnym, i've made a task of the fact that this library is undocumented. phab:T228840TheDJ (talkcontribs) 08:52, 24 July 2019 (UTC)
Thanks! --Gonnym (talk) 18:29, 24 July 2019 (UTC)
Try this (untested).
local success, result = pcall(mw.ext.ParserFunctions.expr, 'trunc 1234.5678')
if success then
    -- result is the result
else
    -- result is error text
end
Johnuniq (talk) 08:16, 24 July 2019 (UTC)
That seems to work although I have no idea why, as mw.ext.ParserFunctions.expr('trunc 1234.5678') doesn't. --Gonnym (talk) 18:29, 24 July 2019 (UTC)
ᱪᱷᱟᱸᱪ:Re are you sure? i didn't create a module to test it, but it did return 1234 in scribuntu debug console (open any module for edit, and type "=mw.ext.ParserFunctions.expr('trunc 1234.5678')" <Enter> in the debug console). peace - קיפודנחש (aka kipod) (talk) 18:53, 24 July 2019 (UTC)
Well, now it does work. No idea what I did differently before. I blame the hour. Also, thanks :)--Gonnym (talk) 19:23, 24 July 2019 (UTC)
oh, and the most intriguing question: why would anyone even want to call "mw.ext.ParserFunctions.expr" if what you want to do is "trunc"? why not ask lua directly?
local input = 1234.5678
local truncated = math.floor(input) -- returns the same as mw.ext.ParserFunctions.expr('trunc ' .. input)
input = '1234.5678' -- works for strings, too:
truncated = math.floor(input) -- still 1234: i guess math.floor is nice enough, and calls tonumber() for you, or something. JS behave similarly
-- math.floor will throw if the parameter is not "numberable", e.g. "bla", '', or nil. you can use pcall, or test directly:
local num = tonumber(input)
if num then -- safe: in lua, 0 evaluates to true
    truncated = math.floor(num)
else
-- do some error handling
end
i can't imagine what do you gain by calling mw.ext.ParserFunctions.expr, if what you want to do is trunc, unless the parameter passed to the template _is_ trunc 1234.5678 or or trunc(1234.5678). this looks a bit bizarre (why would the editor pass something like this as a parameter?), but i guess it's possible. peace - קיפודנחש (aka kipod) (talk) 19:34, 24 July 2019 (UTC)
I'm trying to convert a set of templates to lua, so the first step I was trying to do is just understand the code flow and get a simple version working. That template used the trunc function which was why I wanted that. I didn't spend time debugging all scenarios to see if it did anything else. I completely agree with you though, that using your suggested code would be a much better end-result. --Gonnym (talk) 20:10, 24 July 2019 (UTC)
I have converted a couple of complex templates by writing the logic in pseudocode, being careful to include all the tricky conditions that apply to template wikitext (like what #if really does). After I've got the pseudocode, I convert that to Lua and hope that testcases will detect problems. Some functions in Module:Math might be useful. Johnuniq (talk) 00:13, 25 July 2019 (UTC)

Arithmetic expressions as strings ᱥᱟᱯᱲᱟᱣ

I have an array of arithmetic expressions as strings and I need to do a calculation with an x value for the relevant one, however it can't do that with an arithmetic expression as a string. I'd like to avoid needing to do this as an if/else. Any ideas? Code below:

local function main(value1, value2)
	local arithmeticExpressionList = {
		[1] = " % 2 + 4",
		[4] = " / 2 % 2 + 2",
		[6] = " / 4 % 2 + 5",
		[9] = " / 8 % 2 + 5",
		[12] = " / 16 % 2 + 3",
		[14] = " / 32 % 2 + 8",
		[17] = " / 64 % 2 + 6"
	}

	local arithmeticExpression = arithmeticExpressionList[value1]
	if (arithmeticExpression) then
		value = value2 .. arithmeticExpression
		return value
	end
end

--Gonnym (talk) 06:51, 25 July 2019 (UTC)

A simpler example with the expected result would be helpful (or use this example if you want to show its evaluation). I can't tell if the items should be concatenated and then evaluated or what. And % is remainder? Essentially you have to write your own expression parser (which I have done for a special purpose, but definitely not recommended!), or use mw.ext.ParserFunctions.expr from the previous section. Johnuniq (talk) 07:31, 25 July 2019 (UTC)
For a given value1 of 1 and a value2 of 7: value = value2 .. arithmeticExpression should be the same result as value = 7 % 2 + 4. % is Modulo operation. --Gonnym (talk) 07:36, 25 July 2019 (UTC)
Thanks for the tip about using mw.ext.ParserFunctions.expr, that does work (with a small change from "%" to "mod"). --Gonnym (talk) 07:44, 25 July 2019 (UTC)
Works, but producers different results for some values. mw.ext.ParserFunctions.expr(48.. arithmeticExpressionList[17])) results in 6, while 48 / 64 % 2 + 6 results in 6.75. That said, the template that I'm working from also gives 6, so mw.ext.ParserFunctions.expr is at least consistent with that and produces the same results. --Gonnym (talk) 08:10, 25 July 2019 (UTC)
Lua's % is more like ParserFunctions.expr's fmod, FYI. At least for positive numbers. Anomie 13:28, 25 July 2019 (UTC)
Do the arithmetic expressions actually have to be strings? If not, this should work:
local function main(value1, value2)
	local arithmeticExpressionList = {
		[1] = function ( x ) return x % 2 + 4 end,
		[4] = function ( x ) return x / 2 % 2 + 2 end,
		[6] = function ( x ) return x / 4 % 2 + 5 end,
		[9] = function ( x ) return x / 8 % 2 + 5 end,
		[12] = function ( x ) return x / 16 % 2 + 3 end,
		[14] = function ( x ) return x / 32 % 2 + 8 end,
		[17] = function ( x ) return x / 64 % 2 + 6 end
	}

	local arithmeticExpression = arithmeticExpressionList[value1]
	if (arithmeticExpression) then
		return arithmeticExpression( value2 )
	end
end
Anomie 13:28, 25 July 2019 (UTC)


Lua pattern question ᱥᱟᱯᱲᱟᱣ

Hey, I can't see how to get an exact match. I am using [Cc]hess in a template, looking to get back only the full-word matches of " Chess " and " chess ", but I of course also get "duchess", "duchesses", "Chessie", etc. What pattern code returns only the selected characters when they are surrounded by spaces, and not part of a larger word? Thanks in advance. UnitedStatesian (talk) 12:53, 20 August 2019 (UTC)

try: %f[%a]([Cc]hess)%f[%A]
Trappist the monk (talk) 13:05, 20 August 2019 (UTC)
Thanks TTM, that worked perfectly. You are the MAN! UnitedStatesian (talk) 14:00, 20 August 2019 (UTC)
ᱪᱷᱟᱸᱪ:Yo for my own edification, why is the first %a lower case and the second %A uppercase? Does it matter? UnitedStatesian (talk) 03:49, 23 August 2019 (UTC)
Yes, it matters. Search for "frontier" at mw:Extension:Scribunto/Lua reference manual and see lua-users. %a matches all letters, while %A does the opposite (it matches all non-letters). Johnuniq (talk) 04:05, 23 August 2019 (UTC)
Please excuse the typo fix, Johnuniq, the reference manual is on mediawiki, not meta (I make that mistake myself all the time).
ᱪᱷᱟᱸᱪ:Re The frontier pattern "%f[%a]" matches the transition from non-letters to letters, so "%f[%a]chess" would match "chess" at the beginning of a word (including the beginning of a string). The frontier pattern "%f[%A]" matches the transition from letters to non-letters, so "chess%f[%A]" would match "chess" at the end of a word (including the end of a string). The pattern that Trappist gave you uses both frontier matches to isolate the word alone as a match. HTH --RexxS (talk) 14:01, 23 August 2019 (UTC)
Thanks, both! UnitedStatesian (talk) 14:13, 23 August 2019 (UTC)

InfoboxImage module broken? ᱥᱟᱯᱲᱟᱣ

some of the examples on Module:InfoboxImage aren't showing any image at all, and I don't know if it's just my laptop. I tried having a look at the source but I know absolutely nothing about code so I don't know what's wrong or how to fix it... Snizzbut (talk) 15:48, 30 September 2019 (UTC)

ᱪᱷᱟᱸᱪ:Re I see five examples with no image. Two have no image specified; two specify a placeholder image without the |suppressplaceholder=no flag, and one uses an image that doesn't exist. I think that's correct. Do you see more than five examples without images? Certes (talk) 15:54, 30 September 2019 (UTC)
Return to the project page "Lua".