beetlebug▾
/

ztcl::markdown html

Gives the HTML of a Markdown text.

ztcl::markdown html TEXT ?-gfm VALUE? ?-html escape|keep? ?-ids BOOLEAN? ?-fence CMD? ?-link CMD? ?-heading CMD?

Description

Gives the HTML of TEXT in the form of the reference implementation of CommonMark: each block on its own line, <hr /> and <br />, and the paragraphs of a tight list with no <p>. Raw HTML in TEXT goes out as text unless -html keep is given. The hooks -fence, -link, and -heading are command prefixes that run in the interpreter during the render, and an error of a hook is the error of the command.

Arguments

ArgumentTypeDefaultDescription
TEXTstringrequiredThe Markdown text.

Options

OptionTypeDefaultDescription
-gfmboolean or list1Turns the GFM extensions on or off.
-htmlstringescapeWrites raw HTML of the text as text or as HTML.
-idsboolean0Gives each heading an id from its text.
-fencecommand prefixnoneRuns a command for each fenced code block.
-linkcommand prefixnoneRuns a command for the URL of each link and image.
-headingcommand prefixnoneRuns a command for each heading, and its result is the id of the heading.

-gfm VALUE

Turns the GFM extensions on or off.

A true boolean turns on each extension, and a false one or an empty list gives plain CommonMark. A list of names turns on only those extensions: table, tasklist, strike, autolink, and tagfilter.

% ztcl::markdown html "~~x~~ www.a.org" -gfm strike
<p><del>x</del> www.a.org</p>


% ztcl::markdown html "~~x~~" -gfm 0
<p>~~x~~</p>
% ztcl::markdown html x -gfm {table nope}
bad extension "nope": must be table, tasklist, strike, autolink, or tagfilter
-errorcode TCL LOOKUP INDEX value

-html escape|keep

Writes raw HTML of the text as text or as HTML.

With escape, the raw HTML of TEXT goes out with <, >, &, and " as character references. With keep, it goes out as it is, and with the GFM extension tagfilter the < of the tags title, textarea, style, xmp, iframe, noembed, noframes, script, and plaintext becomes &lt;.

% ztcl::markdown html "a <b>c</b>"
<p>a &lt;b&gt;c&lt;/b&gt;</p>


% ztcl::markdown html "a <b>c</b>" -html keep
<p>a <b>c</b></p>
% ztcl::markdown html x -html raw
bad value "raw": must be escape or keep
-errorcode TCL LOOKUP INDEX value

-ids BOOLEAN

Gives each heading an id from its text.

The id is the slug of the text of the heading: lower case, only letters, digits, _, -, and spaces, and each run of spaces as one -. A slug that came before gets -1, -2, and so on. The rule is the rule of the anchors of the ztcl site.

% ztcl::markdown html "## Fuel and crew\n\n## Fuel and crew" -ids 1
<h2 id="fuel-and-crew">Fuel and crew</h2>
<h2 id="fuel-and-crew-1">Fuel and crew</h2>
% ztcl::markdown html x -ids maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER

-fence CMD

Runs a command for each fenced code block.

CMD runs with two more words: the info string of the block and its text. A result other than the empty string replaces the HTML of the block.

% ztcl::markdown html "```dot\na\n```" -fence {apply {{info text} {return "<svg>$info</svg>"}}}
<svg>dot</svg>
% ztcl::markdown html "```\na\n```" -fence {apply {args {error boom}}}
boom
-errorcode NONE

Runs a command for the URL of each link and image.

CMD runs with the URL as one more word, and its result is the URL to write.

% ztcl::markdown html {[the pad](pad.md)} -link {string map {.md .html}}
<p><a href="pad.html">the pad</a></p>
% ztcl::markdown html {[a](/x)} -link {error boom}
boom
-errorcode NONE

-heading CMD

Runs a command for each heading, and its result is the id of the heading.

CMD runs with three more words: the level, the text, and the id that -ids gives. Each heading gets the result as its id, with or without -ids.

% ztcl::markdown html "## Two words" -heading {apply {{level text id} {return h$level-$id}}}
<h2 id="h2-two-words">Two words</h2>
% ztcl::markdown html "# a" -heading {apply {args {error no}}}
no
-errorcode NONE

Returns

A string of HTML.

Examples

Each example of this page runs after package require ztcl::markdown.

% ztcl::markdown html "The pad is *clear*."
<p>The pad is <em>clear</em>.</p>


% ztcl::markdown html "- \[x\] fuel"
<ul>
<li><input checked="" disabled="" type="checkbox"> fuel</li>
</ul>

Errors

% ztcl::markdown html
wrong # args: should be "ztcl::markdown html text ?-gfm value? ?-html escape|keep? ?-ids bool? ?-fence cmd? ?-link cmd? ?-heading cmd?"
-errorcode TCL WRONGARGS

% ztcl::markdown html x -bogus 1
bad option "-bogus": must be -gfm, -html, -ids, -fence, -link, or -heading
-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.

See also