HTML widgets
The sum of the first $n$ odd numbers is $n^2$ \webwidget[decl=Nat.sum_odd_eq_sq]{lean}.
The badge shows whether the Lean proof assistant has checked this. When
its status changes, the paragraph is broken again around it.The page registers the widget. In outline (the page’s source has the full menu):
reflowtex.host.define('lean', { // \webwidget[decl=…]{lean}: instance.attrs.decl
measure(instance, env) { // the badge as segments, with a break point between each two
const { segs, gaps } = segment(label()); // at spaces, and after . and _
return {
segments: segs.map(t => env.measure(text(t))),
gaps: gaps.map(g => ({ width: g.space ? space : 0, penalty: g.space ? 100 : 300 })),
ends: { left: { cap: pad, cut: cutPad, overhang: 3.75 }, // closed and cut ends
right: { cap: pad + chevron, cut: cutPad, overhang: 3.75 } },
};
},
render(instance, host) { // host.piece: { from, to, left: 'cap' | 'cut', right: 'cap' | 'cut' }
const { from, to, left, right } = host.piece;
host.el.innerHTML = badge(textOf(from, to), left, right);
host.el.querySelector('button')?.addEventListener('click', e => openMenu(e.currentTarget, host.env));
},
});
// The menu's "Show the declaration" changes the label, then env.invalidate():
// the badge is measured again and the paragraph broken again around it.