(samizdat gfm)

Reference

Overview

A samizdat Document as GitHub Flavored Markdown, for a README or the docs of a repository.

(import (samizdat expand))
(import (samizdat commands))
(import (samizdat gfm))

(match (read-document-file "README.sz" standard-commands)
  ((Ok doc) (file-write-text "README.md" (markdown-page doc)))
  ((Err e) (println e)))
document->markdownthe blocks, then the footnotes
markdown-pagethe same under a # heading, with a newline at the end: a file
markdown-optionsthe options, for #:with
blocks->markdown, inlines->markdownpart of a document, for a hook
escape-markdowntext, escaped to show as it is
github-slugthe id that GitHub gives a heading

What the converter makes. What Markdown has is written as Markdown: emphasis, code, links, images, lists, task lists, quotations, fenced code, tables and footnotes. What Markdown does not have is written as HTML, which GitHub shows:

Escaping. Text is escaped only where Markdown would read it as markup. So the file is easy to read as text too. Emphasis is * and ** where Markdown reads them back as the same emphasis. Elsewhere it is <em> or <strong>: for example, emphasis with a letter outside it and a quotation mark inside it. Space at the edges of emphasis goes outside it.

Links to headings. GitHub makes the id of a heading from its text, and samizdat makes ids in another way. With the default anchors, a link to a heading of the document, from @ref, @toc or @docref, goes to the id of GitHub: @section{C# & .NET} is #c--net, and the second Usage is #usage-1. With #:anchors 'html, each heading starts with an <a id> that has the id of samizdat, and links keep that id.

See also: markdown-page, document->markdown, markdown-options, MdAnchors

Reference

Types

Functions

Values

Types

Md

record ↑ Types

The options of the Markdown converter, which markdown-options makes.

A hook gets an Md as its first argument. Give it to blocks->markdown or inlines->markdown to convert what is inside the block or the inline with the same options. The Md that a hook gets also knows the ids of GitHub for the headings of the document.

See also: markdown-options, default-markdown

MdAnchors

union ↑ Types

How a heading of the Markdown gets its id.

AnchorsGithub
The id that GitHub makes from the text of the heading. A link to the heading is changed to that id. Written 'github.
AnchorsHtml
The id of samizdat, in an <a id> at the start of the heading. Written 'html.

MdAnchors is a grammar (see def/grammar). A keyword of this type takes the quoted symbol, #:anchors 'html, or the case, #:anchors AnchorsHtml.

See also: markdown-options, github-slug

Functions

markdown-options

function ↑ Functions

(: markdown-options (-> (#:heading-offset int) (#:anchors MdAnchors) (#:block-hook (-> Md Block (Option string))) (#:inline-hook (-> Md Inline (Option string))) (#:url-hook (-> string string)) Md))
(markdown-options #:heading-offset 1 #:anchors 'github
                         #:block-hook no-block-hook #:inline-hook no-inline-hook
                         #:url-hook same-url)

Makes the options of the Markdown converter.

#:heading-offset
Added to the level of each heading. With 1, a section is ##, below the # title of the page. The level is kept between 1 and 6.
#:anchors
How a heading gets its id.
#:block-hook
Called on each block before the converter. It returns None for a block that the converter writes, or the Markdown text for the block.
#:inline-hook
Called on each inline before the converter. It returns None for an inline that the converter writes, or the Markdown text for the inline.
#:url-hook
Changes the target of each link and image: for example, reference.html to reference.md. A link to a heading gets the id of GitHub after the hook.
returns
The options.
;; An aside is a quotation: Markdown has nothing nearer.
(defun (asides h b)
  (match b
    ((Div "aside" bs) (Some (blocks->markdown h [(Quote bs)])))
    (_ None)))

(markdown-page doc #:with (markdown-options #:block-hook asides))

A hook is called first for each block or inline, also inside other blocks and inlines. When it returns text, the converter does not convert the contents. The text that a hook returns is not escaped.

See also: Md, MdAnchors, default-markdown, markdown-page

document->markdown

function ↑ Functions

(: document->markdown (-> Document (#:with Md) string))
(document->markdown doc #:with default-markdown)

A document as Markdown: its blocks, then its footnotes.

doc
The document.
#:with
The options.
returns
The Markdown, without a newline at the end. The footnotes are [^1]: and so on, after the blocks.

See also: markdown-page, blocks->markdown

markdown-page

function ↑ Functions

(: markdown-page (-> Document (#:with Md) (#:title string) string))
(markdown-page doc #:with default-markdown #:title "")

A document as a Markdown file, under a # heading with its title.

doc
The document.
#:with
The options.
#:title
The title. When it is empty, the page uses the (title ...) of the meta form. When neither is there, the page has no title heading.
returns
The title as a # heading, a blank line, the document, and a newline at the end.

The title heading is always level 1, whatever #:heading-offset is. GitHub counts it when it makes the ids of the headings, so a section with the same text as the title gets the id with -1.

See also: document->markdown, markdown-options

blocks->markdown

function ↑ Functions

(: blocks->markdown (-> Md (Vec Block) string))
(blocks->markdown h bs)

Blocks as Markdown, for a hook.

h
The options. Give the ones the hook gets, so that links to headings get the ids of GitHub.
bs
The blocks.
returns
The Markdown of the blocks, with a blank line between two blocks.

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

inlines->markdown

function ↑ Functions

(: inlines->markdown (-> Md (Vec Inline) string))
(inlines->markdown h xs)

Inlines as Markdown, for a hook.

h
The options. Give the ones the hook gets, so that links to headings get the ids of GitHub.
xs
The inlines.
returns
The Markdown of the inlines, escaped as at the middle of a line.

See also: blocks->markdown, escape-markdown

escape-markdown

function ↑ Functions

(: escape-markdown (-> string string))
(escape-markdown s)

Text, escaped so that Markdown shows it as it is, within a line.

s
The text.
returns
The text, with a backslash before each character that Markdown would read as markup there.
(escape-markdown "a *b* [c]")   ; "a \\*b\\* \\[c\\]"
(escape-markdown "snake_case")  ; "snake_case"

It escapes \ ` * [ ] < ~ $, an _ that is not between two letters or digits, and an & that starts an entity. It does not escape what is markup only at the start of a line, such as # or -.

See also: inlines->markdown

github-slug

function ↑ Functions

(: github-slug (-> string string))
(github-slug text)

The id that GitHub gives a heading with this text.

text
The text of the heading, without markup.
returns
The text in lower case, with a dash for each space, and without each character that is not a letter, a digit, a mark, - or _.
(github-slug "C# & .NET")   ; "c--net"

GitHub adds -1, -2 and so on to a second and a third heading with the same id. This function does not.

See also: MdAnchors

Values

default-markdown

value ↑ Values

(: default-markdown Md)

The options of the Markdown converter with each option at its default: (markdown-options).

See also: markdown-options