Overview
Runs the commands of a document, and gives the functions that a command is written with.
(import (samizdat read))
(import (samizdat doc))
(import (samizdat expand))
(import (samizdat commands))
;; @aside{...}: paragraphs set apart.
(: aside Command)
(defun (aside cx c)
(check-keywords! cx c (vec-empty))
(block (Div "aside" (expand-blocks cx c))))
;; @version["1.2"], or @version["1.2" #:prefix "r"]
(: version Command)
(defun (version cx c)
(check-keywords! cx c ["prefix"])
(inline (Code (str (kw-string-or cx c "prefix" "v") (arg-string cx c 0)))))
(def commands
(map-merge standard-commands
#map(("aside" aside)
("version" version)
("bjolang" (text-command "Bjolang")))))
(match (read-document-file "post.sz" commands)
((Ok doc) ...)
((Err why) (println why))) ; "post.sz:14:3: @version: needs 1 argument in its [...]"
A command is a function of type Command. It gets the state of
the document, a Ctx, and the call as the reader read it. The
arguments and the body are not expanded. The command decides what to
do with them. It returns pieces: inlines and blocks, in any number.
A table of commands is a (Map string Command). The key is the
name of the command, without the @. map-merge with
standard-commands of (samizdat commands) adds commands of
your own. The second map wins, so a command of yours replaces a
standard command with the same name.
Paragraphs. Text between blocks is collected into paragraphs. A blank line ends a paragraph, and so does a block. Whitespace at the start and the end of a paragraph is removed.
Errors. A command that cannot use its call calls
refuse. Then the expansion stops, and expand returns
Err with a message that starts with the file, the line and the
column of the call. Many functions here call refuse for a call
that is not correct. An exception of another kind from a command is not
caught: it is raised from expand.
Commands can call commands to a depth of 200. A call deeper than that is rejected.
Typography. After the commands ran, smarten changes
dashes, ellipses and quotes in the text. #:typography #f stops
this.
See also: read-document, read-document-file, expand, Command, Ctx, refuse, inline, block
Reference
add-note!add-source!arg-countarg-stringarg-string-orblockbody-itemsbody-textcheck-keywords!ctx-sourcedefer-blocksdefer-inlinesexpandexpand-blocksexpand-inlinesexpand-itemshas-body?include-fileinlineitems->blocksitems->inlineskeywordkw-bool-orkw-int-orkw-string-orkw-symbol-ornumber-headings!positionalread-documentread-document-fileread-relativerefuserelative-pathsmartenstate-refstate-set!text-commandunique-id!
Types
Piece
union ↑ Types
What a command makes: an inline or a block.
PInline- An inline. Adjacent inlines are collected into paragraphs.
PBlock- A block. It ends the paragraph before it.
Command
type alias ↑ Types
A command: a function of the state of the document and a call, which returns pieces.
(: kbd Command)
(defun (kbd cx c) (inline (Span "kbd" [(Str (body-text cx c))])))
See also: Ctx, Piece, text-command, expand
Ctx
record ↑ Types
The state of a document while its commands run.
It holds the table of commands, the file that is expanded, the
footnotes, the heading ids, the deferred commands, the source files and
the state that commands keep. A command changes it only through the
functions of this module. One Ctx is used for the whole
expansion, included files too.
See also: Command, ctx-source, state-ref, state-set!, add-note!, unique-id!
Functions
expand
function ↑ Functions
(: expand (-> Source (Map string Command) (#:name string) (#:typography bool) (Result string Document)))
(expand src commands #:name "input" #:typography #t)
Runs the commands of a document that the reader read, and returns the Document.
src- The document, from
read-source. commands- The table of commands.
#:name- The name of the document in an error message. Paths in the document are relative to its directory.
#:typography- When
#f, dashes, ellipses and quotes stay as they were typed. - returns
- The document, or
Errwith the message of the firstrefuse.
The steps are:
- Each command runs, in document order. Text is collected into paragraphs.
- When a command called
number-headings!, the headings are numbered. - Each deferred function runs with the document, and its result replaces its placeholder.
smartenchanges the text of the blocks and of the footnotes, unless#:typographyis#f.
An exception that is not from refuse is raised, not returned.
See also: read-document, read-document-file, refuse, smarten
read-document
function ↑ Functions
(: read-document (-> string (Map string Command) (#:name string) (#:typography bool) (Result string Document)))
(read-document text commands #:name "input" #:typography #t)
Reads the text of a document and runs its commands.
text- The text of the document.
commands- The table of commands.
#:name- The name of the document in an error message. Paths in the document are relative to its directory.
#:typography- When
#f, dashes, ellipses and quotes stay as they were typed. - returns
- The document, or
Errwith a message when the text cannot be read or a command rejects its call.
(read-document "Some @em{text}." standard-commands #:name "note.sz")
It is read-source, then expand. With the default name, the
paths in the document are relative to the current directory.
See also: read-document-file, expand
read-document-file
function ↑ Functions
(: read-document-file (-> string (Map string Command) (#:typography bool) (Result string Document)))
(read-document-file path commands #:typography #t)
Reads a document from a file and runs its commands.
path- The path of the file. Error messages use it, and paths in the document are relative to its directory.
commands- The table of commands.
#:typography- When
#f, dashes, ellipses and quotes stay as they were typed. - returns
- The document, or
Errwith a message when the text cannot be read or a command rejects its call. Thesourcesof the document start withpath. - raises
System.IO.IOException - When the file cannot be opened, for example when it does not exist.
(match (read-document-file "posts/cookies.sz" standard-commands)
((Ok doc) (file-write-text "cookies.html" (html-page doc)))
((Err e) (println e)))
See also: read-document, expand
inline
function ↑ Functions
(: inline (-> Inline (Vec Piece)))
(inline x)
The result of a command that makes one inline.
x- The inline.
- returns
- A vec of one
PInline.
(inline (Code "x"))
block
function ↑ Functions
(: block (-> Block (Vec Piece)))
(block x)
The result of a command that makes one block.
x- The block.
- returns
- A vec of one
PBlock.
(block (Div "aside" (expand-blocks cx c)))
has-body?
function ↑ Functions
(: has-body? (-> Call bool))
(has-body? c)
Whether the call was written with a body, also an empty one.
c- The call.
- returns
#tfor@name{}and@name|{}|, and#ffor@nameand@name[...].
See also: body-items
body-items
function ↑ Functions
(: body-items (-> Call (Vec At)))
(body-items c)
The body of a call as the reader read it.
c- The call.
- returns
- The items of the body. It is empty when the call has no body.
Use it for a command that reads the structure of its body. For
example, @itemlist iterates over the items and expands each
@item.
See also: has-body?, items->blocks, items->inlines, at-lines
body-text
function ↑ Functions
(: body-text (-> Ctx Call string))
(body-text cx c)
The body of a call as text, as it was written.
cx- The state of the document.
c- The call.
- returns
- The text of the body. A command in the body runs, and the text of its inlines is put in its place. It is
""when the call has no body.
@code and @codeblock use it. A command in the body that
makes a block is rejected with refuse.
See also: expand-inlines, inlines->text
expand-inlines
function ↑ Functions
(: expand-inlines (-> Ctx Call (Vec Inline)))
(expand-inlines cx c)
The body of a call, expanded as inlines.
cx- The state of the document.
c- The call.
- returns
- The inlines. Adjacent text is joined. It is empty when the call has no body.
A block in the body is rejected with refuse. Use it for a body
that is text, such as the body of @em.
See also: expand-blocks, items->inlines, body-text
expand-blocks
function ↑ Functions
(: expand-blocks (-> Ctx Call (Vec Block)))
(expand-blocks cx c)
The body of a call, expanded as paragraphs and blocks.
cx- The state of the document.
c- The call.
- returns
- The blocks. Text is collected into paragraphs. It is empty when the call has no body.
Use it for a body that can hold paragraphs, such as the body of
@note or @blockquote.
See also: expand-inlines, items->blocks
expand-items
function ↑ Functions
(: expand-items (-> Ctx (Vec At) (Vec Piece)))
(expand-items cx items)
Items expanded into pieces, without paragraphs.
cx- The state of the document.
items- The items.
- returns
- The pieces, in order. Text is a
PInlineof aStr, and each command gives its pieces.
See also: items->blocks, items->inlines
items->blocks
function ↑ Functions
(: items->blocks (-> Ctx (Vec At) (Vec Block)))
(items->blocks cx items)
Items expanded as paragraphs and blocks.
cx- The state of the document.
items- The items, such as a part of a body that a command took apart.
- returns
- The blocks. Text is collected into paragraphs.
See also: expand-blocks, items->inlines, body-items
items->inlines
function ↑ Functions
(: items->inlines (-> Ctx Call (Vec At) (Vec Inline)))
(items->inlines cx c items)
Items expanded as inlines.
cx- The state of the document.
c- The call that an error message names.
items- The items, such as a part of a body that a command took apart.
- returns
- The inlines. Adjacent text is joined.
A block among the items is rejected with refuse, for the call c.
See also: expand-inlines, items->blocks
defer-blocks
function ↑ Functions
(: defer-blocks (-> Ctx (-> Document (Vec Block)) (Vec Piece)))
(defer-blocks cx make)
The result of a command whose blocks are made from the finished document.
cx- The state of the document.
make- The function that makes the blocks.
- returns
- A placeholder block. After every command ran, it is replaced by what
makereturns.
;; A list of the headings, also those after the command.
(defer-blocks cx (fun (doc)
[(ItemList #f 1 (vec-map (fun (h) (match h ((Tuple level id xs) [(Para xs)])))
(document-headings doc)))]))
Use it for a command that needs what comes after it, or what is in
included files. @toc is an example.
makeis called once, after every command ran and the headings were numbered.- The document that
makegets has the other placeholders in it, not their content. - What
makereturns can hold placeholders too. They are replaced in turn. A placeholder in its own result is removed. - A
refuseinmakenames the file that the command was written in.
See also: defer-inlines, document-headings
defer-inlines
function ↑ Functions
(: defer-inlines (-> Ctx (-> Document (Vec Inline)) (Vec Piece)))
(defer-inlines cx make)
The result of a command whose inlines are made from the finished document.
cx- The state of the document.
make- The function that makes the inlines.
- returns
- A placeholder inline. After every command ran, it is replaced by what
makereturns.
It is defer-blocks for text. @ref is an example: the
heading that it links to can come after it.
See also: defer-blocks
number-headings!
function ↑ Functions
(: number-headings! (-> Ctx Call int void))
(number-headings! cx c depth)
Asks for the headings of the document to be numbered after every command ran.
cx- The state of the document.
c- The call, which an error message names.
depth- The deepest level to number.
The numbering is done with number-headings of
(samizdat doc), before the deferred functions run. So a table of
contents and a reference show the numbers. @numbered-headings
uses it.
A document numbers its headings once. A second call is rejected with
refuse.
See also: number-headings, defer-blocks
relative-path
function ↑ Functions
(: relative-path (-> Ctx string string))
(relative-path cx path)
A path as a document writes it, relative to the directory of the file that is expanded.
cx- The state of the document.
path- The path as written.
- returns
- The path joined to the directory of the file. It is
pathunchanged when the name of the file has no directory.
In an included file, the path is relative to that file. A document read from a string with the default name is in the current directory.
See also: read-relative, include-file, ctx-source
read-relative
function ↑ Functions
(: read-relative (-> Ctx Call string string))
(read-relative cx c path)
The text of a file, relative to the file that is expanded.
cx- The state of the document.
c- The call, which an error message names.
path- The path as the document writes it.
- returns
- The text of the file.
The file is added to the sources of the document. A file that
does not exist is rejected with refuse.
See also: relative-path, add-source!, include-file
add-source!
function ↑ Functions
(: add-source! (-> Ctx string void))
(add-source! cx file)
Adds a file to the sources of the document.
cx- The state of the document.
file- The path of the file.
Call it for a file that a command reads in a different way than with
read-relative. Then a program that keeps the output knows that
the output changes when the file changes. A file that is in the
sources already is not added again.
See also: read-relative, Document
include-file
function ↑ Functions
(: include-file (-> Ctx Call string (Vec Piece)))
(include-file cx c path)
Another document, expanded where the command is, as if its text were written there.
cx- The state of the document.
c- The call, which an error message names.
path- The path of the file, relative to the file that is expanded.
- returns
- The pieces of the included document.
The included document uses the footnotes, the heading ids and the state
of the document. Paths in it are relative to its own file. It is added
to the sources. @include uses it.
These are rejected with refuse:
- a file that does not exist;
- a file that is being included already, directly or through other files. The message shows the chain of files;
- a file that the reader cannot read;
- a file that starts with a
(meta ...)form.
A refuse inside the included file has a second line, which says
where the file was included from.
See also: read-relative, relative-path
positional
function ↑ Functions
(: positional (-> Call (Vec Bjodat)))
(positional c)
The positional arguments of a call: the arguments that are not keywords or values of keywords.
c- The call.
- returns
- The arguments, in order, as bjodat values.
;; @image["cat.png" #:alt "A cat" left]
(positional c) ; [(BjoStr "cat.png") (BjoSym left)]
See also: arg-count, arg-string, keyword
keyword
function ↑ Functions
(: keyword (-> Call string (Option Bjodat)))
(keyword c name)
The value of a keyword argument of a call.
c- The call.
name- The name of the keyword, without
#:. - returns
- The value after the first
#:name, orNonewhen the call does not have it, or when no value comes after it.
See also: kw-string-or, kw-int-or, kw-bool-or, check-keywords!, positional
arg-count
function ↑ Functions
(: arg-count (-> Call int))
(arg-count c)
The number of positional arguments of a call.
c- The call.
- returns
- The number. Keywords and their values are not counted.
See also: positional, arg-string
arg-string
function ↑ Functions
(: arg-string (-> Ctx Call int string))
(arg-string cx c i)
A positional argument that must be there, as text.
cx- The state of the document.
c- The call.
i- The index of the argument, counting from 0.
- returns
- The argument. A string gives its text, and a symbol gives its name.
These are rejected with refuse: a call with fewer than
i + 1 positional arguments, and an argument that is not a string
or a symbol.
See also: arg-string-or, arg-count, positional
arg-string-or
function ↑ Functions
(: arg-string-or (-> Ctx Call int string string))
(arg-string-or cx c i fallback)
A positional argument as text, or a default when the call does not have it.
cx- The state of the document.
c- The call.
i- The index of the argument, counting from 0.
fallback- The result when there is no argument at that index.
- returns
- The argument, or
fallback. A string gives its text, and a symbol gives its name.
An argument that is not a string or a symbol is rejected with refuse.
See also: arg-string
kw-string-or
function ↑ Functions
(: kw-string-or (-> Ctx Call string string string))
(kw-string-or cx c name fallback)
The value of a keyword argument as text, or a default when the call does not have it.
cx- The state of the document.
c- The call.
name- The name of the keyword, without
#:. fallback- The result when the call does not have the keyword.
- returns
- The value, or
fallback. A string gives its text, and a symbol gives its name.
A value that is not a string or a symbol is rejected with refuse.
See also: kw-symbol-or, kw-int-or, kw-bool-or, keyword, check-keywords!
kw-symbol-or
function ↑ Functions
(: kw-symbol-or (-> Ctx Call string string string))
(kw-symbol-or cx c name fallback)
The value of a keyword argument as text, or a default: for a value written as a symbol.
cx- The state of the document.
c- The call.
name- The name of the keyword, without
#:. fallback- The result when the call does not have the keyword.
- returns
- The value, or
fallback.
;; @itemlist[#:style ordered]{...}
(kw-symbol-or cx c "style" "bullets") ; "ordered"
It is the same as kw-string-or: a string and a symbol give the
same text. Use it where the document writes a symbol, so that the code
says so.
See also: kw-string-or
kw-int-or
function ↑ Functions
(: kw-int-or (-> Ctx Call string int int))
(kw-int-or cx c name fallback)
The value of a keyword argument as an int, or a default when the call does not have it.
cx- The state of the document.
c- The call.
name- The name of the keyword, without
#:. fallback- The result when the call does not have the keyword.
- returns
- The value, or
fallback.
A value that is not an integer is rejected with refuse.
See also: kw-string-or, kw-bool-or
kw-bool-or
function ↑ Functions
(: kw-bool-or (-> Ctx Call string bool bool))
(kw-bool-or cx c name fallback)
The value of a keyword argument as a bool, or a default when the call does not have it.
cx- The state of the document.
c- The call.
name- The name of the keyword, without
#:. fallback- The result when the call does not have the keyword.
- returns
- The value, or
fallback.
A value that is not #t or #f is rejected with refuse.
See also: kw-string-or, kw-int-or
check-keywords!
function ↑ Functions
(: check-keywords! (-> Ctx Call (Vec string) void))
(check-keywords! cx c names)
Rejects a keyword argument that a command does not take.
cx- The state of the document.
c- The call.
names- The names of the keywords that the command takes, without
#:.
(check-keywords! cx c ["lang" "file"])
;; @codeblock[#:langauge "x"] is rejected:
;; "post.sz:1:1: @codeblock: takes no #:langauge; it takes #:lang, #:file"
Call it first in a command that takes keywords, or with
(vec-empty) in a command that takes none. Then a misspelt keyword
gives an error and is not ignored. A keyword that is not in
names is rejected with refuse, and so is a keyword with no
value after it.
refuse
function ↑ Functions
(: refuse (-> Ctx Call string %a))
(refuse cx c why)
Stops the expansion with a message that says where the call is.
%a- Any type, so that a call can stand where a value is expected.
cx- The state of the document.
c- The call that is wrong.
why- What is wrong, such as
"needs a body: {...}". - returns
- It does not return.
- raises
System.IO.InvalidDataException - Always. The message is
FILE:LINE:COLUMN: @name: why.
(unless (has-body? c) (refuse cx c "needs a body: {...}"))
;; post.sz:14:3: @aside: needs a body: {...}
expand catches this exception and returns Err with the
message. Do not catch it in a command.
See also: check-keywords!, expand
add-note!
function ↑ Functions
(: add-note! (-> Ctx (-> (Vec Block)) int))
(add-note! cx make)
Adds a footnote to the document and returns its number.
cx- The state of the document.
make- The function that makes the blocks of the footnote.
- returns
- The number of the footnote, counting from 1, for a
NoteRef.
(inline (NoteRef (add-note! cx (fun () (expand-blocks cx c)))))
The number is taken before make is called. So a footnote inside
a footnote gets the next number.
See also: Document
unique-id!
function ↑ Functions
(: unique-id! (-> Ctx string string))
(unique-id! cx text)
An id made from text, which no other call in the document returned.
cx- The state of the document.
text- The text, such as the title of a heading.
- returns
- The id.
"The Defaults"gives"the-defaults", and a second time"the-defaults-2".
Letters and digits are kept, in lower case. Whitespace, - and
_ become one -. Other characters are removed. A text that
gives nothing gives \"section\". Use it for a Heading or
an Anchor.
See also: Inline, Block
ctx-source
function ↑ Functions
(: ctx-source (-> Ctx string))
(ctx-source cx)
The name of the file that is expanded, as an error message gives it.
cx- The state of the document.
- returns
- The name of the document, or the path of the included file that is expanded.
See also: relative-path, refuse
state-ref
function ↑ Functions
(: state-ref (-> Ctx string (Option Bjodat)))
(state-ref cx key)
A value that a command kept for the rest of the document.
cx- The state of the document.
key- The key.
- returns
- The value, or
Nonewhen nothing was kept under the key.
All commands use the same keys. Start a key with the name of the
command, such as \"defmodule:current\". The state is kept for
the whole expansion, included files too.
See also: state-set!
state-set!
function ↑ Functions
(: state-set! (-> Ctx string Bjodat void))
(state-set! cx key value)
Keeps a value for the rest of the document.
cx- The state of the document.
key- The key. Start it with the name of the command.
value- The value. It replaces a value kept under the same key.
See also: state-ref
text-command
function ↑ Functions
(: text-command (-> string Command))
(text-command text)
A command that stands for some text.
text- The text.
- returns
- A command that makes the text as a
Str.
(map-set standard-commands "bjolang" (text-command "Bjolang"))
;; @bjolang is "Bjolang", and @|bjolang|s is "Bjolangs".
A call with a positional argument or a body is rejected with refuse.
See also: Command
smarten
function ↑ Functions
(: smarten (-> (Vec Block) (Vec Block)))
(smarten blocks)
Changes dashes, ellipses and straight quotes in the text of blocks into typographic ones.
blocks- The blocks.
- returns
- The blocks, with the text changed.
---becomes an em dash,--an en dash and...an ellipsis.- A straight quote becomes an opening quote after whitespace, a bracket, a dash or an opening quote. Otherwise it becomes a closing quote.
- The character before an inline is carried across it, so the quote after
@em{word}closes.
It changes the text in paragraphs, headings, quotations, lists, divs
and tables. Code, code blocks and raw text do not change. expand
calls it unless #:typography is #f.
See also: expand