Skip to main content

encode_anchor

Function encode_anchor 

Source
pub fn encode_anchor(text: &str) -> String
Expand description

Encodes one text for an anchor (the identifier a link points at; see ANCHORS): a label’s type, the label after the colon, or a heading title’s plain text.

The encoding depends on the text alone: no document-order counter takes part, so one text always yields one encoding, in this document and in any other.

There are two paths.

  • The exact path, taken when the text holds only ASCII letters, ASCII digits, ., - and single spaces (none leading, trailing or doubled), is not empty and neither begins nor ends with a hyphen: the text is reproduced character for character, case preserved, with every space written _. intro stays intro; quantum error-correcting code becomes quantum_error-correcting_code; main.v2 stays main.v2.
  • The tagged path, taken for every other text: the text is NFKD-decomposed (a compatibility decomposition, which splits an accented letter into its base letter and a combining accent), the combining marks are dropped, and what is left is reduced: an ASCII letter, an ASCII digit, . and - are written as themselves, and every maximal run of other characters becomes one _ when the run is whitespace only and one - otherwise; - and _ are trimmed from both ends. A ~ and a tag of six hexadecimal digits computed from the text as written (before the decomposition) are appended, so that two texts that reduce alike still differ. Éléments 2 becomes Elements_2~<tag>; \(\mathcal{C}_\alpha^\infty\) becomes mathcal-C-alpha-infty~<tag>; the empty text becomes ~<tag>.

A ~ never appears on the exact path and always on the tagged one, so the two paths cannot produce the same encoding.

Why a hyphen at either end leaves the exact path. An anchor made from a label joins its two encoded parts with one hyphen (label_anchor), and one made from a title or a position joins its parts with two (title_anchor, auto_anchor), so the separators tell the two kinds apart, unless a hyphen of the encoded text falls beside the joining one. That happens at both ends: a label whose own text begins with a hyphen would turn sec:-Foo into sec--Foo, the anchor of an unlabeled heading titled Foo, and eq:-7 into eq--7…, the shape of an unlabeled equation line’s anchor; a label whose type ends with a hyphen would turn sec-:Foo into the same sec--Foo. On the tagged path the hyphen is trimmed and a tag is appended, so the anchor of a label never takes the shape of the other kinds, regardless of what the label says. No standard construct accepts such a label type, since each accepts a fixed list of types; a reference point that accepts any type can receive one.

§Examples

use flm_core::stdlib::features::refs::encode_anchor;

assert_eq!(encode_anchor("intro"), "intro");
assert_eq!(encode_anchor("main.v2"), "main.v2");
assert_eq!(encode_anchor("quantum error-correcting code"),
           "quantum_error-correcting_code");

// Anything else takes the tagged path.
let tagged = encode_anchor("Éléments 2");
assert!(tagged.starts_with("Elements_2~"));
assert_eq!(tagged, encode_anchor("Éléments 2"));