I’m still working on my Periodic Table wiki, and I’m wondering about whether what I want to do is straightforward in wikitext. I can do it with JS, and will if I have to. But is there a way in TW to do something similar to the cascade mechanism, but with the logic centralized in a single tiddler rather than scattered with one tiddler per case?
If you look at an element, say Copper, you will see that the footer shows the values of most of the fields of this data tiddler. I actually want to add formatting to many of these, to display units or add links or some such. I’d like to get output something like this:
element: [[Copper]]
atomic-mass: Ar°(Cu) = 63.546
atomic-number: 29
atomic-radius: 1.6 pm
boiling-point: 2835 °K
density: 8.96 g/cm3
discoverer: Prehistoric
electronegativity: Pauling scale: 1.9
element-type: [[Transition Metal]]
first-ionization: 7.7264 kJ/mol
group: [[11|https://en.wikipedia.org/wiki/Group_11_element]]
melting-point: 1357.75 °K
metal: yes
metalloid: no
natural: yes
non-metal: no
number-of-electrons: 29
number-of-isotopes: 11
number-of-neutrons: 35
number-of-protons: 29
number-of-shells: 4
number-of-valence:
period: [[4|https://en.wikipedia.org/wiki/Period_4_element]]
phase: solid
radioactive: no
specific-heat: 0.385 J⋅kg−1⋅K−1
symbol: Cu
year: Prehistoric
But I don’t want to change the underlying data to add these units (especially because I think some of them may be wrong; I’m not a chemist!)
I’m imagining a macro, which I would call with something like
<!-- optional second parameter of tiddler, defaulted to current tiddler -->
<<display-field "melting-point">>
(although the field name in this case would likely be a variable, not hard-coded.)
I can do this in JavaScript with code like this:
const displayField = ((handlers = {
'atomic-mass': (v, e) => `A<sub>r</sub><sup>°</sup>(${e.symbol}) = ${v}`,
'boiling-point': (v, e) => `${v} °K`,
'density': (v, e) => `${v} g/cm<sup>3</sup>`,
'atomic-radius': (v, e) => `${v} pm`,
'electronegativity': (v, e) => `Pauling scale: ${v}`,
'element-type': (v, e) => `[[${v}]]`,
'first-ionization': (v, e) => `${v} kJ/mol`,
'group': (v, e) => v ? `[[${v}|https://en.wikipedia.org/wiki/Group_${v}_element]]` : ``,
'melting-point': (v, e) => `${v} °K`,
'metal': (v, e) => v || 'no',
'metalloid': (v, e) => v || 'no',
'natural': (v, e) => v || 'no',
'non-metal': (v, e) => v || 'no',
'period': (v, e) => v ? `[[${v}|https://en.wikipedia.org/wiki/Period_${v}_element]]`: ``,
'radioactive': (v, e) => v || 'no',
'specific-heat': (v, e) => `${v} J⋅kg<sup>−1</sup>⋅K<sup>−1</sup>`,
'year': (v, e) => v || e.discoverer, // interesting hack
}) => (field, element) =>
(handlers [field] || ((v) => v)) (element [field] || '', element)
) ()
(In each of these functions, v
is the field value, and e
the element, a data tiddler. If it’s in the way, I can choose to lose the second parameters; it’s only used in two places, and they’re inessential.)
I can easily convert these individual functions to wikitext, but I don’t know of a good way to convert the overall mechanism. I’m quite sure I can do this with the cascade. But that would mean having a separate tiddler for each of those functions. In one sense, I know that this is the TiddlyWiki way. I understand, and mostly agree with the Philosophy of Tiddlers, but I have a competing concern: the current goal of this wiki is for users to make their own copy and hack away at customizing for their own study, annotation, or other use of chemical information. These users will not likely be sophisticated TW users. But the parts should still be hackable. If they want to add a new field and they want it to get something more than the default formatting (just show the text), then I think it would be much easier to explain that they have to edit this tiddler than to explain the cascade to them.
I’m more than willing to be convinced otherwise: that this is a perfect use-case for cascade
. So feel free to explain what I’m missing.
But I’m most interested in how to write in wikitext something similar to a switch
statement or a if-elseif-elseif
chain. Is this possible? Can it be done in a way that casual users can easily figure out how to extend it?