I’m so happy for the relatively recent join
attribute on the ListWidget. But it’s a bit too naive for my tastes. It give this behavior:
A B C D: “A, B, C, D”
A B C: “A, B, C”
A B: “A, B”
A: “A”
When what I really want is this:
A B C D: “A, B, C, and D”
A B C: “A, B, and C”
A B: “A and B”
A: “A”
(and yes, I like Oxford commas! )
I can write a procedure to do this:
\procedure add-commas(filter)
<$list filter=<<filter>> counter="counter"><% if [<counter-first>!match[yes]] %><% if [<counter-last>match[yes]] %><% if [<counter>match[2]] %><$text text=" and "/><% else %><$text text=", and "/><% endif %><% else %><$text text=", "/><% endif %><% endif %>{{!!title}}</$list>
\end
Question 1: That’s fairly ugly. Is there a cleaner way to do this?
Question 2: It would still need to be extended to accept a procedure/macro to handle the display of the list item—we won’t always want just {{!!title}}
. I’ve done that before with macros and <$macrocall>
, but my quick attempt to do that with <$transclude>
didn’t work, and nor did it work properly when I fall back to macros. Do you have any pointers for me?
Question 3: Does anyone have a good way to lay out <% if %>
clauses for inline use (meaning that they generate no extraneous spaces.) I did the following but am not real happy with it:
\procedure add-commas(filter)
<$list filter=<<filter>> counter="counter"><%
if [<counter-first>!match[yes]]
%><%
if [<counter-last>match[yes]]
%><%
if [<counter>match[2]]
%><$text text=" and " /><%
else
%><$text text=", and "/><%
endif
%><%
else
%><$text text=", "/><%
endif
%><%
endif
%>{{!!title}}</$list>
\end
Do you have a cleaner suggestion? (Yes, I do know that space collapse in HTML, but I would likely want to use this inside preformatted sections as well.)
You can see these in action, including my broken attempt at passing a macro by downloading the following and dragging it to any old wiki.
add-commas.json (2.9 KB)