Module talk:Excerpt/Archive 4

Archive 1 Archive 2 Archive 3 Archive 4

Disambiguating identical section names

I tried to transclude this section, but it produced an error:

{{excerpt|Climate change in India|Agriculture_2}}

Is this not possible because the transcluded section has the same name as another section in the same article? The other section appeared to be transcluded without errors:

{{excerpt|Climate change in India|Agriculture}}

Jarble (talk) 01:29, 9 November 2023 (UTC)

@Jarble:, this isn't an answer to your question, and it definitely deserves one, but here are a couple of ideas, more like workarounds, that might help. First thing I thought, was, "Why does it have two sections with the same name?" Sometimes this is justifiable due to MOS:NOBACKREF, and perhaps that's the case here, but sometimes you can just alter one of the section names so they are not the same anymore. Pay attention to any incoming section links from other articles, that you would have to update, if you choose this path; you can examine the results of Special:WhatLinksHere/Climate_change_in_India and search the results for "section"; there are only four sections with in-links, and none of them point to either of the "Agriculture" sections, so you are in luck; if you want to change one of them, you're free to, as long as you can find a reasonable name that others won't object to, and revert. The first one could maybe be "#Agricultural emissions" or "#Agricultural byproducts" or similar.
The second section is poorly named, as it isn't about agricultural byproducts, it's about the negative economic impact on people, especially poor people, due to reduced crop yields caused by climate change. I think "Reduced crop yields" would be a fine name for that section. If you decide to change it, be sure and leave a detailed edit summary stating your justification for it, and imho it would also be worth mentioning that you checked "WhatLinksHere" and there are no incoming links needing adjustment.
Back to your original question: I'm subscribed, so I'll be watching for a response, too. But in the meantime, I hope these ideas might help at least in this one case. Cheers, Mathglot (talk) 05:16, 9 November 2023 (UTC)
While MediaWiki itself allows specifying a number at the end of a section name to mean "the Nth occurance of that heading", the way the underlying module of excerpt (Module:Transcluder) works is that it attempts to find a heading with the exact wikitext you provide (see here), and has no concept of the repeated occurance of a header. This means that when you provide "Agriculture_2", it tries to find a heading with that exact wikitext instead of the 2nd occurance of "Agriculture". This could probably be implemented as a feature, since I'm pretty sure this isn't the first time I've seen this issue crop up. Aidan9382 (talk) 07:41, 9 November 2023 (UTC)
Anchors could be the solution here. I coded the module naively to expect the section heading text to match the parameter. This doesn't work when there's an anchor in there. For example, Album#Tracks has a heading of ==Tracks{{anchor|Music track}}==, and the only way to extract it is by matching that literally with {{excerpt|Album|Tracks{{((}}anchor{{!}}Music track{{))}}}}. It would be really nice if both {{excerpt|Album|Tracks}} and {{excerpt|Album|Music track}}} worked here. Then we could just add appropriately specific alternative names to subsections, e.g. by concatenating section + subsection titles in an anchor. Certes (talk) 10:15, 9 November 2023 (UTC)
I've had a go at this in Module:Transcluder/sandbox. It successfully finds anchors, both from the Anchor template and from the span tag to which subst:Anchor expands. It attempts to find headings which have an anchor next to them but fails. I introduced a bug somewhere on lines 482-486 but, as Scribunto disables even the crude debugging that comes with standard Lua, it eludes me. It all works perfectly in an offline Lua session without Scribunto. Certes (talk) 17:50, 9 November 2023 (UTC)
I managed to fix Album § Tracks not working (side effects of how lua handles variable assignment). Not entirely sure why Unusual features still doesnt work, will keep looking into that one. Aidan9382 (talk) 18:24, 9 November 2023 (UTC)
Getting anchors working seems like a nice addition, but isn't there a way to deal with the OP question without adding anchors? Does the Module have access to the generated html, or only the wikicode? If the former, then we could distinguish duplicates from the "id" in the span tag not matching the text of the <H> tags. E.g., for the "Agriculture" sections in Climate change in India we have one H3 and one H4:
  • <h3><span class="mw-headline" id="Agriculture">Agriculture</span>
  • <h4><span class="mw-headline" id="Agriculture_2">Agriculture</span>
The ToC clearly knows the difference; can we do what it's doing? Or do we not see that from the Module? Mathglot (talk) 18:34, 9 November 2023 (UTC)
The most html we get access to is probably whatever frame:preprocess() is willing to offer, which in this case (Using == test == as an example) is ==?'"`UNIQ--h-0--QINU`"'? test ==, and the strip marker isnt respected by mw.text.unstrip (it'll be replaced with an empty string since it doesn't like exposing raw HTML), so we have no way to generate the id that the html would normally have. Basically, I'm pretty sure we have to track headings ourself. Aidan9382 (talk) 18:40, 9 November 2023 (UTC)
Pity; thanks for that informative (and quick) reply. Mathglot (talk) 18:45, 9 November 2023 (UTC)
We could track heading counts in the wikicode, using gmatch rather than match, at the expense of adding a little more complexity. I suppose the second Agriculture section would be a second choice after trying and failing to find a section called literally "Agriculture 2". However, what we really want may be the Agriculture subsection of Economic impacts, regardless of whether it's the first, second or only heading of that name. That's better done with an anchor (if we brush aside the minor quibble that it doesn't actually work). Certes (talk) 18:51, 9 November 2023 (UTC)
(edit conflict) Not to complicate things unduly, but your wording above made me think, "Okay, why *don't* we somehow allow them to specify 'the Agriculture subsection of Economic impacts'?" E.g., something like:
  • {{excerpt|Climate change in India|Agriculture|in=Greenhouse gas emissions}}
  • {{excerpt|Climate change in India|Agriculture|in=Economic impacts}}
  • {{excerpt|Climate change in India|Agriculture|in=#top}} /* in case of H2 section */
Does that offer anything we could use? Mathglot (talk) 19:06, 9 November 2023 (UTC)
That works, again at the expense of complexity. I'm looking at this through the coder's end of the telescope rather than the editor's, so I don't know whether that's what's desired. We are just string matching; there's no fancy data structure from which to pick sectionText['Economic impacts']['Agriculture']. Certes (talk) 19:20, 9 November 2023 (UTC)
Yes, I assumed it would be complex. Trying a though experiment, I imagined adding a function to scan the entire text for section headers, build a hash ('dictionary'?) of arrays, with each section header being one value in the hash (which maybe would just have numeric-ish keys: 1, 1.1. 1.2, 2, ...), with two values, one being the section name, and the other value being an array consisting of the name of every parent section, up to level 2. I believe that could be constructed in one pass, but I haven't thought it out. Once we had that, I see two possibilities: either use the param |in= feature and consult the array for that section to see if it matches one of the higher level section names listed in the array, or more interestingly, see if our hash actually "matches" the mw-built ToC structure (why wouldn't it? they have to be doing something similar) and then in that case, we can just go back to Agriculture_2 (or Agriculture#2, or whatever) and figure it out on our own, without the "in" param. I grant it would be complex, but think of the glory. Could be worth double your normal editor salary, or even a barnstar. Mathglot (talk) 19:38, 9 November 2023 (UTC)
If you exported that as a library function, I bet that would be useful all over the place. Heck, maybe we could just ask wmf for it; someone over there must have something similar they could adapt for general use on our side. Mathglot (talk) 19:47, 9 November 2023 (UTC)
Managed to fix Abercwmeiddaw quarry § Unusual features too, though I felt a bit insane while investigating (I always forget gsub has a 2nd return that you have to be careful about, I thought mw.text.trim was somehow failing to trim a space). Aidan9382 (talk) 19:01, 9 November 2023 (UTC)
Hi! IMHO, I think just renaming one of the sections, or adding some invisible wikitext like an anchor, is the most sensible solution, especially considering how rare this situation seems to be. But if this situation isn't considered too rare, and a solution is required that doesn't imply renaming sections or adding invisible anchors, then perhaps the simplest approach would be to add a third parameter to getSection that simply skips a given number of sections, like so:
{{Excerpt|Climate change in India|Agriculture|skip=1}}
Ugly as hell, but surely simpler to implement and perhaps acceptable given the rarity of the situation. Also, no matter how sophisticated the solution, it seems to me like there will always be a need of an extra parameter and the user will always have to read some documentation about it, so if every solution is equally unintuitive to the user, we might as well pick the simplest one to implement. Sophivorus (talk) 23:04, 9 November 2023 (UTC)
(edit conflict) I love "ugly" when it's easy for a user to understand, and this surely is. Ugly is beautiful. Might need something like a MOS:HIDDENLINKADVICE hidden comment at the first one (or all of them, if skip=3; god I hope not...), letting editors know that they might break something remote from there, if they removed/renamed (any of) the duplicate section(s). Mathglot (talk) 23:18, 9 November 2023 (UTC)
I'm concerned that a change to a different section could quietlEy break the excerpt. For example, if we do {{Excerpt|Foo|Agriculture|skip=1}} (or whatever syntax we pick) then changing an earlier, unrelated section heading to or from Agriculture (or inserting or deleting the section entirely) will cause the wrong section or no text to come out. Certes (talk) 23:34, 9 November 2023 (UTC)
There's plenty of precedent for that, as we deal with it all the time with respect to all section redirects at Wikipedia; There are various approaches to dealing with it, of which MOS:HIDDENLINKADVICE is one. See the comment just above. Mathglot (talk) 23:39, 9 November 2023 (UTC)
Also, that is *already* a risk with Excerpt, any time you do a section excerpt, and we seem to accept that risk, and I don't know what proportion of added section excerpts that included the |skip= param would break, which would not already break even without that param. Mathglot (talk) 23:45, 9 November 2023 (UTC)
Broken excerpts are normally tracked from Category:Articles with broken excerpts and routinely fixed. Most broken excerpts with the skip parameter would end up there too. For example, in {{Excerpt|Climate change in India|Agriculture|skip=1}}, if the first Agriculture section gets renamed, then the second Agriculture section would be skipped, yielding an empty excerpt and thus categorizing the the page. If the second Agriculture section gets renamed instead, same result. Unless, of course, there happens to be another Agriculture section down below. But this would surely be super rare? That being said, the simpler solution of renaming the section or adding an anchor would avoid all that. Sophivorus (talk) 23:58, 9 November 2023 (UTC)
  Resolved

@Jarble: I have gone ahead and renamed that section to Climate change in India § Reduced crop yields, which is a better name for it anyway, even without the name collision. Feel free to excerpt from it now using that section name. (The "resolved" indicator is for the OP question, and not intended to stifle further conversation on the numerous interesting ongoing threads of discussion in this section, so by all means continue.) Mathglot (talk) 01:49, 10 November 2023 (UTC)

Lua error in mw.text.lua at line 25: bad argument #1 to 'match' (string expected, got nil).

Why does this error appear when I try to transclude this section?

{{excerpt|Variadic function|In Rust|subsections=yes}}

Jarble (talk) 17:56, 13 November 2023 (UTC)

Module:Transcluder's getTemplates() function is getting confused by the (eval $e:expr) => {{ [...] }} line as it thinks the presence of {{ is meant to indicate the start of a template and the code afterwards doesn't consider it could be receiving invalid data. I've fixed it with this edit. Aidan9382 (talk) 18:15, 13 November 2023 (UTC)

Param references=no doesn't skip sfn

I noticed that adding |references=no doesn't skip {{sfn}} templates. Shouldn't it? This example didn't do what I expected, rendering an {{sfn}} I thought would be stripped. Mathglot (talk) 04:07, 12 November 2023 (UTC)

I guess if we do address this, we would want to add something to the config, because the {{harv}} series would be in the same boat. (So would citation wrapper templates, in theory, but I don't hear any squeaks from those wheels.) Mathglot (talk) 19:04, 12 November 2023 (UTC)
@Mathglot I guess we could add something like referenceTemplates to the config. For now, this can be fixed by adding |templates=-sfn Sophivorus (talk) 13:20, 14 November 2023 (UTC)
Aarghh, why didn't I think of that?? Thanks! It probably would help in the long run, as there might be cases where someone would want to exclude sfn/harv, etc. but not other templates in the excerpted source. Mathglot (talk) 18:27, 14 November 2023 (UTC)

Section not found errors (Vichy France)

I'm getting 'Section not found' errors, but only when trying to excerpt from Vichy France. See this test link. Thanks, Mathglot (talk) 10:52, 15 November 2023 (UTC)

@Mathglot I think it's because the article contains <onlyinclude> tags, and Module:Transcluder is designed to respect them. In other words, the excerpt is looking for the sections within the <onlyinclude> tags, which only contain an infobox. Sophivorus (talk) 11:44, 15 November 2023 (UTC)
  Resolved. Thank you, Sophivorus; I should have noticed that. I'll look more carefully next time. Thanks again, Mathglot (talk) 11:55, 15 November 2023 (UTC)
That raises the question of why the article contains <onlyinclude> tags. They are sometimes added to suit the needs of one particular transcluding page, without thinking about what other transclusions may need. A better solution might be for the first transclusion to be done in some other way, perhaps with {{Excerpt}} or labeled section transclusion, allowing the transcluded article to play nicely with other pages which may wish to transclude it differently. Certes (talk) 12:59, 15 November 2023 (UTC)
Indeed, <onlyinclude> seems like outdated to me, only suitable for the template namespace (if at all). Sophivorus (talk) 14:37, 15 November 2023 (UTC)
noinclude and chums are perfect for template space, where everything that transcludes it needs to exclude its documentation, etc. Selective transclusion of articles is specific to the transcluding page and should be specified there, not in the donor article. Certes (talk) 15:49, 15 November 2023 (UTC)
I could see that it was transcluding the Infobox from another article, following the model at Help:Transclusion § Parametrization method (later modified here by Frietjes for reasons I don't understand, but that don't matter for the purposes of this discussion) and Help section § Parametrization method ought to be updated to repeat or link the caveat about using <onlyinclude> mentioned at the top of § Selective transclusion (as well as have the model use and examples changed, if Frietjes' edit is to be preferred over what the Help page states now).
In any case, I will find some other solution, either another WP:SELTRANS solution that doesn't involve the <lonlyinclude>, or perhaps simply moving the Infobox to its own template, where it can be transcluded by both articles. That would be the simplest to implement, but I'm not sure it's the best solution, since that Infobox-template is unlikely to be transcluded by more than just those two articles, and I don't know if templates are the best choice for that kind of situation, which is why I used SELTRANS in the first place in Vichy France.
It would be helpful if the Help page could offer guidance about when, and not only how, to use it, and about what type of application each of the offered seltrans solutions is best for. If noinclude and chums are inadvisable for mainspace, maybe the Help page should say something about that, to. As things stand, I'm not sure what the best solution is for that Infobox, but I'm leaning towards labeled section transclusion, which doesn't require any of the <include> family. Thanks everyone for these helpful comments; it's helping me understand seltrans better, and I hope some of this can rub off on the doc, so others can benefit likewise. Mathglot (talk) 20:08, 15 November 2023 (UTC)
As far as doc updates, I've started the ball rolling with an update to Template:Excerpt/doc (diff). Mathglot (talk) 21:03, 15 November 2023 (UTC)
I updated it to use module:excerpt, so no more <onlyinclude>...</onlyinclude>. Frietjes (talk) 22:01, 15 November 2023 (UTC)
The many thousands of articles transcluding the 27,000+ articles with onlyinclude tags, 3,000+ articles with includeonly tags and 2,000+ articles with noinclude tags, would probably benefit from excerpts. Many replacements could be done with JWB and it would be an efficient and effective way to spread the awareness of excerpts. Sophivorus (talk) 13:07, 16 November 2023 (UTC)
unfortunately, as far as I can tell, neither WP:LST nor Module:Excerpt can pass parameters to modules in the excerpt section (e.g., 2017 FIFA U-17 World Cup), so not all of those cases can be converted. also, I am not sure how you excerpt one number from an infobox (e.g., Game of Thrones) or a paragraph (e.g., Google Translate). Frietjes (talk) 16:00, 16 November 2023 (UTC)
To excerpt one number from an infobox, there's {{Template parameter value}}, which is designed for exactly that situation. Aidan9382 (talk) 16:05, 16 November 2023 (UTC)
interesting, are there problems with post-expand size limits, or is there another reason why that's not being used with these articles? Frietjes (talk) 16:18, 16 November 2023 (UTC)
The most likely reason it wasn't used is because it isn't well known and was also quite buggy until a 2023 revamp to the module code behind it. The main issue it could have is lua memory/time usage but I'm not sure if these would be substantial (Also, in the case of Game of Thrones, it already appears to be used via {{Aired episodes}}, so those tags might be redundant). Aidan9382 (talk) 16:29, 16 November 2023 (UTC)
{{Template parameter value}} has been used successfully in similar situations. For example, The Simpsons used to enclose the number of episodes in <onlyinclude>...</onlyinclude> tags so that related articles could show a current count by transcluding the whole page. That prevented its lead from appearing in a portal (using Module:Excerpt, which respects the tag). Related articles now extract just the template parameter value, giving other applications access to the whole article. Certes (talk) 18:53, 16 November 2023 (UTC)