From Markdown To Pastable Formatted Text In OS X Terminal

By Tuesday, August 4, 2015 0 , , Permalink 2

I use Markdown a lot. I use it right now, to compose this post 🙂 . It’s my go-to syntax for quick plain text files for notes and such. It’s what I use when I code something that outputs “formatted” text to terminal.

Recently, I wanted to take Markdown output from a script I wrote, and send a nicely formatted version of it by email.

Read on for the solution I eventually came up with, after several iterations, and essential tips from Brett Terpstra, the Markdown Master.

tl;dr:

cat foo.md | \
  pandoc --from markdown --to html | \
  textutil -convert rtf -stdin -stdout -format html | \
  pbcopy -Prefer rtf

Markdown is my go-to syntax for everything-text

My motivating use-case

My program produces a Markdown report that looks something like this:

tl;dr - [read full report here](http://link.to.full/report.html) (total of **36,985 records produced**)

Details
=======

* Name: Foo
* Date: July 1, 2015, 10:21:03
* Showing details of 1 run on dataset
  + Run #1 (on July 01 2015 at 08:12:38 by itamar from prod-1)
    - Run type foo.
    - Command line: `./foo --bar baz`
    - Run made from Git branch master (Git hash deadbeef)

### Run Stats:

+ Input records: 39,482
+ Unique input records: 38,023
+ Processed unique records: 37,989 **(lost 34)**
+ ...

### Runtime & Cost:

+ Pre-processing finished in 1h5m on 10-100 machines (~$43)
+ Processing took ~2h45m on 100-1000 machines (~$1,412)
+ Post-processing took ~5.5hr on 1000-300 machines (~$1,135)
+ **Total cost ~$2,600.**

This is great for the terminal, but not so much for sending a nicely formatted report by email.

The goal is to make it simple to go from that to this:

My solution

For the sake of simplicity, let’s assume I have the Markdown text in a file foo.md. It doesn’t really matter, but this way, I can completely ignore the way the Markdown is generated.

I already spoiled in the post intro:


cat foo.md | \
  pandoc --from markdown --to html | \
  textutil -convert rtf -stdin -stdout -format html | \
  pbcopy -Prefer rtf

I arrived at this solution after seeking advice from Brett Terpstra, who knows everything about Markdown.

Brett’s suggestion was to use the MultiMarkdown to RTF Service in his Markdown Service Tools collection. You can read more about this specific service in the blog post that introduced it. Once the service is available, I can configure keyboard shortcuts, or wrapper-services, to make my life easier. For example, I can get the Markdown text into the clipboard (with pbcopy), and paste directly the styled RTF in Gmail (with an Automator service + keyboard shortcut that uses the clipboard instead of highlighted text).

Unfortunately, I couldn’t get the service to work, and didn’t feel like digging into it.

With that not working, I extracted this from looking inside the MultiMarkdown to RTF service:


cat foo.md | \
  pandoc --from markdown --to html | \
  textutil -convert rtf -stdin -stdout -format html | \
  pbcopy -Prefer rtf
  1. pandoc takes care of converting Markdown to HTML.
  2. textutil follows up by converting HTML to RTF.
  3. pbcopy copies the formatted text to the clipboard, telling the next paste command to look first for RTF content.

I wouldn’t say this yields perfect results, though:

I don’t really know what’s up with these issues, but it looks “good enough” as a sent email:

I aimed for a nicer result, if you compare this to motivation screenshot above. I got that result with Markable, in case you’re curious.

Why not just pandoc?

pandoc is quite versatile. It is capable of outputting RTF. So why did I use it to output HTML, and then another tool (textutil) to convert that HTML to RTF?

Well, I tried…

cat foo.md | pandoc -s --from markdown --to rtf | bcopy -Prefer rtf

Summary

There you have it. A simple one-liner to get from a Markdown blob, to RTF in your clipboard, ready to paste wherever you want.

This is so useful for me, that I defined an alias for it, so I can cat foo.md | mrender.

alias mdrender='pandoc --from markdown --to html | textutil -convert rtf -stdin -stdout -format html | pbcopy -Prefer rtf'

This solution has its quirks, and could benefit from better style. If you know how to improve it, please let me know! 🙂

No Comments Yet.

Leave a Reply