(samizdat doc)

Reference

Overview

The document tree that commands make and converters print: Block, Inline, Document.

(import (samizdat doc))

;; A converter is a match on the two unions.
(: words (-> (Vec Block) int))
(defun (words bs)
  (loop (:for b bs)
        (:acc n (folding 0
                  (match b
                    ((Para xs) (+ n (vec-length (string-split (inlines->text xs) " "))))
                    (_ n))))))

(meta-string doc "title")    ; (Some "Cookies, carefully")

A Document holds the result of a document after its commands ran. It does not say how the document looks. The commands in (samizdat commands) make the tree. A converter, such as (samizdat html), prints it.

A converter is a function of a Document. It matches on Block and Inline. The compiler reports a case that the match does not handle.

Two pairs of cases are for content that the other cases do not cover:

document->sexp is a small converter. The tests use it to compare trees.

See also: Document, Block, Inline, Table, meta-string, document-headings, document->sexp

Reference

Types

Functions

Types

Align

union ↑ Types

How the cells of a table column are aligned.

AlignDefault
No alignment was given. The converter decides.
AlignLeft
Aligned to the left.
AlignCenter
Centred.
AlignRight
Aligned to the right.

See also: Table

Table

record ↑ Types

A table: a caption, the alignment of each column, the head rows, and groups of body rows.

caption
The caption. It is empty when the table has no caption.
aligns
One alignment for each column.
head
The rows of the head. Each row is a vec of cells, and each cell is a vec of inlines. It is empty when the table has no head.
bodies
The groups of body rows. A rule in the source divides two groups. Each group is a vec of rows.

This is the table model of Org. A Markdown table also fits: it has one head row and one group. Every row has one cell for each column. @read-table in (samizdat lightweight) makes a Table.

See also: Align, map-table-text

Inline

union ↑ Types

A piece of text in a paragraph, a heading, a cell or a caption.

Str
Plain text.
Emph
Emphasis.
Strong
Strong emphasis.
Strike
Struck-through text.
Superscript
Superscript.
Subscript
Subscript.
Checkbox
A checkbox. The bool is #t when the box is checked.
Code
Code, as it was written.
Link
A link: the target, and the text that the link shows.
Image
An image: the source, and the alternative text.
LineBreak
A line break.
NoteRef
A reference to a footnote. The number counts from 1 into the notes of the document.
Span
Inlines with a class, for a converter that knows the class.
RawInline
Text for one format: the format, such as "html", and the text.
Anchor
A target for a link that is not a heading. It holds an id, which is unique in the document. It shows nothing.

See also: Block, inlines->text, inlines->sexp

Block

union ↑ Types

A block of a document: a paragraph, a heading, a list, a table, and the other kinds.

Para
A paragraph.
Heading
A heading: the level, the id, and the title. Level 1 is a section. The id is unique in the document.
UnnumberedHeading
A heading with no id: the level and the title. It is not in a table of contents.
CodeBlock
A block of code: the language, and the code. The language is "" when none was given.
Figure
A figure: the source of the image, the alternative text, and the caption. The alternative text is "" and the caption is empty when they were not given.
Quote
A quotation, which holds blocks.
ItemList
A list: whether it is numbered, the number of the first item, and the items. Each item is a vec of blocks.
LineBlock
Lines whose breaks are kept, such as verse or an address. Each line is a vec of inlines.
DefinitionList
A definition list. Each entry is a term and its definitions. Each definition is a vec of blocks.
Rule
A horizontal rule.
Div
Blocks with a class, for a converter that knows the class.
RawBlock
A block for one format: the format, such as "html", and the text.
TableBlock
A table.

See also: Inline, Table, Document, blocks->sexp

Document

record ↑ Types

A document after its commands ran: the meta form, the blocks, the footnotes and the source files.

meta
The (meta ...) form as bjodat read it, or None when the document has no meta form.
blocks
The content of the document.
notes
The footnotes, in the order of their numbers. A NoteRef of 1 refers to the first.
sources
The files that the document was made from, in the order they were read. The file of the document is first, when it was read from a file. The files that commands read come after it, such as an @include, an @include-markdown or the file of a @codeblock.

A program that keeps an output can compare it with sources. When one of these files changes, the output is stale.

samizdat gives the meta form no meaning. To read it into a record, use def/bjodat-type of (text bjodat) with #:tag meta.

See also: meta-values, meta-string, meta-strings, document-headings, document->sexp

Functions

meta-values

function ↑ Functions

(: meta-values (-> Document string (Option (List Bjodat))))
(meta-values doc name)

The values of the first clause with a given name in the meta form.

doc
The document.
name
The name of the clause, such as "tags".
returns
The values after the name, or None when the document has no meta form or no clause with that name.
;; (meta (title "Cookies") (tags web http))
(meta-values doc "tags")    ; (Some ((BjoSym web) (BjoSym http)))
(meta-values doc "date")    ; None

A clause is a list whose head is a symbol. Other items in the meta form are ignored.

See also: meta-string, meta-strings, Document

meta-string

function ↑ Functions

(: meta-string (-> Document string (Option string)))
(meta-string doc name)

The one value of a clause in the meta form, as text.

doc
The document.
name
The name of the clause, such as "title".
returns
The value as text, or None when there is no such clause, when the clause has more or fewer than one value, or when the value is not a string or a symbol.
;; (meta (title "Cookies, carefully"))
(meta-string doc "title")   ; (Some "Cookies, carefully")

See also: meta-strings, meta-values

meta-strings

function ↑ Functions

(: meta-strings (-> Document string (Vec string)))
(meta-strings doc name)

The values of a clause in the meta form that are strings or symbols, as text.

doc
The document.
name
The name of the clause, such as "tags".
returns
The values as text, in order. Values of other kinds are left out. It is empty when there is no such clause.
;; (meta (tags samizdat bjolang))
(meta-strings doc "tags")   ; ["samizdat" "bjolang"]

See also: meta-string, meta-values

inlines->text

function ↑ Functions

(: inlines->text (-> (Vec Inline) string))
(inlines->text inlines)

The text of some inlines, without their markup.

inlines
The inlines.
returns
The text. An image gives its alternative text, and a line break gives a space. A checkbox, a footnote reference, raw text and an anchor give nothing.

Use it for text that cannot hold markup: an id, an alternative text, or the title of a page.

See also: Inline

document-headings

function ↑ Functions

(: document-headings (-> Document (Vec (Tuple int string (Vec Inline)))))
(document-headings doc)

Every heading of a document, in order: its level, its id and its title.

doc
The document.
returns
One tuple for each Heading, in document order.

It looks in quotations, lists, definition lists and divs too. It does not look in the footnotes. An UnnumberedHeading has no id, so it is not in the result. @toc and @ref use this function.

See also: number-headings, Block

number-headings

function ↑ Functions

(: number-headings (-> (Vec Block) int (Vec Block)))
(number-headings bs depth)

Puts a number in front of the title of each heading down to a level: 1, 1.1, 2.

bs
The blocks.
depth
The deepest level to number. A heading at a deeper level gets no number.
returns
The blocks, with each numbered heading changed.
(number-headings blocks 2)
;; (Heading 1 "reading" [(Str "Reading")]) becomes
;; (Heading 1 "reading" [(Span "heading-number" [(Str "1")]) (Str " ") (Str "Reading")])

The headings are numbered in the order of document-headings. The number is a Span of class "heading-number" and a space, at the start of the title. So every converter shows the number, and so do a table of contents and a reference made from the title later.

Numbering starts at the shallowest heading that is numbered. A level that is skipped is 0: a subsection before the first section is 0.1. An UnnumberedHeading is not changed. The ids do not change.

@numbered-headings uses this function, through number-headings! of (samizdat expand).

See also: numbered-title?, document-headings

numbered-title?

function ↑ Functions

(: numbered-title? (-> (Vec Inline) bool))
(numbered-title? xs)

Whether the title of a heading starts with the number that number-headings gave it.

xs
The title.
returns
#t when the first inline is a Span of class "heading-number".

See also: number-headings

map-table-text

function ↑ Functions

(: map-table-text (-> (-> (Vec Inline) (Vec Inline)) Table Table))
(map-table-text f t)

A table with a function applied to its caption and to each cell.

f
The function to apply.
t
The table.
returns
A new table with the same alignments and the same shape.

Use it in a pass over the text of a document that must also change the text in tables.

See also: Table

document->sexp

function ↑ Functions

(: document->sexp (-> Document string))
(document->sexp doc)

A document as s-expressions: one block on each line, then the footnotes.

doc
The document.
returns
The text. The footnotes are one (notes (note 1 ...) ...) form on the last line. There is no such line when the document has no footnotes.
(document->sexp doc)
;; (heading 1 "the-defaults" "The defaults")
;; (para "See " (em "this") "." (note-ref 1))
;; (notes (note 1 (para "A note.")))

bjo sz tree prints a document with this function. The tests compare trees as this text. The meta form and the sources are not in the text.

See also: blocks->sexp, inlines->sexp

blocks->sexp

function ↑ Functions

(: blocks->sexp (-> (Vec Block) string))
(blocks->sexp blocks)

Blocks as s-expressions, separated by spaces.

blocks
The blocks.
returns
The text: (para ...), (heading 1 "id" ...), (bullets (item ...)), and so on.

See also: document->sexp, inlines->sexp

inlines->sexp

function ↑ Functions

(: inlines->sexp (-> (Vec Inline) string))
(inlines->sexp inlines)

Inlines as s-expressions, separated by spaces.

inlines
The inlines.
returns
The text. Plain text is a string literal, and the other kinds are forms such as (em ...) and (link "url" ...).

See also: document->sexp, blocks->sexp