ztcl::markdown events
Gives the blocks and the inlines of a Markdown text as a flat list of events.
ztcl::markdown events TEXT ?-gfm VALUE? ?-positions chars|bytes|lines? ?-ids BOOLEAN?
Description
Gives a list of events in the order of the text. An event is one of four forms:
enter KIND START END ATTRIBUTES: the start of a block or an inline, with its range in the source and a dict of its attributes.exit KIND: its end.text START END ?BYTES?: text. BYTES is there when the text is not the bytes of its range: an escape, a character reference, or the text of a code block without the prefixes of its containers.break START END soft|hard: a line break.
The kinds are document, quote, list, item, task, paragraph, heading, code, html, rule, table, row, cell, emphasis, strong, strike, codespan, htmlspan, link, and image. START is the first byte and END the byte after the last one, as offsets of the UTF-8 bytes of TEXT. A code block, an HTML block, a code span, and raw HTML have their text as one text between their enter and their exit. The attributes are level (and id with -ids) of a heading; type, marker, start, and tight of a list; checked of a task; fenced and info of a code block; url and title of a link and an image; head of a row; and align of a cell.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| TEXT | string | required | The Markdown text. |
Options
| Option | Type | Default | Description |
|---|---|---|---|
-gfm | boolean or list | 1 | Turns the GFM extensions on or off, as -gfm of html. |
-positions | string | chars | Gives each position as a character index, a byte offset, or a line and a column. |
-ids | boolean | 0 | Gives each heading the attribute id, as -ids of html. |
-gfm VALUE
Turns the GFM extensions on or off, as -gfm of html.
A true boolean turns on each extension, a false one gives plain CommonMark, and a list of names turns on only those: table, tasklist, strike, autolink, and tagfilter.
% lindex [ztcl::markdown events "~~a~~" -gfm 0] 2
text 0 5% ztcl::markdown events x -gfm x
bad extension "x": must be table, tasklist, strike, autolink, or tagfilter
-errorcode TCL LOOKUP INDEX value-positions chars|bytes|lines
Gives each position as a character index, a byte offset, or a line and a column.
With chars, a position is an index of the characters of the text, as string range takes it. With bytes, it is an offset of the UTF-8 bytes of the text, and with lines a list of the line and the column, both from 1, where the column counts characters.
% lindex [ztcl::markdown events "a\nb" -positions lines] 3
break {1 2} {2 1} soft
% lindex [ztcl::markdown events "é *b*"] 3
enter emphasis 2 5 {}
% lindex [ztcl::markdown events "é *b*" -positions bytes] 3
enter emphasis 3 6 {}% ztcl::markdown events x -positions words
bad value "words": must be chars, bytes, or lines
-errorcode TCL LOOKUP INDEX value-ids BOOLEAN
Gives each heading the attribute id, as -ids of html.
The id is the slug of the text of the heading, with -1, -2, and so on after a slug that came before.
% lindex [ztcl::markdown events "# Go" -ids 1] 1
enter heading 0 4 {level 1 id go}% ztcl::markdown events x -ids maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBERReturns
A list of events.
Examples
Each example of this page runs after package require ztcl::markdown.
% ztcl::markdown events "## Pad *A*"
{enter document 0 10 {}} {enter heading 0 10 {level 2}} {text 3 7} {enter emphasis 7 10 {}} {text 8 9} {exit emphasis} {exit heading} {exit document}
% lindex [ztcl::markdown events "a & b"] 2
text 0 9 {a & b}
Errors
% ztcl::markdown events
wrong # args: should be "ztcl::markdown events text ?-gfm value? ?-positions chars|bytes|lines? ?-ids bool?"
-errorcode TCL WRONGARGS
% ztcl::markdown events x -html keep
bad option "-html": must be -gfm, -positions, or -ids
-errorcode TCL LOOKUP INDEX option
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.8
- Compute cost
- One compute unit for each 256 bytes of the text in each of the two passes, and one for each step of the work of the delimiters and the brackets.