[NTG-context] ConTeXt, KeenWrite, Markdown, and XHTML

Thangalin thangalin at gmail.com
Sat Mar 6 22:55:54 CET 2021

How would you convert the first class attribute of XHTML div elements
to ConTeXt LMTX-specific start/stop environments?

For example:

<div class="poem dark">
<div class="stanza">
Whose woods these are I think I know.
<div class="stanza">
And miles to go before I sleep.

Becomes (the equivalent of?):

Whose woods these are I think I know.
And miles to go before I sleep.

In case this is an XY problem, background details follow. In a
nutshell: convert a processed Markdown file that includes interpolated
variables, arbitrary classes, and output from R functions into a
format that ConTeXt can typeset.

Background Details

Pandoc's fenced div syntax is now supported by my text editor,
allowing users to write "paragraph classes" in Markdown, such as:

::: {.poem .dark}
::: stanza
Whose woods these are I think I know.

This gets converted to XHTML (as shown above), which is to be rendered
using ConTeXt LMTX. Rendering via ConTeXt needs the following tool

Editor (.md) -> run Pandoc (.tex) -> run ConTeXt (.pdf)

Generating XHTML means Pandoc can be removed, simplifying the chain:

Editor (.md -> magic -> .xhtml) -> run ConTeXt (.pdf)

The Lua code to generate start/stop environments with Pandoc is
described in Part 8 of my Typesetting Markdown series.


The XML-MKIV documentation shows how to map a specific setup to a
particular attribute:


In Schmitz' XHTML-oriented My Way from 2010 there's a getmeas Lua
function defined on page 9 that parses a width attribute. That's great
for the opening div, but how would you close/stop the correct
environment for nested divs? Would ConTeXt automatically track these
on a stack, for example?

Additional Ideas

Given that the text editor converts Markdown to XHTML and has an XSLT
engine, the following is possible:

Editor (.md -> .xhtml) -> XSLT (.tex) -> run ConTeXt (.pdf)

Piping XHTML directly into ConTeXt would be ideal, though. Well,
piping CommonMark plus fenced div extensions to ConTeXt would be
optimal, but my understanding is that support isn't quite there yet.

Aditya wrote a Markdown filter that runs:

pandoc -t context -o \externalfilteroutputfile\space \externalfilterinputfile

This still needs Pandoc, though.

There's also a dependency-free Markdown module. A few problems though.
First, the module isn't ported to ConTeXt LMTX, yet? Second, it
doesn't support element attributes beyond enabling content slicing.
(Third, the module has LaTeX support tightly bundled within, which
bloats a pure ConTeXt solution.)

Related Links


More information about the ntg-context mailing list