Module talk:Lang-zh: Difference between revisions

Content deleted Content added
m Archiving 1 discussion(s) to Module talk:Lang-zh/Archive 5) (bot
Line 21:
{{archive box | auto=yes }}
__TOC__
 
== Trailing bold in l= not being removed ==
 
In <syntaxhighlight>{{zh|t=竹子林站|j=Zuk1 Zi2 Lam4 Zaam6|l = '''Bamboo Forest station'''}}</syntaxhighlight>, the opening bold markup is properly removed, but the trailing bold markup is not removed. It looks like the regular expression at <syntaxhighlight>term = string.gsub(term, "^([ \"']*)(.*)([ \"']*)$", "%2")</syntaxhighlight> needs some adjustment to the middle wildcard search. – [[User:Jonesey95|Jonesey95]] ([[User talk:Jonesey95|talk]]) 13:23, 16 September 2024 (UTC)
 
:{{ping|Jonesey95}} This is because the * operator is greedy, so .* matches everything else in the string. Changing .* to .*? would make it lazy, so that the final term catches all trailing characters. In other words, change the line of code to: <syntaxhighlight>term = string.gsub(term, "^([ \"']*)(.*?)([ \"']*)$", "%2")</syntaxhighlight> [[User:Freelance Intellectual|Freelance Intellectual]] ([[User talk:Freelance Intellectual|talk]]) 13:51, 16 September 2024 (UTC)
::Thanks! That fixed the problem at [[Zhuzilin station]] and probably other pages. – [[User:Jonesey95|Jonesey95]] ([[User talk:Jonesey95|talk]]) 17:26, 16 September 2024 (UTC)
::Thank you for fixing my shoddy regex, by the way. <span style="border-radius:2px;padding:3px;background:#1E816F">[[User:Remsense|<span style="color:#fff">'''Remsense'''</span>]]<span style="color:#fff">&nbsp;‥&nbsp;</span>[[User talk:Remsense|<span lang="zh" style="color:#fff">'''论'''</span>]]</span> 13:05, 19 September 2024 (UTC)
:::{{re|Jonesey95|Remsense}} On further reflection, this doesn't work as intended. I had thought the string was a regex, but it is in fact a Lua pattern, which is slightly different. The Lua equivalent of *? is - which would give: <syntaxhighlight>term = string.gsub(term, "^([ \"']*)(.-)([ \"']*)$", "%2")</syntaxhighlight> Writing .*? in Lua (as I suggested above) actually means greedily matching all characters (.*) followed by a single question mark (? can also be an operator, but Lua pattern operators can't be nested so in this context it is interpreted as a literal). So actually the new pattern usually doesn't make a substitution, unless there is a question mark. This means it usually fails, e.g. where there are multiple glosses separated by commas and spaces, the spaces are not stripped. However, looking at what the pattern match applies to, I'm not completely sure I understand why the quotes should be stripped in the first place (is there a set of testcases to check against?). At [[Zhuzilin station]], the current code makes no substitution, and so it keeps the bold formatting, presumably as intended. The old code meant that the bold formatting was stripped at the beginning and not the end, so the rest of the article became bold (which was a bad and confusing error). Correcting .*? to .- as above would strip both, making it impossible to add bold formatting. Is the intention to catch cases where an editor unnecessarily adds quotes to the gloss? Is this a common problem? If so, is removing the ability to add bold and italic formatting a fair price to pay?
::: If we want to strip one quote mark but no more (so that we catch editors manually adding quotes, but allow formatting), pattern matching is a bit more complicated. I think it would be easiest to separate the stripping of whitespace and quotes. When stripping one single quote, we need to check that there isn't more than one, but we also need to allow the string to contain an apostrophe (so we can't just use [^']- in the middle) and a gloss could potentially be a single character (so we can't just use [^'].-[^'] in the middle). So it seems easiest to strip the leading and trailing quotes separately. This gives three lines (I've also removed two sets of brackets that were capturing substrings that weren't used): <syntaxhighlight>term = string.gsub(term, "^ *(.-) *$", "%1")
term = string.gsub(term, "^[\"']?([^\"'].-)$", "%1")
term = string.gsub(term, "^(.-[^\"'])[\"']?$", "%1")</syntaxhighlight> [[User:Freelance Intellectual|Freelance Intellectual]] ([[User talk:Freelance Intellectual|talk]]) 15:43, 24 September 2024 (UTC)
::::I think it's fine to strip all quote marks, in any quantity. That was the original intent of the code, and I don't see any complaints on this page. Adding bold to text is probably against [[WP:MOS]], and adding italics should be done with a parameter. People can use {{tag|b}} and {{tag|i}} tags if they insist on them. – [[User:Jonesey95|Jonesey95]] ([[User talk:Jonesey95|talk]]) 15:51, 24 September 2024 (UTC)
:::::Okay. I had taken your comment about fixing the Zhuzilin station article to mean that keeping the bold markup was intended, but I can see why it could be discouraged. I've also just found [[Template:Lang-zh/testcases]] (I had only looked under Module:Lang-zh before), and I don't see any testcases for stripping markup. So, if stripping markup is the desired functionality, the .- version above would work. I think it would make sense to document this, since there are three different kinds of thing being stripped: whitespace, markup, and quotes (double quotes aren't markup). It could be documented either on [[Template:Lang-zh/doc]] or directly as a code comment next to the line we're discussing, e.g. "remove trailing and leading spaces, quotes, and bold/italic markup". [[User:Freelance Intellectual|Freelance Intellectual]] ([[User talk:Freelance Intellectual|talk]]) 20:39, 24 September 2024 (UTC)
:::::Currently, this stripping only applies to literal glosses and not translations, but they should reasonably be treated the same. So, fixing the pattern, matching all whitespace (not just spaces), expanding the comments, and applying the same to the translation, I suggest changing lines 236-247 to the following:<syntaxhighlight> elseif (part == "l") then
local terms = ""
-- put individual, potentially comma-separated glosses in single quotes
-- (first strip leading and trailing whitespace and quotes, including bold/italic markup)
for term in val:gmatch("[^;,]+") do
term = string.gsub(term, "^([%s\"']*)(.-)([%s\"']*)$", "%2")
terms = terms .. "&apos;" .. term .. "&apos;, "
end
val = string.sub(terms, 1, -3)
elseif (part == "tr") then
-- put translations in double quotes
-- (first strip leading and trailing spaces and quotes, including bold/italic markup)
val = string.gsub(val, "^([%s\"']*)(.-)([%s\"']*)$", "%2")
val = "&quot;" .. val .. "&quot;"
end</syntaxhighlight> [[User:Freelance Intellectual|Freelance Intellectual]] ([[User talk:Freelance Intellectual|talk]]) 09:31, 25 September 2024 (UTC)
:::::{{re|Jonesey95|Remsense}} What do you think? Are you happy with the above suggestion?
{{edit template-protected|Module:Lang-zh|answered=yes}}
:::::Also, instead of directly using a Lua string pattern, it might be more readable and maintainable to use an existing function for stripping leading and trailing characters, namely mw.text.trim:<syntaxhighlight> elseif (part == "l") then
local terms = ""
-- put individual, potentially comma-separated glosses in single quotes
-- (first strip leading and trailing whitespace and quotes, including bold/italic markup)
for term in val:gmatch("[^;,]+") do
term = mw.text.trim(term, "%s\"'")
terms = terms .. "&apos;" .. term .. "&apos;, "
end
val = string.sub(terms, 1, -3)
elseif (part == "tr") then
-- put translations in double quotes
-- (first strip leading and trailing spaces and quotes, including bold/italic markup)
val = mw.text.trim(val, "%s\"'")
val = "&quot;" .. val .. "&quot;"
end</syntaxhighlight> [[User:Freelance Intellectual|Freelance Intellectual]] ([[User talk:Freelance Intellectual|talk]]) 09:02, 27 September 2024 (UTC)
::::::{{re|Jonesey95|Remsense}} pinging again. The current code inserts quotes incorrectly, e.g. on the following pages you can see an opening quote followed by a space: [[Gun (staff)]], [[Indonesian slang]], [[Ping On]]. The code above would fix this. [[User:Freelance Intellectual|Freelance Intellectual]] ([[User talk:Freelance Intellectual|talk]]) 14:23, 8 October 2024 (UTC)
::::::: Reping {{u|Remsense}}. Could you please look at this? I think you're one of the only template editors who has enough understanding of Chinese orthography to understand what is being changed here and why. [[User:Pppery|* Pppery *]] [[User talk:Pppery|<sub style="color:#800000">it has begun...</sub>]] 17:19, 16 November 2024 (UTC)
::::::::Will have this looked at ASAP. Huge apologies for letting it slip through the cracks for months. <span style="border-radius:2px;padding:3px;background:#1E816F">[[User:Remsense|<span style="color:#fff">'''Remsense'''</span>]]<span style="color:#fff">&nbsp;‥&nbsp;</span>[[User talk:Remsense|<span lang="zh" style="color:#fff">'''论'''</span>]]</span> 23:39, 16 November 2024 (UTC)
:::::::::{{done}}{{snd}}so sorry for the delay, again. <span style="border-radius:2px;padding:3px;background:#1E816F">[[User:Remsense|<span style="color:#fff">'''Remsense'''</span>]]<span style="color:#fff">&nbsp;‥&nbsp;</span>[[User talk:Remsense|<span lang="zh" style="color:#fff">'''论'''</span>]]</span> 23:37, 18 November 2024 (UTC)
 
== The unnamed parameter ==