Formatting only the integers of a field with macro; splitregexp?

Hello there! As the title says, I’m trying to understand how to look at the contents of a field, and if that field has integers in it, display it with the said integers formatted through a simple macro
(so they are displayed with an icon and a special color).

To give more context:
I’m working with a ViewTemplate that lists a number of fields. I have a main global macro like this:

\define entity(icon, label, value)
<div class="mark" title="$icon$ $value$"><span class="label entity clean"><span class="icon-$icon$">$label$</span></span><span class="data entity clean">$value$</span></div>
\end

which I’m using in other simpler macros like:

\define cost-hp(value)
<$macrocall $name=entity icon=cost-hp value=$value$/>
\end

So that when I use for example <<cost-fp {{!!base_cost}}>>, it displays the field “base_cost” (which contains “2” in the example below) like this:
image
(the “FP” is the icon here, all this is done through an icon font and CSS).

This works great when the fields contain exactly what I want displayed, no more, no less.

But staying in the current example, if that field contains for example “cannot be maintained”, rather than an actual cost of “2”, I’d like to NOT format it with the little icon and color. In other words, I’m looking for a simple method to format it if contains a number, and not if it doesn’t.

This is the complete View Template I’m working in:

<$list filter="[all[current]tag[GURPS Spell]]">

"""
//<$link to={{!!college}}>{{!!college}}</$link>// - ''{{!!class}}''<span style="float:right">//Source: {{!!source}}//</span>//<h3><$list filter="[<currentTiddler>has[prerequisites]]">''<hr/>Prerequisites'': {{!!prerequisites}}</$list></h3>//<hr/>
''Energy cost to cast'': <<cost-fp {{!!base_cost}}>><$list filter="[<currentTiddler>has[additional_cost]]">/ ''Additional cost'': {{!!additional_cost}}</$list>
''Time to Cast'': <<time {{!!casting_time}}>>
''Duration'': <<duration {{!!duration}}>><$reveal type="nomatch" state="!!concentration" text="No">, requires concentration to maintain.</$reveal>
''Items'': {{!!items}}
''Cost to create'': {{!!cost_item}}
"""
</$list>

The field I’m trying to format conditionally is “additional_cost”, so that if it doesn’t contain a number, it’s rendered normally ( {{!!additional_cost}} ), and, if it does contain a number, sort of reformat it so the number(s) and only the number(s) are formatted with the macro (<<cost-fp a_number>> to maintain).

I was thinking this should be possible but may require the use of more advanced stuff than I’m capable to handle yet, like splitregexp and various clever syntaxes. Perphaps even copying the contains of the field to a temp tiddler or something… Anybody clever enough to figure it out elegantly? :slight_smile:

Does this work for you?

<$list filter="[<currentTiddler>has[additional_cost]]">
''Additional cost'':
	<$list filter="[{!!additional_cost}split[ ]]" variable="seg">
		<% if [<seg>regexp[\d+]] %>
			<$macrocall $name=cost-fp value=<<seg>> />
		<% else %>
			<<seg>>
		<% endif %>
	</$list>
</$list>

What I did here:

  • Split the contents of the field additional_cost at each space.
    • Each result gets assigned to the variable <<seg>> for “segment”; this keeps <<currentTiddler>> pointing to its original value in case you need to reference other field values.
    • If you don’t care whether the value of <<currentTiddler>> changes, you could omit variable="seg" and replace <seg> and <<seg>> in the $list contents with {!!title} and {{!!title}} or <currentTiddler> and <<currentTiddler>>, respectively.
  • For each <<seg>>:
    • If it contains 1+ digits (that’s what we’re searching for with regexp[\d+]), use it as the value of the cost-fp macro.
      • Note that I’m using the longform $macrocall instead of the shortform <<cost-fp>>; this allows me to use variables/transclusions as parameters. Formally, shortform macrocalls only support literal strings. (A quick side note: <<cost-fp {{!!base_cost}}>> may be working for you as {{!!base_cost}} is getting substituted for $value$ in the cost-hp definition, but this kind of substitution can be brittle and it’s best not to rely on it.)
    • Otherwise, simply render the value of <<seg>>.

This code relies on conditional shortcut syntax, available in v5.3.2+, but you could substitute a $list widget if you’re using an older version. Similarly, you could refactor some of your existing $lists like <$list filter="[<currentTiddler>has[additional_cost]]">, which would become <% if [<currentTiddler>has[additional_cost]] %>. Conditional syntax is a little shorter and more semantic, IMO, and doesn’t have the same potential pitfalls with the changing value of <<currentTiddler>>.

Let me know how it goes!