Overview
The high-level interface of samizdat: one Setup for every option, documents to files, and folders to sites.
(import (samizdat author))
(def setup (author-setup #:css ["style.css"]))
(convert-file "posts/cookies.sz" 'html #:setup setup) ; writes posts/cookies.html
(convert-tree "docs" 'html #:out "site" #:setup setup) ; every .sz file below docs
(match (load-document "posts/cookies.sz" #:setup setup)
((Ok doc) (println (render doc 'markdown #:setup setup)))
((Err why) (println why)))
A Setup holds every option: the commands, typography, the
title, the heading offset, the stylesheets, the hooks of each
converter, and a URL hook. Each function takes it as #:setup.
The default is default-setup. bjo sz is written on this
module, and an sz.bjo changes a Setup.
The module re-exports Document, Command and
standard-commands. So an sz.bjo that adds commands can
import this module alone for the common case.
author-setup, default-setup | a Setup. Change a field later with record-set |
load-document, load-document-text | read a document and run its commands |
render | the document as text in an OutputFormat |
convert-file, convert-tree | write the output files |
output-path, output-suffix, sz-files | the names of the files |
document-outline, document-sources | the headings, and the files a document was made from |
document-links, document-ids, check-links | the links, the ids, and the links that go nowhere |
command-names | the names of the commands of a setup |
Errors are values. Each function that reads a document returns
Err when the document cannot be read or a command rejects it.
The message has the form FILE:LINE:COLUMN: @command: why.
No function raises for a bad document. A failure to write a file
raises.
A file is written only when its text changes. So the
modification time of an output changes only for a real change. With
#:check #t, nothing is written, and the result tells whether
the file would change.
A link to a .sz file goes to its output. With the default
sz-links, a relative link or image to a .sz file goes to
the file that the same format makes of it. In HTML,
"guide.sz#install" becomes "guide.html#install", and in
Markdown "guide.md#install". A link with a scheme, such as
https:, or a link that starts with /, does not change.
See also: Setup, author-setup, load-document, render, convert-file, convert-tree, check-links
Reference
Types
Setup
record ↑ Types
Every option of reading and writing a document.
commands- The commands that a document can use.
typography- Whether to make curly quotes, dashes and ellipses of the text.
title- The title of a page or a Markdown file. When it is empty, the
(title ...)of the meta form is used. heading-offset- Added to the level of each heading. With 1, a section is
<h2>or##, below the title. id-prefix- HTML: put in front of each id, and in front of each link to an id.
lang- HTML: the
langattribute of<html>. css- HTML: the URLs of the stylesheets. Each one is a
<link>in the<head>. head- HTML: text that is put in the
<head>as it is, after the stylesheets. highlighter- HTML: a function of the language and the code of a code block, which returns the node to write, or
Nonefor the plain code block. html-block-hook- HTML: called on each block first. It returns
None, or the nodes for the block. html-inline-hook- HTML: called on each inline first. It returns
None, or the nodes for the inline. anchors- Markdown: how a heading gets its id.
md-block-hook- Markdown: called on each block first. It returns
None, or the text for the block. md-inline-hook- Markdown: called on each inline first. It returns
None, or the text for the inline. sz-links- Whether a relative link or image to a
.szfile goes to its output. url-hook- Changes the target of each link and image, in both formats. It is applied after
sz-links.
(record-set default-setup (css ["/style.css"]) (lang "sv"))
The fields are public. Change one with record-set. The HTML
fields are the options of html-options in
(samizdat html), and the Markdown fields are the options of
markdown-options in (samizdat gfm).
See also: author-setup, default-setup
OutputFormat
union ↑ Types
A format to write a document in.
FormatHtml- A whole HTML page. Written
'html. FormatHtmlFragment- The HTML of the body only, without
<html>,<head>and<body>. Written'html-fragment. FormatMarkdown- GitHub Flavored Markdown. Written
'markdown. FormatSexp- The tree of the document as s-expressions, to debug commands. Written
'sexp.
OutputFormat is a grammar (see def/grammar). An argument
of this type takes the quoted symbol, 'html, or the case,
FormatHtml.
See also: render, output-suffix
OutlineEntry
record ↑ Types
A heading of a document.
level- The level: 1 for a section, 2 for a subsection, and so on.
id- The id that
@refand a link with#take. title- The text of the heading, without markup.
See also: document-outline
DocLink
record ↑ Types
A link or an image of a document.
target- Where it goes: the URL of a link, or the source of an image.
text- The text of a link, without markup, or the alt text of an image.
image- Whether it is an image.
See also: document-links, check-links
BrokenLink
record ↑ Types
A link that goes nowhere, and why.
link- The link.
why- Why it goes nowhere, as a message.
See also: check-links
Functions
author-setup
function ↑ Functions
(: author-setup (-> (#:commands (Map string Command)) (#:typography bool) (#:title string) (#:heading-offset int) (#:id-prefix string) (#:lang string) (#:css (Vec string)) (#:head string) (#:highlighter (Option (-> string string (Option Node)))) (#:html-block-hook (-> Html Block (Option (List Node)))) (#:html-inline-hook (-> Html Inline (Option (List Node)))) (#:anchors MdAnchors) (#:md-block-hook (-> Md Block (Option string))) (#:md-inline-hook (-> Md Inline (Option string))) (#:sz-links bool) (#:url-hook (-> string string)) Setup))
(author-setup #:commands standard-commands #:typography #t #:title ""
#:heading-offset 1 #:id-prefix "" #:lang "en" #:css (vec-empty) #:head ""
#:highlighter None
#:html-block-hook (fun (h b) None) #:html-inline-hook (fun (h i) None)
#:anchors 'github
#:md-block-hook (fun (h b) None) #:md-inline-hook (fun (h i) None)
#:sz-links #t #:url-hook (fun (u) u))
Makes a Setup. Each keyword sets the field of the same name.
#:commands- The commands that a document can use.
#:typography- Whether to make curly quotes, dashes and ellipses of the text.
#:title- The title. Empty for the
(title ...)of the meta form. #:heading-offset- Added to the level of each heading.
#:id-prefix- HTML: put in front of each id.
#:lang- HTML: the
langattribute. #:css- HTML: the URLs of the stylesheets.
#:head- HTML: text for the
<head>. #:highlighter- HTML: the highlighter of code blocks.
#:html-block-hook- HTML: the block hook.
#:html-inline-hook- HTML: the inline hook.
#:anchors- Markdown: how a heading gets its id.
#:md-block-hook- Markdown: the block hook.
#:md-inline-hook- Markdown: the inline hook.
#:sz-links- Whether a link to a
.szfile goes to its output. #:url-hook- Changes the target of each link and image.
- returns
- The setup.
(author-setup #:commands (map-merge standard-commands mine)
#:css ["style.css"]
#:anchors 'html)
See Setup for what each field does.
See also: Setup, default-setup
output-suffix
function ↑ Functions
(: output-suffix (-> OutputFormat string))
(output-suffix format)
The suffix of a file in a format.
format- The format.
- returns
".html"for both HTML formats,".md"for Markdown, and".sexp"for s-expressions.
See also: output-path
output-path
function ↑ Functions
(: output-path (-> string OutputFormat string))
(output-path input format)
The name of the file that a format makes of a source file.
input- The path of the source.
format- The format.
- returns
- The path with
.szat its end replaced by the suffix of the format. When the path does not end in.sz, the suffix is added.
(output-path "a/b.sz" 'html) ; "a/b.html"
(output-path "README" 'markdown) ; "README.md"
See also: output-suffix, convert-file
load-document
function ↑ Functions
(: load-document (-> string (#:setup Setup) (Result string Document)))
(load-document path #:setup default-setup)
Reads a document file and runs its commands.
path- The path of the file.
#:setup- The commands and the typography of the setup are used.
- returns
- The document, or
Errwith a message when the file does not exist, cannot be read as a document, or a command rejects it.
A relative @include is read relative to the file.
See also: load-document-text, render
load-document-text
function ↑ Functions
(: load-document-text (-> string (#:name string) (#:setup Setup) (Result string Document)))
(load-document-text text #:name "input" #:setup default-setup)
Reads a string as a document and runs its commands.
text- The text of the document.
#:name- The name that error messages give the document.
#:setup- The commands and the typography of the setup are used.
- returns
- The document, or
Errwith a message when the text cannot be read as a document, or a command rejects it.
A relative @include is read from the current directory.
See also: load-document
render
function ↑ Functions
(: render (-> Document OutputFormat (#:setup Setup) string))
(render doc format #:setup default-setup)
A document as text in a format.
doc- The document.
format- The format.
#:setup- The options of the converter.
- returns
- The text. It ends with a newline.
'html uses each HTML field of the setup. 'html-fragment
does not use title, lang, css and head.
'markdown uses title and the Markdown fields.
'sexp uses no field.
See also: OutputFormat, convert-file, load-document
convert-file
function ↑ Functions
(: convert-file (-> string OutputFormat (#:setup Setup) (#:output string) (#:check bool) (Result string bool)))
(convert-file input format #:setup default-setup #:output "" #:check #f)
Reads a document file and writes it in a format.
input- The path of the source.
format- The format.
#:setup- The options of reading and writing.
#:output- The file to write. When it is empty, the file is
(output-path input format). #:check- When it is
#t, nothing is written. - returns
#twhen the file changed, and#fwhen it already had the same text. With#:check #t, whether the file would change.Errwith the message of the document when it cannot be loaded.
The file is written only when its text changes, so its modification time changes only for a real change. The directory of the file is made when it does not exist. A failure to write the file raises.
See also: convert-tree, output-path, render
sz-files
function ↑ Functions
(: sz-files (-> string (Vec string)))
(sz-files dir)
Every .sz file below a directory, sorted.
dir- The directory.
- returns
- The paths, each starting with
dir.
It does not go into a directory whose name starts with ., such
as .git and .bjo. A file whose name starts with .
is included.
See also: convert-tree
convert-tree
function ↑ Functions
(: convert-tree (-> string OutputFormat (#:setup Setup) (#:out string) (#:check bool) (Vec (Tuple string string (Result string bool)))))
(convert-tree dir format #:setup default-setup #:out "" #:check #f)
Converts every .sz file below a directory, with convert-file.
dir- The directory.
format- The format.
#:setup- The options of reading and writing.
#:out- The directory to write into, with the same relative paths as below
dir. When it is empty, each output is written beside its source. #:check- When it is
#t, nothing is written. - returns
- For each file of
(sz-files dir): the source, the output, and the result ofconvert-file.
(loop (:for (Tuple input target result) (convert-tree "docs" 'html #:out "site"))
(:do (match result
((Err why) (println why))
((Ok #t) (println (str "Wrote " target)))
((Ok #f) (ignore 0)))))
It converts each file, also after an error in another file.
See also: convert-file, sz-files
document-outline
function ↑ Functions
(: document-outline (-> Document (Vec OutlineEntry)))
(document-outline doc)
The headings of a document that have an id, in order.
doc- The document.
- returns
- The headings, also those in quotations, lists and divs. A starred heading, such as
@section*, has no id and is left out. The notes are not included.
See also: OutlineEntry, document-ids
document-sources
function ↑ Functions
(: document-sources (-> Document (Vec string)))
(document-sources doc)
The files that a document was made from.
doc- The document.
- returns
- Its own file first, then each file that a command read, such as an
@include, in order.
See also: load-document
document-links
function ↑ Functions
(: document-links (-> Document (Vec DocLink)))
(document-links doc)
Every link and image of a document, in order.
doc- The document.
- returns
- The links and the images, those in the notes included. A link that
@refor@tocmade goes to an id, such as#install.
The targets are as the document has them, before sz-links and
url-hook change them.
See also: DocLink, check-links, document-ids
document-ids
function ↑ Functions
(: document-ids (-> Document (Vec string)))
(document-ids doc)
The ids in a document that a link with # can go to.
doc- The document.
- returns
- The id of each heading of
document-outline, then the id of each anchor.
See also: document-outline, check-links
check-links
function ↑ Functions
(: check-links (-> string (#:setup Setup) (Result string (Vec BrokenLink))))
(check-links path #:setup default-setup)
The links and images of a document file that go nowhere.
path- The path of the document.
#:setup- The setup to read the document, and each other document it links to.
- returns
- The broken links, in order.
Errwith the message of the document when the document atpathcannot be loaded.
- A link that is
#idalone must go to an id of the document: of a heading or of an anchor. - The file of any other relative link must exist, relative to the directory of
path. Escapes such as%20are decoded first. A link to a directory is not broken. - A link to
FILE.htmlorFILE.mdgoes toFILE.szwhen that file exists. So the outputs do not need to be written yet. - An
#idafter a.szfile must be an id of that document. Each other document is read once. When it has an error, each link with an#idto it is broken, and the error is why. - An
#idafter a file that is not a document is not checked. - A link with a scheme, such as
https:, or a link that starts with/, is not checked.
See also: document-links, document-ids, BrokenLink
command-names
function ↑ Functions
(: command-names (-> Setup (Vec string)))
(command-names setup)
The names of the commands of a setup, sorted.
setup- The setup.
- returns
- The names, without the
@.
See also: Setup
Values
default-setup
value ↑ Values
(: default-setup Setup)
The standard commands, and each other option at its default: (author-setup).
See also: author-setup, Setup, standard-commands