Overview
A samizdat Document as HTML: (text xml) nodes, and an HTML writer for them.
(import (samizdat expand))
(import (samizdat commands))
(import (samizdat html))
(match (read-document-file "posts/cookies.sz" standard-commands)
((Ok doc) (file-write-text "cookies.html" (html-page doc)))
((Err e) (println e)))
The converter makes the document into (text xml) nodes, and the
writer makes the nodes into HTML text. A template can put the nodes of
a document in a page of its own nodes, and write the result with
nodes->html.
document->nodes, document->html | the blocks, then the footnotes |
html-page | a whole page around the document |
blocks->nodes, inlines->nodes | part of a document, for a hook |
nodes->html | any nodes, as HTML text |
html-element, raw-html, raw-html-block | nodes for a hook |
The module re-exports Node and xml-text of
(text xml). A hook or a template does not need to import
(text xml), whose Document is not the Document of
samizdat.
What the converter makes.
- A heading is
<h2 id="...">for a section, with the default heading offset. - A code block is
<pre><code class="language-scheme">. highlight.js and Prism use this class. A highlighter can replace the<pre>. - A
Spanor aDivis a<span>or a<div>with the class as itsclass. - A footnote reference is
<sup class="note-ref">with a link. The notes are a<section class="notes">at the end. Each note has a link back. - Struck text is
<del>. A checkbox is a disabled<input type="checkbox">. - A table has its caption, a
<thead>, and a<tbody>for each group. An aligned column hasstyle="text-align: ..."on its cells. - A list item that is one paragraph, perhaps with lists after it, has no
<p>. A definition is written the same way. - Raw text for the format
"html"is written as it is. Raw text for any other format is left out.
The writer writes HTML, not XML:
- A void element has no end tag:
<br>, not<br />. - An empty element that is not void has an end tag:
<td></td>. - A boolean attribute whose value is empty or its own name is written bare:
(Tuple "open" "open")isopen. - An element with block elements among its children puts each block on a line of its own. The writer does not change text, and does not change anything inside a
<pre>. - The text of a
<script>or a<style>is not escaped. - A comment is written. A processing instruction is left out.
See also: document->html, html-page, html-options, document->nodes, nodes->html
Reference
Types
Html
record ↑ Types
The options of the HTML converter, which html-options makes.
A hook gets an Html as its first argument. Give it to
blocks->nodes or inlines->nodes to convert what is inside
the block or the inline with the same options.
See also: html-options, default-html
Functions
html-options
function ↑ Functions
(: html-options (-> (#:heading-offset int) (#:id-prefix string) (#:block-hook (-> Html Block (Option (List Node)))) (#:inline-hook (-> Html Inline (Option (List Node)))) (#:url-hook (-> string string)) (#:highlighter (Option (-> string string (Option Node)))) Html))
(html-options #:heading-offset 1 #:id-prefix ""
#:block-hook no-block-hook #:inline-hook no-inline-hook
#:url-hook (fun (u) u) #:highlighter None)
Makes the options of the HTML converter.
#:heading-offset- Added to the level of each heading. With 1, a section is an
<h2>, below the<h1>of the page. The level is kept between 1 and 6. #:id-prefix- Put in front of each id that the converter writes, and in front of each link to an id. Use it for two documents on one page, whose ids are otherwise the same.
#:block-hook- Called on each block before the converter. It returns
Nonefor a block that the converter writes, or the nodes for the block. #:inline-hook- Called on each inline before the converter. It returns
Nonefor an inline that the converter writes, or the nodes for the inline. #:url-hook- Changes the target of each link and image. A link whose target starts with
#does not go through it. #:highlighter- A function of the language and the text of a code block. It returns the node to write, usually a
<pre>, orNonefor the plain code block. - returns
- The options.
(defun (asides h b)
(match b
((Div "aside" bs) (Some (list (html-element "aside" Nil (blocks->nodes h bs)))))
(_ None)))
(document->html doc #:with (html-options #:block-hook asides #:id-prefix "post-1-"))
A hook is called first for each block or inline, also inside other
blocks and inlines. When it returns nodes, the converter does not
convert the contents. The hook converts them with blocks->nodes
or inlines->nodes, if it needs them.
samizdat has no highlighter. The package textmate-bjolang has one:
(def code-colours (highlighter #:theme 'light-plus))
(html-options #:highlighter
(Some (fun (lang code) (Some (code->node code-colours lang code #:style 'classes)))))
See also: Html, default-html, document->html
document->nodes
function ↑ Functions
(: document->nodes (-> Document (#:with Html) (List Node)))
(document->nodes doc #:with default-html)
A document as nodes: its blocks, then its footnotes.
doc- The document.
#:with- The options.
- returns
- The nodes for the
<body>of a page. When the document has notes, the last node is a<section class="notes">.
(document->nodes doc)
;; ((h2 (@ (id "the-defaults")) "The defaults")
;; (p "Setting a cookie is one header, but getting it " (em "right") " is the hard part."
;; (sup (@ (class "note-ref")) (a (@ (id "note-ref-1") (href "#note-1")) "1")))
;; ...)
See also: document->html, html-page, nodes->html
blocks->nodes
function ↑ Functions
(: blocks->nodes (-> Html (Vec Block) (List Node)))
(blocks->nodes h bs)
Blocks as nodes, for a hook or a template.
h- The options.
bs- The blocks.
- returns
- The nodes, in the order of the blocks.
See also: inlines->nodes, blocks->html, html-options
inlines->nodes
function ↑ Functions
(: inlines->nodes (-> Html (Vec Inline) (List Node)))
(inlines->nodes h xs)
Inlines as nodes, for a hook or a template.
h- The options.
xs- The inlines.
- returns
- The nodes, in the order of the inlines.
See also: blocks->nodes, inlines->html, html-options
document->html
function ↑ Functions
(: document->html (-> Document (#:with Html) string))
(document->html doc #:with default-html)
A document as HTML text, without the page around it.
doc- The document.
#:with- The options.
- returns
- The HTML of the blocks, then of the footnotes. It has no newline at the end.
See also: document->nodes, html-page
blocks->html
function ↑ Functions
(: blocks->html (-> Html (Vec Block) string))
(blocks->html h bs)
Blocks as HTML text.
h- The options.
bs- The blocks.
- returns
- The HTML.
See also: blocks->nodes, inlines->html
inlines->html
function ↑ Functions
(: inlines->html (-> Html (Vec Inline) string))
(inlines->html h xs)
Inlines as HTML text.
h- The options.
xs- The inlines.
- returns
- The HTML.
See also: inlines->nodes, blocks->html
html-page
function ↑ Functions
(: html-page (-> Document (#:with Html) (#:title string) (#:head string) (#:lang string) string))
(html-page doc #:with default-html #:title "" #:head "" #:lang "en")
A whole HTML page with a document as its body.
doc- The document.
#:with- The options.
#:title- The text of the
<title>. When it is empty, the page uses the(title ...)of the meta form, if there is one. #:head- HTML that is put in the
<head>as it is, after the<title>: a stylesheet link, for example. #:lang- The
langattribute of<html>. - returns
- The page. It starts with
<!DOCTYPE html>and ends with a newline.
(html-page doc #:head "<link rel=\"stylesheet\" href=\"/style.css\">")
The <head> has a <meta charset="utf-8">, a viewport
<meta>, the <title>, and then #:head. The page
does not add the title as a heading in the <body>.
See also: document->html, document->nodes
nodes->html
function ↑ Functions
(: nodes->html (-> (List Node) string))
(nodes->html nodes)
Nodes as HTML text.
nodes- The nodes.
- returns
- The HTML, without a newline at the end.
The writer writes HTML, not XML. See the module’s doc for its rules. An element that is in no namespace is written by its local name.
See also: document->nodes, html-element, raw-html
html-element
function ↑ Functions
(: html-element (-> string (List (Tuple string string)) (List Node) Node))
(html-element name attrs children)
An element, with its name and its attributes as strings.
name- The name of the element.
attrs- The attributes, as pairs of a name and a value. For a boolean attribute, give its name as its value:
(Tuple "open" "open"). children- The children.
- returns
- The element.
- raises
System.ArgumentException - When the name of the element or of an attribute is not a valid XML local name, such as "a:b" or "".
(html-element "a" (list (Tuple "href" "/")) (list (xml-text "Home")))
See also: raw-html, blocks->nodes, inlines->nodes
raw-html
function ↑ Functions
(: raw-html (-> string Node))
(raw-html markup)
HTML markup that the writer writes as it is, among text.
markup- The markup. Nothing checks it.
- returns
- A node that holds the markup.
The node is an element in a namespace that only this writer knows. A function that iterates over the nodes cannot see the markup as elements.
See also: raw-html-block, nodes->html
raw-html-block
function ↑ Functions
(: raw-html-block (-> string Node))
(raw-html-block markup)
HTML markup that the writer writes as it is, as a block on a line of its own.
markup- The markup. Nothing checks it.
- returns
- A node that holds the markup.
See also: raw-html, nodes->html
escape-html
function ↑ Functions
(: escape-html (-> string string))
(escape-html s)
Text, with &, < and > escaped as HTML entities.
s- The text.
- returns
- The escaped text.
See also: escape-attribute
escape-attribute
function ↑ Functions
(: escape-attribute (-> string string))
(escape-attribute s)
Text, escaped for an attribute value in double quotes.
s- The text.
- returns
- The text with &, <, > and " escaped as HTML entities.
See also: escape-html
Values
default-html
value ↑ Values
(: default-html Html)
The options of the HTML converter with each option at its default: (html-options).
See also: html-options