Jump to content

Wikipedia talk:Lua

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
(Redirected from Wikipedia talk:Lua/To do)

Methods

[edit]

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)[reply]

@MSGJ: This is because string objects in Lua have a metatable where the __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)[reply]
Does that mean if you use setmetatable(t, y) you could then use t:concat()? —  Jts1882 | talk  14:29, 22 March 2024 (UTC)[reply]
Not unless t.concat is a function.
t:concat() is defined as t.concat(t).
For the latter to work, t.concat has to be a function.
A working example:
t = { 'abc', 'def', 'z' }
y = { __index = { concat = function(x) return string.reverse(table.concat(x)) end } }
setmetatable(t, y)
z = t:concat()  -- z = 'zfedcba'
Johnuniq (talk) 05:33, 23 March 2024 (UTC)[reply]
Thanks both for your replies. A lot to process there, but looks very useful/interesting. Is there an easy way to do the following:
  • Associate a particular table with the standard table library
  • Associate all tables by default with the standard table library
— Martin (MSGJ · talk) 09:34, 25 March 2024 (UTC)[reply]
No, there is no good way to alter the way 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'
Search for 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)[reply]
That seems like it could be worth splitting off into its own module… jlwoodwa (talk) 07:18, 24 June 2024 (UTC)[reply]

 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)[reply]

parsing the first sentence

[edit]

Does anybody know a parsing method to reliably get the first sentence of an article on a Lua module, removing templates such as infoboxes completely? -1ctinus📝🗨 00:13, 3 June 2024 (UTC)[reply]

Module:Excerpt is at least one or two users' attempt to date. Izno (talk) 02:23, 3 June 2024 (UTC)[reply]
Using {{excerpt|Lion|paragraph=1|only=paragraphs|hatnote=no}} gets the first paragraph of the Lion article (minus infoboxes, templates, etc). Add |links=no to get plain text. It should be straightforward to get the first sentence from that.  —  Jts1882 | talk  11:49, 3 June 2024 (UTC)[reply]

Editing the args metatable of a child frame

[edit]

Module:Params offers the possibility to map or rename parameters using either helper templates or modules or parser functions. Currently, in the case of mapping_by_invoking and renaming_by_invoking, while iterating, a new child frame gets created for each parameter (#1, #2). This means that if the parameters to map or rename are more than about a hundred the code breaks, because, as the Scribunto-Lua manual says, “The number of frames that may be created at any one time is limited”.

I have tried to solve the problem in Module:Params/sandbox, but all my attempts at keeping only one child frame throughout the loop failed (#3, #4, #5, #6). The reason is that once the child frame object gets created I can no longer edit its .args metatable. Yet, I would need to do that, because each module invocation needs to receive different parameters.

Testcases are available at Module:Params/testcases/tmaps and Module:Params/testcases/tmaps sandbox. Any idea on how to solve the problem and being able to map more than 100 parameters using a module? --Grufo (talk) 15:41, 3 June 2024 (UTC)[reply]

Update. I have mentioned the problem at MediaWiki. --Grufo (talk) 12:09, 4 June 2024 (UTC)[reply]

Looking for an existing module that takes a page name and converts it into wikitext

[edit]

Is there an existing Lua module like that either (a) takes a page name and converts it into wikitext or (b) takes a page name and regex and returns the result text (something similar to Module:String2#findpagetext)? Don't want to write a new module if one already exists and I thought something like this exists but I can't find it. Gonnym (talk) 09:20, 6 June 2024 (UTC)[reply]

I am not sure I understood exactly what you are looking for, but for getting the wikitext of a page there is {{msgnw}} – you should use the double colon if the page is in the main namespace, as in {{msgnw::Richard Feynman}}. That text transcluded in this way can then be passed as a parameter to a template or a module (and this includes {{#invoke:string|match}}, if that's what you are looking for). And so
{{#invoke:string|match|s={{msgnw::Richard Feynman}}|pattern=known%s+for%s+([^.]+)|nomatch=}}
yields
his work in the [[path integral formulation]] of [[quantum mechanics]], the theory of [[quantum electrodynamics]], the physics of the [[superfluidity]] of supercooled [[liquid helium]], as well as his work in [[particle physics]] for which he proposed the [[parton model]]
--Grufo (talk) 11:01, 6 June 2024 (UTC)[reply]
I think that was what I was looking for, thanks! Gonnym (talk) 11:04, 6 June 2024 (UTC)[reply]
I am glad that helped! --Grufo (talk) 11:16, 6 June 2024 (UTC)[reply]
Isn't "takes a page name and converts it into wikitext" just getContent? Izno (talk) 15:58, 6 June 2024 (UTC)[reply]
Yes, but I was asking how to access it from wikitext and not Lua. Gonnym (talk) 16:00, 6 June 2024 (UTC)[reply]
That sounds like transclusion: {{:Page name}}. Certes (talk) 17:12, 6 June 2024 (UTC)[reply]
Already solved above. Gonnym (talk) 17:56, 6 June 2024 (UTC)[reply]

Looking for module that processes black/white text.

[edit]

Hi. I'm looking for a module that can output 000000 or FFFFFF depending on the color code inputted. Is one available? Thanks. '''[[User:CanonNi]]''' (talkcontribs) 04:20, 11 June 2024 (UTC)[reply]

How would the module decide which of the two colors to output? Izno (talk) 06:25, 11 June 2024 (UTC)[reply]
I saw this post on Stack Overflow. Could this be implemented in Lua? '''[[User:CanonNi]]''' (talkcontribs) 09:36, 11 June 2024 (UTC)[reply]
{{Infobox television season}} uses {{Greater color contrast ratio|{{If empty|{{{bg_colour|}}}|#CCCCFF}}|black|white}} (simplified it), if this is can help you. Gonnym (talk) 09:36, 11 June 2024 (UTC)[reply]
@Gonnym this should work, thanks! '''[[User:CanonNi]]''' (talkcontribs) 09:38, 11 June 2024 (UTC)[reply]

Executing and scraping results from an advanced search url

[edit]

Is a module capable of executing an WP:Advanced search via a url param that is passed to it, and then processing the output of the search? I would like to be able to pass this url to a Module, have it execute the search, and return the total number of results by scraping it out of the content. For example, it should return value 4 given this url:

https://en.wikipedia.org/w/index.php?search=Draft%3A+hastemplate%3AOKA++hastemplate%3AAfC_submission%2Fdraft&title=Special:Search&profile=default&fulltext=1

The resulting Html contains the following which looks easily scrapable, if a module can access it:

<div class="results-info" data-mw-num-results-offset="0" data-mw-num-results-total="4">Results <strong>1 – 4</strong> of <strong>4</strong></div>

The module should be capable of handling any url that represents an advanced search, and find the total results. Is that possible? Mathglot (talk) 07:04, 6 July 2024 (UTC)[reply]

No. Searching uses Special:Search and modules cannot access special pages which are not based on wikitext. I don't know what API may be available for use by JavaScript. Johnuniq (talk) 08:01, 6 July 2024 (UTC)[reply]
Thanks. I suspected as much, but wasn't sure. I'll look into other avenues. Mathglot (talk) 08:06, 6 July 2024 (UTC)[reply]

Request for Lua code review: New language module

[edit]

Reposting this village pump post for visibility: Wikipedia:Village_pump_(technical)#Request_for_Lua_code_review:_New_language_module

Feedback welcome on the talk page here.

--Nonabelian (talk) 15:27, 25 July 2024 (UTC)[reply]