To help centralise discussions and keep related topics together, all Wikipedia:Lua subpages' talk pages and Help talk:Lua redirect here. |
The contents of the Wikipedia:Lua/Requests page were merged into Wikipedia talk:Lua on 5 February 2016. For the contribution history and old versions of the redirected page, please see its history. |
Wikipedia:Lua/To do was nominated for deletion on 21 May 2015. The result of the discussion was keep. |
This page has archives. Sections older than 90 days may be automatically archived by ClueBot III when more than 1 section is present. |
If x is a string why can I use x:lower()
as a shortcut for string.lower(x)
but if y is a table then I can't use y:concat()
as a shortcut for table.concat(y)
? — Martin (
MSGJ ·
talk) 13:01, 22 March 2024 (UTC)
__index
points to the the string
table in the standard library. Tables, by default, do not have a metatable (presumably as programmers often use custom metatables for tables in their programs). To unpack that explanation, you need to know how metatables work; there is a pretty good explanation in
chapter 13 in the Programming in Lua book. Also there is a little bit more detailed treatment of the string library metatable
here. —
Mr. Stradivarius
♪ talk ♪ 13:51, 22 March 2024 (UTC)
setmetatable(t, y)
you could then use t:concat()
? —
Jts1882 |
talk 14:29, 22 March 2024 (UTC)
t.concat
is a function.t:concat()
is defined as t.concat(t)
.t.concat
has to be a function.t = { 'abc', 'def', 'z' }
y = { __index = { concat = function(x) return string.reverse(table.concat(x)) end } }
setmetatable(t, y)
z = t:concat() -- z = 'zfedcba'
table
works. I recommend using standard table.concat
and living with it. However, it is possible to muck around with something like this for a single table.t = { 'def', 'abc', 'A' }
mt = { __index = { concat = table.concat, sort = table.sort } }
setmetatable(t, mt)
a = t:concat() -- 'defabcA'
t:sort()
b = t:concat() -- 'Aabcdef'
Collection
at
Module:Age to see how I have sometimes defined a simple list. It's very weird to follow but
Module:IPblock has more examples includings :sort()
.
Johnuniq (
talk) 04:24, 26 March 2024 (UTC)ln: is a small wikipedia with less than 10 active users per month and non of them with lua knowledge. There are some lua errors in templates that were probably copy pasted in the past from fr: The pages can be seen here: https://ln.wikipedia.org/?title=Cat%C3%A9gorie:Pages_avec_des_erreurs_de_script&action=edit&redlink=1
Could someone who knows lua have a look to the templates ("modèle") and give us a hand or at least some advice? Thank you for helping. Bombo ( talk) 21:35, 18 April 2024 (UTC)
I'm working on a module that let's the user to use some predefined placeholders to add categories/text. For example, on the "Category:144 births" category they can add the following template
{{YearCategory
|__DECADE__s births
|Births by year
|RD(__CENTURY__)-century births
|__YEAR__ beginnings
|header=In this category you can find people who were born on [[__YEAR__]].
}}
And it will add the 140s births, Births by year, 2nd-century births and 144 beginnings categories to the page and will display a header with "In this category you can find people who were born on 144." content. The code is at hy:Մոդուլ:ԺամանակիԿատեգորիա and an example of usage at hy:Կատեգորիա:1944 ծնունդներ. It work fine except when I want to use those placeholders inside a template or an {{#if: block. As I understand, mediawiki parses the text before sending it to the Lua module. Is there a way to change this? ԱշոտՏՆՂ ( talk) 01:26, 21 April 2024 (UTC)
get
. —
andrybak (
talk) 01:05, 22 April 2024 (UTC){{YearCategory|{{
Decade|2024}}
births..
to implement the decade handling. You may need {{PAGENAME}} or something derived from it as the template parameter.
Certes (
talk) 09:34, 22 April 2024 (UTC)
{{{year}}}
to a decade using {{
DECADE}}
. —
andrybak (
talk) 15:44, 22 April 2024 (UTC)I wrote the following function:
local function linkToWikipage(link, text)
local page = mw.title.new(link)
if page.exists == true then
if page.redirectTarget then
local redir = page.redirectTarget.fullText
or page.redirectTarget.prefixedText
return "[[" .. redir .. "|" .. text .. "]]"
else
return "[[" .. link .. "|" .. text .. "]]"
end
else
return text
end
end
It works on a small scale, but on a larger scale I'm told too many expensive function calls. Can anyone help me find something cheaper that still fulfills the goals of:
Eievie ( talk) 03:35, 23 April 2024 (UTC)
frame:expandTemplate{title = 'Link if exists', args = link}
Greek and Coptic
but not if it said Greek and Coptic (Unicode block)
){{
Link if exists}}
for the latter. This does work (see
testcases here). It creates the links I need and doesn't create a "too many expensive function calls" error.mw.title.new()
is the expensive partpage.exists
and page.redirectTarget
are both cheap{{
Link if exists}}
both do the mw.title.new()
call{{
Link if exists}}
and
Module:Redirect?
Eievie (
talk) 16:14, 23 April 2024 (UTC)
page.exists
is the expensive part? So ordering the function this way would be cheaper?local function linkToWikipage(pageStr, text)
local pageObj = mw.title.new(pageStr)
if pageObj.redirectTarget then
local redir = pageObj.redirectTarget.fullText
or pageObj.redirectTarget.prefixedText
return "[[" .. redir .. "|" .. text .. "]]"
elseif pageObj.exists == true then
return "[[" .. pageStr .. "|" .. text .. "]]"
else
return text
end
end
You are invited to join the discussion at MediaWiki talk:Scribunto-doc-page-header § Category:Module documentation pages. — andrybak ( talk) 09:27, 14 May 2024 (UTC)
To help centralise discussions and keep related topics together, all Wikipedia:Lua subpages' talk pages and Help talk:Lua redirect here. |
The contents of the Wikipedia:Lua/Requests page were merged into Wikipedia talk:Lua on 5 February 2016. For the contribution history and old versions of the redirected page, please see its history. |
Wikipedia:Lua/To do was nominated for deletion on 21 May 2015. The result of the discussion was keep. |
This page has archives. Sections older than 90 days may be automatically archived by ClueBot III when more than 1 section is present. |
If x is a string why can I use x:lower()
as a shortcut for string.lower(x)
but if y is a table then I can't use y:concat()
as a shortcut for table.concat(y)
? — Martin (
MSGJ ·
talk) 13:01, 22 March 2024 (UTC)
__index
points to the the string
table in the standard library. Tables, by default, do not have a metatable (presumably as programmers often use custom metatables for tables in their programs). To unpack that explanation, you need to know how metatables work; there is a pretty good explanation in
chapter 13 in the Programming in Lua book. Also there is a little bit more detailed treatment of the string library metatable
here. —
Mr. Stradivarius
♪ talk ♪ 13:51, 22 March 2024 (UTC)
setmetatable(t, y)
you could then use t:concat()
? —
Jts1882 |
talk 14:29, 22 March 2024 (UTC)
t.concat
is a function.t:concat()
is defined as t.concat(t)
.t.concat
has to be a function.t = { 'abc', 'def', 'z' }
y = { __index = { concat = function(x) return string.reverse(table.concat(x)) end } }
setmetatable(t, y)
z = t:concat() -- z = 'zfedcba'
table
works. I recommend using standard table.concat
and living with it. However, it is possible to muck around with something like this for a single table.t = { 'def', 'abc', 'A' }
mt = { __index = { concat = table.concat, sort = table.sort } }
setmetatable(t, mt)
a = t:concat() -- 'defabcA'
t:sort()
b = t:concat() -- 'Aabcdef'
Collection
at
Module:Age to see how I have sometimes defined a simple list. It's very weird to follow but
Module:IPblock has more examples includings :sort()
.
Johnuniq (
talk) 04:24, 26 March 2024 (UTC)ln: is a small wikipedia with less than 10 active users per month and non of them with lua knowledge. There are some lua errors in templates that were probably copy pasted in the past from fr: The pages can be seen here: https://ln.wikipedia.org/?title=Cat%C3%A9gorie:Pages_avec_des_erreurs_de_script&action=edit&redlink=1
Could someone who knows lua have a look to the templates ("modèle") and give us a hand or at least some advice? Thank you for helping. Bombo ( talk) 21:35, 18 April 2024 (UTC)
I'm working on a module that let's the user to use some predefined placeholders to add categories/text. For example, on the "Category:144 births" category they can add the following template
{{YearCategory
|__DECADE__s births
|Births by year
|RD(__CENTURY__)-century births
|__YEAR__ beginnings
|header=In this category you can find people who were born on [[__YEAR__]].
}}
And it will add the 140s births, Births by year, 2nd-century births and 144 beginnings categories to the page and will display a header with "In this category you can find people who were born on 144." content. The code is at hy:Մոդուլ:ԺամանակիԿատեգորիա and an example of usage at hy:Կատեգորիա:1944 ծնունդներ. It work fine except when I want to use those placeholders inside a template or an {{#if: block. As I understand, mediawiki parses the text before sending it to the Lua module. Is there a way to change this? ԱշոտՏՆՂ ( talk) 01:26, 21 April 2024 (UTC)
get
. —
andrybak (
talk) 01:05, 22 April 2024 (UTC){{YearCategory|{{
Decade|2024}}
births..
to implement the decade handling. You may need {{PAGENAME}} or something derived from it as the template parameter.
Certes (
talk) 09:34, 22 April 2024 (UTC)
{{{year}}}
to a decade using {{
DECADE}}
. —
andrybak (
talk) 15:44, 22 April 2024 (UTC)I wrote the following function:
local function linkToWikipage(link, text)
local page = mw.title.new(link)
if page.exists == true then
if page.redirectTarget then
local redir = page.redirectTarget.fullText
or page.redirectTarget.prefixedText
return "[[" .. redir .. "|" .. text .. "]]"
else
return "[[" .. link .. "|" .. text .. "]]"
end
else
return text
end
end
It works on a small scale, but on a larger scale I'm told too many expensive function calls. Can anyone help me find something cheaper that still fulfills the goals of:
Eievie ( talk) 03:35, 23 April 2024 (UTC)
frame:expandTemplate{title = 'Link if exists', args = link}
Greek and Coptic
but not if it said Greek and Coptic (Unicode block)
){{
Link if exists}}
for the latter. This does work (see
testcases here). It creates the links I need and doesn't create a "too many expensive function calls" error.mw.title.new()
is the expensive partpage.exists
and page.redirectTarget
are both cheap{{
Link if exists}}
both do the mw.title.new()
call{{
Link if exists}}
and
Module:Redirect?
Eievie (
talk) 16:14, 23 April 2024 (UTC)
page.exists
is the expensive part? So ordering the function this way would be cheaper?local function linkToWikipage(pageStr, text)
local pageObj = mw.title.new(pageStr)
if pageObj.redirectTarget then
local redir = pageObj.redirectTarget.fullText
or pageObj.redirectTarget.prefixedText
return "[[" .. redir .. "|" .. text .. "]]"
elseif pageObj.exists == true then
return "[[" .. pageStr .. "|" .. text .. "]]"
else
return text
end
end
You are invited to join the discussion at MediaWiki talk:Scribunto-doc-page-header § Category:Module documentation pages. — andrybak ( talk) 09:27, 14 May 2024 (UTC)