Posts tagged ‘Markdown’

Preview (re)Structured Text in a Browser

2013-08-24 22:33

Those cute little text formats are all the rage now, especially Markdown and reStructuredText. You can write them pretty easily, without lots of markup boilerplate that HTML entails, so they increasingly rule the READMEs of various open source projects. Both GitHub and Bitbucket support them perfectly well for this purpose.

They are obviously not WYSIWYG, though, so you may want to check the formatting first, before showing your prose to the rest of the world. There are some standard HTML generators for each of those text formats, but it would be more convenient to have one tool to rule them all…
And so there is Pandoc. That nifty utility is capable of converting from many input text formats into a vast variety of output formats, including HTML and PDF. It can be installed very easily on most systems:

  1. $ sudo apt-get install pandoc

and downloadable installers are available for OS X and Windows.

pandoc is a well behaved command line program, so it can accept both files and standard input, enabling it to be used in shell pipelines. Unfortunately, browser executables are not so cooperative – they generally want files, not just streams of data. We can circumvent that with the use of /tmp directory and a little bit of shell scripting:

  1. #!/bin/sh
  3. # Preview structed text files in a browser
  4. # Usage: $ preview FILE [BROWSER]
  6. FILE="$1"
  7. BROWSER="${2:-firefox}"
  9. out=$(mktemp)
  10. pandoc "$FILE" >"$out"
  11. $BROWSER "$out"

This should work firefox and chrome, and likely with any other browser.

Next step? Maybe deploy a file system watcher, hook it up with some browser instrumentation, and make the page reload whenever a change in the source is detected. Non-trivial, but definitely doable :)


© 2023 Karol Kuczmarski "Xion". Layout by Urszulka. Powered by WordPress with