Saturday, 16 April 2011

Bloggin' with pandoc and blogspot

17.4.2011, update 1: Changed the -fhighlight -> -fhighlighting. Thanks to Joni Freeman for spotting this.

Pandoc is a tool which allows conversion of multiple input markup formats (markdown, literal haskell) to various output formats (pdf, html). It is implemented using Haskell, and has (optional) support for syntax highlighting.

In this blog entry I'll show how

  • pandoc is set up,
  • how you can type up texts using Markdown syntax,
  • syntax highlighting works, and
  • what modifications you need to blogspot theme to support the syntax highlighting.

Let's start with pandoc.

Installing pandoc

Since pandoc is implemented in Haskell, the usual Haskell installation procedure applies. You need to have Haskell platform installed (hint: on Mac OS X, use brew). Cabal is the easiest way to install Haskell software once the base platform is installed, so install it from here.

Pandoc has optional support for syntax highlighting using highlighting-kate. It is not enabled by default, you have to add -fhighlighting flag to installation command:

    $ cabal install -fhighlighting pandoc

Test the pandoc command:

    $ pandoc -v

The command should produce following output (if the list of supported highlight languages is missing, the you are missing the highlighting support).

    pandoc 1.8.1.1
Compiled with citeproc support.
Compiled with syntax highlighting support for:
Actionscript, Ada, Alert, Alert_indent, Ansys, Apache, Asn1, Asp, Awk, Bash,
<...>

Writing blog texts

I use the Markdown syntax for writing up the texts. Markdown syntax is quite simple and elegant plain text syntax for writing lightly formatted text (such as blog texts).

Here's quick taster of Markdown syntax:

    # Title

This is the body text, with *highlight* and [an url](http://www.google.com).

* First item in list
* Second item
* Third item

Example of command call

~~~ {.bash}
echo "Foobar" > /dev/null
~~~

Note that I use some pandoc extensions here for command syntax highlighting. You can remove the tilde-lines to have everything in standard Markdown. (Hint: I use syntax .mediawiki to highlight these markup snippets).

Syntax highlighting

Pandoc has extended Markdown syntax with couple of useful additions. The most important for technical blogging purposes is the delimited code block extension. It allows marking up code blocks with tilde-characters, and adding language hint for syntax highlighting. Following example is from above Pandoc page:

    ~~~ {.haskell}
qsort [] = []
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++
qsort (filter (>= x) xs)
~~~

The markup produces following output:

    qsort []     = []
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++
qsort (filter (>= x) xs)

As a second example, here is some (contrived) Python code highlighted:

    import os

def current_path():
return os.getcwd()

if __name__ == "__main__":
print "Hello, %s" % os.path.abspath(current_path())

Putting the examples together, the typical text I write looks something like this in markup form:

    # Title

This is the body text, with *highlight* and [an url](http://www.google.com).

* First item in list
* Second item
* Third item

Example of command call

~~~ {.bash}
$ echo "Foobar" > /dev/null
~~~

## Code samples

Quicksort in Haskell:

~~~ {.haskell}
qsort [] = []
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++
qsort (filter (>= x) xs)
~~~

Contrived example of Python code:

~~~ {.python}
import os

def current_path():
return os.getcwd()

if __name__ == "__main__":
print "Hello, %s" % os.path.abspath(current_path())
~~~

I use Vim as an editor with syntax highlighting, so the markup is nicely colored.

Building output

Following command can be used to build stand-alone document, which contains header and footer elements:

    $ pandoc -s blogentry.md -o blogentry.html

The produced document can be opened in browser to see how the output is laid out.

If you are using pandoc as a helper for blogging to, for example, blogspot, then you can easily produce output that can be copy-pasted to blog editor there by omitting the -s flag:

    $ pandoc -s blogentry.md -o blogentry.html

I typically at this point open the html-file to Chrome using command

    $ open blogentry.html

and then copy and paste the HTML code to blogspot for preview. You may need to snip the first heading out, since the blog software usually provides it for you.

Modding blogspot style

The generated output generally works fine without any modifications, but syntax highlighting requires some extra CSS to be added to your blog template.

Fortunately, there are existing stylesheets for highlighting-kate module which pandoc uses, and they can be found from highlighting-kate Git repository.

The extra CSS must be copy-pasted to Blogger's template editor. First, open the template editor, find the Extra settings selection on topright (last item), then from the opened list select the "Add CSS". Paste the desired stylesheet here and save.

Final comments

I first planned to add quick instructions for using Google command line tools for automatic upload of generated comments, but this entry got fairly long so I'll write a new entry about that.

Meanwhile, hopefully you found this entry useful. You may also want to check out Pandoc documentation, Markdown syntax documentation and Hakyll (a site generator written in Haskell which uses Pandoc for markup handling).

Thursday, 7 April 2011

Vim as fullscreen, distraction free editor

Distraction-free editors such a Writeroom, Darkroom, JDarkroom and others are very popular nowadays. I like them too, writing is much more productive when you can dim the colors and remove all the bells and whistles from the user interface and focus on the text.

Unfortunately, I have a slight problem. I have trained my fingers and brain to use Vim keybindings, and it is fairly painful to try to edit something using classical keybindings. This prompted me to check if I can convert my trusty Vim to simple full-screen editor.

Experimentation

I typically do my writing on Mac OS X laptop, and this means that I usually use MacVim with my customized configuration. I started the process by checking out the MacVim capabilities, and lo'and behold - MacVim supports fullscreen mode (using CMD+SHIFT+F keybinding).

Next step is to see what font would look nice on the full screen mode. When programming, smaller fonts are very useful to keep as much information as possible on the screen, but they also strain eyes which is undesirable when writing text. After a quick test, I settled (for now) to Menlo, 18pt font. It can be set on MacVim using following command:


:set guifont=Menlo:h18

I did not really want to have 16:10 screen to be filled with text from left to right, so I tested narrowing the display, and finally settled to 90 columns. MacVim automatically centers the view in full screen mode, which was very pleasant surprise.


:set columns=90

Next step in my experimentation was to find a suitable colorscheme. I decided to use for now the rubyblue theme (available here).


:colorscheme rubyblue

Basic distraction-free environment is now done, but there are still some tweaks that can be done. For example, the color scheme I have chosen is maybe a bit too bright, and status bar is visible. Also, since I have just typed the commands to vim, I need to retype the commands every time I start the editor - quite fatal flaw indeed.

Small iterative improvements

First, I'll see if I can hide the statusbar. Unfortunately, not everything can be hidden, but Ubuntuforums provided following tip:


:set laststatus=0

I think this is good enough.

Next, I'll try to find a nicer color scheme. I prefer dark color schemes, since I've noticed that the white background strains eyes (the backlight). I downloaded the vim color sampler pack and browsed the online gallery a bit.

I settled down with color scheme named dante:


:colorscheme dante

I also want to undo some configuring I have done for programming work (cursor line highlighting, visible whitespace):


:set nocursorline :set nolist

I'd also like vim to go to full screen mode when I load the settings. This is possible using following command:


:set fu

When I open a markdown file, all sections are automatically folded, since I have markdown bundle installed. This is quite irritating, but can be disabled with following command:


:set nofoldenable

Making profile

I created a file called ~/.gvimrc.fullscreen and put following contents to it:


" Include defaults source ~/.vimrc source ~/.gvimrc set guifont=Menlo:h18 set columns=90 set laststatus=0 colorscheme dante set nocursorline set nolist set nofoldenable set fu " Finally, clear hightlight noh

The settings can now be taken use with command:


:source ~/.gvimrc.fullscreen

Final tweaks

Keyboard shortcuts are quite easy to add in vim. Following command automatically loads the full screen profile, when F3 is pressed:


:nmap <F3> :so ~/.gvimrc.fullscreen<CR>

Switching back to normal profile can also be mapped as easily:


:nmap <F2> :so ~/.gvimrc<CR>

Note: both setting-files must overwrite the same settings in order for this to work.

Spell checking is always nice thing to have. Fortunately, Vim7 has built-in spell checker which can be enabled with command:

:set spell

Final version

The final version of my config (in ~/.gvimrc.fullscreen) is following:


" Include defaults source ~/.vimrc source ~/.gvimrc set guifont=Menlo:h18 set columns=90 set laststatus=0 colorscheme dante set nocursorline set nolist set nofoldenable set fu nmap <F2> :so ~/.gvimrc<CR> nmap <F3> :so ~/.gvimrc.fullscreen<CR> " Finally, clear highlight which for some reason goes on noh

I have also made following additions to my regular ~/.gvimrc:


<...> " Full screen mode nmap <F2> :so ~/.gvimrc<CR> nmap <F3> :so ~/.gvimrc.fullscreen<CR> set nofu <...>

That's it. Happy vimming!