Live-ish preview for MMD with Scrivener


UPDATE: A much improved version, which doesn’t rely on Automator, is available below.

A lot of what I write in Scrivener includes math and tables, which aren’t particularly easy to read using the MMD/LaTeX syntax. To help, I’ve put together an automator workflow that:

  • takes the current selection;
  • appends metadata,
  • converts to HTML, and
  • previews in Marked.app (although any web browser works too).

I’ve saved this as an OS X service, and set up a keyboard shortcut (using Keyboard Maestro) so that pressing a hotkey previews the current Scrivener selection, or updates it (if the preview window is already open).

The main advantage is for use when writing math. The metadata includes a HTML header that points to MathJax.js, so all LaTeX math is previewed in HTML (which makes editing easier).

The metadata is:

Title: MMD Preview
CSS: /path/to/theme.css
HTML header: <script type="text/javascript"
       src="http://mathjax.org/mathjax/MathJax.js">
       </script>

You can also adjust the output using CSS, if needed. It’s fairly crude, but I find this workflow very useful. Sharing here in case anyone else might too. The full Automator workflow can be viewed here: http://cl.ly/AhbD Suggestions welcome!

Update: I’ve found a much better way of doing this, that doesn’t rely on Automator…

One annoyance I’ve had with MMD in Scrivener is images. I use MMD reference syntax for images, so I can specify titles and width/heights. I keep all my images in a folder outside of Scrivener, so they can be easily edited later. To include an image I write, for example:

![Coefficient plot with 95% credible intervals for `severity` (MCMC estimation)][fig]

And then at the bottom of the page:

[fig]: ../graphics/5_workintensity/5c_acrosseurope/plots/affect/severity_base.pdf

This is great, but to preview an image it must be opened externally (e.g. in Preview). To get over this, the script below replaces “…/” with the full path my ‘graphics’ folder, such that when previewing the document in Marked.app, the images are displayed correctly.

To recap, the script:

  • Save the clipboard to a file.
  • Appends some MMD metadata
  • Replaces “…/” with the full path to my ‘graphics folder’.
  • And opens the final in Marked.app, for previewing.

So, with this script I get an instant preview from Scrivener that includes images, tables and math/equations. Even better, this version relies on Marked.app to produce the HTML, so can be styled using Marked’s CSS themes. It’s also much faster than the Automator version, above.

As before, I use it via Keyboard Maestro, which, triggered by a hotkey, copies the selected text and runs the script.

[code]pbpaste > /tmp/content.txt
# Save clipboard content

echo "Title: MultiMarkdown Math Preview
CSS: /Users/ewancarr/Documents/Work/Code/CSS/Byword.css
HTML header:

" > /tmp/header.txt
# Save MMD header

awk ‘{gsub(/[.]{2}//,"/Users/ewancarr/Documents/Work/PhD/Writing/");print}’ /tmp/content.txt > /tmp/content_fix.txt
# Replace “…/” with the full path.

cat /tmp/header.txt /tmp/content_fix.txt > /tmp/combined.txt
# Combine the header and content.

open /tmp/combined.txt -a /Applications/Marked.app
# Open the file in Marked.app

[/code]

I didn’t manage to build a Service with Automator.app in 10.5 (Leopard).

Are services supported by Automator only from Snow Leopard onwards?

@szabi: I’m not sure, but the script version is a much better way to go.

I’ve updated it (again) to be even simpler. Marked 1.3, released yesterday, has baked-in support for MathJax, so there’s no need to add any metadata. The script now looks like:

pbpaste > /tmp/content.txt 

awk '{gsub(/[.]{2}\//,"/Users/ewancarr/Documents/Work/PhD/Writing/") ; print}'  /tmp/content.txt > /tmp/MMD_Preview.txt
	
open /tmp/MMD_Preview.txt -a /Applications/Marked.app

(Note: this does require you to copy the selected text first). I trigger this using Keyboard Maestro:

Though I’m guessing other solutions might also work (e.g. FastScripts, which is free). I’ve put together a 20 second video showing how this works, here: http://tinyurl.com/6c7xu8o

Actually, I wanted to use Automator only to build the Service package. I’ve included a hand-crafted bash script rather than your workflow into Automator…

I’m good enough at command line tinkering.

It’s just that I’ve got neither Keyboard Maestro nor Marked.app, so for the second I still used mmd, for the first, I wanted to wrap the whole thing into a service, and that’s where Ifailed. (Now doing it via script, but I have to explicitly Cmd+C after selecting before calling the script).

Wouldn’t it be much easier to pipe all .rtf files inside a Scrivener package through pandoc

pandoc -f rtf -t markdown

and concatenate everything into one text file that is then opened by Marked, or whatever.
Also, with Automator you could use pandoc to parse from rtf to markdown, pipe that output through mmd (the command line utility), which write a new text file to hard disk that you then tell Safari to reload whenever the workflow finishes running.

Anybody tried that already?

Edit: I’m sorry. I was wrong. pandoc can’t convert from rtf (anymore?). Anybody had luck converting from rtf directly?

Well, since presumably we’re all writing with MMD syntax in the RTF, rather than using RTF itself to do anything, textutil could be used to batch convert a bunch of RTF files to txt, then concatenate and open in Marked.

Hi Amber,

Good idea, in the meanwhile, though, someone else did write something that accomplishes what I was trying to do. It’s included in the Marked scripts. Haven’t tried it yet, but will after I finish work I get paid for. :wink:

brettterpstra.com/marked-scripts … scrivener/

Yeah, I posted about that earlier. :slight_smile: The scripts are by Brett. Looks pretty nice.

For those less technically inclined you can use a combination of Multimardown Composer as on intermediary and Marked for preview.

I forgot…I use QuickCursor as the glue between Scrivener and MultiMarkdown Composer.