Wednesday, October 24, 2012

Using Hazel for the automatization of the conversion: Markdown -> Latex -> PDF

Using Hazel for the automation of the conversion: Markdown -> Latex -> PDF
As I have explained in my last posts, I am trying to craft a writing chain that would allow me to write in Markdown and easily convert this readable text to Latex and PDF. I can even download the final PDF file on my iPAD using a Dropbox folder.
Hazel is a very nice automatization system that I was planning to explore more in details, and it indeed solves a lot of problems in this case.
My chain works as the following (this completes my previous message):
  1. I write my text in IAWrite (under OSX or iOS) and they get saved in the iCloud (suppose that the file is called mytext.md).
  2. On the desktop, these files appear in the mobile documents folder (~/Library/Mobile Documents/74ZAFF46HB~jp~informationarchitects~Writer/Documents). I use a first Hazel script (see Figure 1), attached to this folder, for copying this file to a Dropbox folder, as soon as a new version is saved. 
    Fig. 1: Copying the .md file
  3. When the .md file is copied, other Hazel scripts, attached to the destination Dropbox folder, start to play.
  4. One script converts the file to .html and .opml formats. This script calls multimarkdown for converting the copied markdown file to these formats (see Figures 2–3).
    Fig 2: Running the script for converting to HTML and OPML
    Fig 3: The script for calling multimarkdown
  5. Another script (see Figures 4–5) converts the file to Latex format (only if the md file contains the meta tags that are necessary for obtaining a complete Latex file at the end of the conversion – otherwise Multimarkdown converts Latex snippets that cannot be compiled by Latex).
    Fig 4: Calling the script for converting to Latex
    Fig 5: The script that calls multimarkdown
  6. A last script (see Figure 6) converts the Latex file obtained in the previous step to PDF, using latexmake. Latexmake is a Perl script that automatically runs the necessary operations for converting your tex file to PDF (the final format I have chosen here). Moreover, I have discovered that multimarkdown is not able to correctly convert accented characters in the meta tag fields. So, I have problems if the title, for example, is in French. For correctly converting these Unitype characters, one needs to compile the document using xelatex, and not pdflatex. The full Bash script I use can be downloaded below (including some commented instructions that was useful for debugging it, and that could be useful in some other contexts).

    Fig 6: Calling the script that runs latexmake
Download the script
  

No comments: