Tags

, ,

Markdown is a light weight markup language that is inspired by the formatting style of emails. As such, writing text in markdown feels very natural. For some time I was wondering, wouldn’t it be great if I could write markdown directly in ConTeXt. Markdown support can be added in two ways:

  • write a markdown parser in TeX (no thanks) or in Lua (or use one that exists)
  • use an external program (like pandoc) to parse markdown and generate ConTeXt output

I am no fan of reinventing the wheel, so I decided to take the second route but make it more general than markdown. This is an announcement of a module that can run an arbitrary external program over a bunch of text. The module is at a preliminary stage, and I do plan to add more features in the future. But, I think that even in its current form, it will be useful to others. Download the module and place it in your current directory. To use it, simply say

\usemodule[filter]

An external filter for markdown is defined as follows:

\defineexternalfilter
  [markdown]
  [filter={pandoc -w context -o \externalfilteroutputfile}]

The filter should be a program that writes it output to a file\externalfilteroutputfile: the name of this file can be changed using

\setupexternalfilter
  [markdown]
  [output={...}]

It defaults to a sane value, so I suggest that you do not change it. The \defineexternalfilter command creates a \startmarkdown ... \stopmarkdown environment that can be used as such:

\startmarkdown
## Lists ##

Unordered (bulleted) lists use asterisks, pluses, and hyphens (`*`,
`+`, and `-`) as list markers. These three markers are
interchangeable. As an example, this:

    *   Candy.
    *   Gum.
    *   Booze.

gives:

*   Candy.
*   Gum.
*   Booze.
\stopmarkdown

which gives the following: Not bad for 50 lines of code! The module has some other bells and whistles, but I have not finalized the user-interface yet. More on this module later.

Edit: Modified code so that it works with the latest version of the module.