glepub/cfi

EPUB Canonical Fragment Identifiers — epubcfi(/6/4!/4/10/2:3) — the standard way to address a point inside a publication.

A CFI is a path of child steps. Even indices address element children (/2 is the first element child, /4 the second, …), odd indices the text between them, and ! steps out of the package document into the content document the itemref references. The part before the first ! identifies the spine item — SpineItem.cfi holds exactly that path — and the rest addresses a node inside the chapter, optionally ending in a :n character offset into a text node. A step may carry an [assertion], usually the id the addressed element is expected to have.

This module parses and prints point CFIs and locates them in a book’s spine. Range CFIs (epubcfi(/6/4!/4,/10/2:1,/10/2:5)) and the temporal/spatial offsets used for audio and images are not supported.

Types

pub type Cfi {
  Cfi(parts: List(List(Step)), offset: option.Option(Int))
}

Constructors

  • Cfi(parts: List(List(Step)), offset: option.Option(Int))

    Arguments

    parts

    One list of steps per document: the first walks the package document to an itemref, and each subsequent list follows a ! indirection into the referenced content document.

    offset

    The :n character offset into the text node the last step lands on.

pub type Step {
  Step(index: Int, assertion: option.Option(String))
}

Constructors

  • Step(index: Int, assertion: option.Option(String))

    Arguments

    index

    Even for element children, odd for the text between them.

    assertion

    The id the addressed element is asserted to have, from [...].

Values

pub fn locate(
  cfi: Cfi,
) -> Result(#(Int, option.Option(Cfi)), Nil)

Split a publication-level CFI into the spine position it addresses and the remainder pointing within that chapter’s document, if any.

pub fn parse(text: String) -> Result(Cfi, Nil)

Parse an epubcfi(...) string.

pub fn path_to_string(cfi: Cfi) -> String

The CFI path without the epubcfi(...) wrapper — the form used for the intra-document part of a fragment, and for joining onto a spine item’s base path with !.

pub fn spine_item(
  book: glepub.Book,
  cfi: Cfi,
) -> Result(#(glepub.SpineItem, option.Option(Cfi)), Nil)

The spine item a CFI addresses, with the remainder of the CFI pointing within that chapter’s document, if any.

pub fn to_string(cfi: Cfi) -> String

Print a CFI back out as an epubcfi(...) string.

Search Document