GNU TeXmacsUser manualReferenceAboutEnglish
The Markdown converters

The converters between TeXmacs and Markdown are written in Scheme, in

convert/markdown

, on the model of those for HTML: a parser and a serializer between the text and a tree, and two converters between this tree and the trees of TeXmacs. The user-level description is in converters for Markdown.

1.The formats and the converters

init-markdown.scm

defines the format markdown (suffixes md, markdown, mkd) and its converters; it is loaded by need (lazy-format in

init-texmacs.scm

). The format has no :recognize predicate: a text is never taken for Markdown by its contents.

markdown-document, markdown-snippet \(\rightarrow\) markdown-stree

parse-markdown-document and parse-markdown-snippet, in

markdownin.scm

.

markdown-stree \(\rightarrow\) texmacs-stree

markdown->texmacs, in

markdowntm.scm

.

texmacs-stree \(\rightarrow\) markdown-stree

texmacs->markdown, in

tmmarkdown.scm

, with the options texmacs->markdown:html and texmacs->markdown:front-matter (both on by default; they are preferences, shown in the tab Markdown of the preferences of the converters).

markdown-stree \(\rightarrow\) markdown-document, markdown-snippet

serialize-markdown, in

markdownout.scm

.

2.The Markdown tree

The tree between the two halves is (markdown block …), or (!file (markdown …)) for a whole document. Its strings are in UTF-8, as the text; the two converters to and from TeXmacs change the encoding.

Blocks

(meta (key "value") …) for the header, (h1 …) to (h6 …), (p …), (blockquote block …), (hr), (ul (li block …) …), (ol …), (pre "code"), (table (tr (th …) …) (tr (td …) …) …), (displaymath "latex"), (html "text"), (footnote-def "label" block …) and (ref-def "text") for the line of a definition of a link to which nothing refers (the others are inlined in the links).

Text

Strings, (em …), (strong …), (del …), (code "text"), (a …), (img), (br), (math "latex"), (displaymath "latex"), (html "text") and (footnote "label").

Attributes

As in sxml, (tag (@ (name "value") …) …): start and loose for the lists, checked for the items of a task list, lang for pre, align for the cells, href and title for the links, src, alt, title, width and height for the images (the sizes as in HTML: 300, 50%, or with a unit). The children of img are its description, of which alt is the plain text; the tag img of HTML is read as this node too (md-html-image).

A snippet of a single paragraph is the list of its nodes, without the p, so that a piece of text which is pasted stays inside its line. In a session of Scheme, (convert s "markdown-snippet" "markdown-stree") shows the tree of a text.

3.The parser

markdownin.scm

follows the two passes of the specification of CommonMark.

Blocks

The first pass works on the lines (md-blocks): the YAML header (md-front-matter), the fences of code, the indented code, the headings, the rules, the quotations (with their lazy lines), the lists (tight or loose, with the number of their first item, their tasks), the tables of GitHub, the formulas between $$ or \[ and \], the blocks of HTML, and the definitions of the links and of the footnotes, which are collected before the text is read.

Text

The second pass reads the text of the paragraphs, the headings and the cells (md-inlines): the escapes, the code, the formulas, the entities, the links and the images, the references to footnotes, the addresses, the tags of HTML, and then the emphasis, by the algorithm of the delimiter runs of CommonMark (~~ included).

A tab at the start of a line goes to the next multiple of four columns for the blocks and stays a tab in what they hold. md-expand-tabs reads a tab of width \(n\) as md-tab-head followed by \(n - 1\) times md-tab-fill, two control characters which md-indent and md-space? count as spaces; the columns which a block takes away (md-dedent) leave fills without their head, which are spaces, and md-untab makes a head with its fills a tab again in the tree. A text which holds one of these two characters has its tabs read as spaces.

Formulas are recognized before the emphasis, so that _ and * in a formula are left alone. md-dollar-math has the rules of Pandoc for the dollars, and md-literal-brackets? tells the escaped brackets which other programs write (\[2\]) from a formula.

A file may be made to take a time which grows as the square of its length, or faster, and a file of a few kilobytes then hangs the editor. What is looked for and not found is therefore looked for once: the strings which do not occur any more in the text which is cut into tokens, the runs of backquotes of each length (md-code-runs) and the brackets with their ends (md-none); a formula which does not end before a blank line (md-no-math-end) and a comment of HTML which does not end (md-no-comment-end?). The quotations and the lists inside each other are read down to md-max-depth levels, each level being a call inside the previous one, and the lists of nodes are walked by loops and not by calls, for the paragraphs of thousands of nodes. On the import a footnote inside a footnote is left as its mark (mdtm-footnote): definitions which refer to each other twice would make a document whose size doubles with each of them.

4.The import

markdown->texmacs maps the blocks and the text to the tags of the standard styles: the sections, itemize and enumerate, quotation, hrule, block for the tables, hlink, image, footnote. An image alone in its paragraph, with a description or a title, is a big-figure with this caption (mdtm-figure), as Pandoc does; the sizes of an image are its width and height (mdtm-size). A link with a title is hlink*, a macro of std-markup which is typeset as hlink and which the converters for HTML know too. The language of a fence selects a tag lang-code by the table mdtm-languages. Formulas are given to the converter of LaTeX (latex-snippet) and HTML to the one of HTML (html-snippet). A document gets the style generic; its title comes from the header or from a single heading of the first level (mdtm-body).

5.The export

5.1.Macro expansion

As for HTML, a document is expanded before it is converted, so that the converter sees the numbers and the references as text: buffer_export (

Texmacs/Data/new_buffer.cpp

) and the copy of a selection (

Edit/Replace/edit_select.cpp

) call edit_typeset_rep::exec_markdown (

Edit/Editor/edit_typeset.cpp

, markdown-expand in Scheme). It evaluates the tree in the environment of the document, patched by (tmmarkdown-env-patch) (

tmmarkdown-expand.scm

), where a macro tmmarkdown-name of the style, if any, replaces name.

The patch redefines as themselves the macros which the converter wants to see: the renderers of the theorems and of the figures, the titles of the sections, the lists, the environments of code and of mathematics, footnote, render-key… The title, doc-data, is given back quoted as (markdown-doc-data …) by the function tmmarkdown-doc-data: the result of an extern is evaluated, and a tag which is not a macro would be an error.

5.2.The converter

texmacs->markdown dispatches on the tag with the table tmmarkdown-methods% (a logic-dispatcher, as tmhtml-methods%). A method takes the arguments of the tag and returns a list of nodes; a tag without a method is replaced by the conversion of its arguments. The converter also accepts trees which were not expanded, with the tags of the styles, for the calls from Scheme.

6.The style for the files

A file is read as a document of the style markdown (

styles/markdown.ts

), and written as it was read. The import of a file (mdtm-faithful?, off for a piece of text and with the option markdown->texmacs:faithful) uses the macros of this style for what the standard tags do not say, and the export knows them (tmmd-faithful? for a document of the style, where each heading has its level and a link to #name is kept).

Structure

md-metadata and md-meta for the lines of the header, of which the tree keeps the source; md-h6; md-loose; the three lists for the markers -, * and + (the attribute marker); md-fence (the attribute info); md-image; md-footnote and md-footnote-text.

Sources

md-source and md-block-source hold a body, the text of the file for it and a copy of the body: a formula, a table, a piece of HTML, and with an empty body what TeXmacs does not understand. Every formula is kept so (mdtm-keep-math): to know which ones TeXmacs writes in the same way, each would be converted to LaTeX again, which takes as long as reading it. The text is written as long as the body is the copy. In a buffer this must be told before the macros are expanded, which changes both: the two macros are replaced by externs (tmmarkdown-md-source in

tmmarkdown-expand.scm

) which get their arguments as they are and leave md-kept with the text, or the body. A text which is kept becomes a node with the attribute source, which the serializer writes as it is. Kept so as well: text with a tab or another control character (shown as a space, kept as <#9>, since the codes below 32 are accents in the encoding of TeXmacs), the unused definitions of links and of footnotes, and the marks of a footnote after its first one (mdtm-footnote). A table which is kept counts neither its links nor its marks of footnotes as uses: the definitions stay as they were written (md-refs-kept, mdtm-in-kept-table?), since the table is written from its text as long as it is not edited. A header is a header when each of its lines is one which YAML has and one at least is a key (md-yaml?); the export writes only such headers (tmmd-md-metadata), and snippets have none.

Other formats

The LaTeX export knows the macros (tmtex-md-body and the others in

tmtex.scm

): it writes what they display, and not what they keep.

The property which the tests check: for a file \(x\), the file \(N (x)\) which is written after reading it has the same Markdown tree as \(x\), and \(N (N (x)) = N (x)\). The spelling which \(N\) changes is the one which has no node of its own in the tree (lines of a paragraph, headings with a line under them, fences, rules, markers of emphasis, definitions of links, entities). To keep one more, give it an attribute in the parser, a macro in the style, and handle both in the import, the export and the serializer; or keep the block in md-block-source.

7.The serializer

serialize-markdown writes a block as a list of lines, to which the blocks around it add their prefix: > for a quotation, the indentation of an item. A paragraph is one line. The text is escaped by mdout-escape, only where it would be read as markup, and the start of a line by mdout-escape-start, where it would start another block. The code is written between more backquotes than it holds, and the columns of a table are padded to their width.

8.Tests

The suite markdown of (run-all-tests), in

check/markdown-test.scm

, checks the four steps and the round trips on small texts and trees: (run-regression-suite "markdown"). A text which is written and read again gives the same tree, and the texts which the export writes come back unchanged from an import and an export.

9.Extending the converters

10.Known limitations

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".