This page has archives. Sections older than 90 days may be automatically archived by ClueBot III when more than 1 section is present. |
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)
- @MSGJ: This is because string objects in Lua have a metatable where the
__index
points to the thestring
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)- Does that mean if you use
setmetatable(t, y)
you could then uset:concat()
? — Jts1882 | talk 14:29, 22 March 2024 (UTC)- Not unless
t.concat
is a function. t:concat()
is defined ast.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)
- 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)
- No, there is no good way to alter the way
table
works. I recommend using standardtable.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)- That seems like it could be worth splitting off into its own module… jlwoodwa (talk) 07:18, 24 June 2024 (UTC)
- No, there is no good way to alter the way
- Thanks both for your replies. A lot to process there, but looks very useful/interesting. Is there an easy way to do the following:
- Not unless
- Does that mean if you use
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)
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)
- Module:Excerpt is at least one or two users' attempt to date. Izno (talk) 02:23, 3 June 2024 (UTC)
- 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)
- Using
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)
- Update. I have mentioned the problem at MediaWiki. --Grufo (talk) 12:09, 4 June 2024 (UTC)
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)
- 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)
- I think that was what I was looking for, thanks! Gonnym (talk) 11:04, 6 June 2024 (UTC)
- I am glad that helped! --Grufo (talk) 11:16, 6 June 2024 (UTC)
- I think that was what I was looking for, thanks! Gonnym (talk) 11:04, 6 June 2024 (UTC)
- Isn't "takes a page name and converts it into wikitext" just
getContent
? Izno (talk) 15:58, 6 June 2024 (UTC)- Yes, but I was asking how to access it from wikitext and not Lua. Gonnym (talk) 16:00, 6 June 2024 (UTC)
- That sounds like transclusion: {{:Page name}}. Certes (talk) 17:12, 6 June 2024 (UTC)
- Already solved above. Gonnym (talk) 17:56, 6 June 2024 (UTC)
- That sounds like transclusion: {{:Page name}}. Certes (talk) 17:12, 6 June 2024 (UTC)
- Yes, but I was asking how to access it from wikitext and not Lua. Gonnym (talk) 16:00, 6 June 2024 (UTC)
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]]''' (talk • contribs) 04:20, 11 June 2024 (UTC)
- How would the module decide which of the two colors to output? Izno (talk) 06:25, 11 June 2024 (UTC)
I saw this post on Stack Overflow. Could this be implemented in Lua? '''[[User:CanonNi]]''' (talk • contribs) 09:36, 11 June 2024 (UTC)
- {{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)- @Gonnym this should work, thanks! '''[[User:CanonNi]]''' (talk • contribs) 09:38, 11 June 2024 (UTC)
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:
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)
- 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)
- Thanks. I suspected as much, but wasn't sure. I'll look into other avenues. Mathglot (talk) 08:06, 6 July 2024 (UTC)
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.