Redecorating

When you find yourself in a new place that you intend to stay at for some time, you usually want to personalize it a bit.. Some time may be needed for making the big changes, so that you don't overdo it, but there are always a few itches that you just have to scratch!

For Ghost, when starting a code-centric blog, one of those is syntax highlighting. Luckily, others before me have solved this problem; in particular, Peter Timofejew wrote a blog post about how to solve it without modifying your theme:

https://www.timofejew.com/fixing-ghost-casper-theme/

Of the two syntax highlighting options, highlight.js has Elixir support according to the docs. Problem is, the CDN-hosted highlight.js.min doesn't include Elixir!

To fix that, we can add languages/elixir.min.js as well, and upgrade to highlight.js 8.6 at the same time, ending up with the following in our Blog Header injection:

<!-- http://highlightjs.readthedocs.org/en/latest/css-classes-reference.html -->  
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/styles/github.min.css">  
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/highlight.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/languages/elixir.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>  
<style>  
  pre {
    word-wrap: normal;
    -moz-hyphens: none;
    -ms-hyphens: none;
    -webkit-hyphens: none;
    hyphens: none;
    font-size: 0.7em;
    line-height: 1.3em;
  }
    pre code, pre tt {
    white-space: pre;
  }
</style>

Now, I can get my Elixir code[^1] a bit more readable:

defmodule Everex.Util do
  require Record

  defmacro __using__(_options) do
    quote do
      require Record
      require Everex.Util
      import Everex.Util, only: [deftype: 3]
    end
  end

  def record_to_struct(the_record, fields, struct_name) do
    [_tag | values] = the_record |> Tuple.to_list
    do_record_to_struct(values, fields, struct(struct_name))
  end

  defp do_record_to_struct([], [], acc), do: acc
  # value is record; convert to struct
  defp do_record_to_struct([value|vt], [{key,:undefined}|dt], acc)
  when Record.is_record(value)
  do
    acc = struct(acc,[{key, Everex.Types.to_struct(value)}])
    do_record_to_struct(vt, dt, acc)
  end
  # value is list; process and convert contents to structs if needed
  defp do_record_to_struct([value|vt], [{key,[]}|dt], acc) do
    value = r2s_process_list(value, [])
    do_record_to_struct(vt, dt, struct(acc,[{key, value}]))
  end
  # it's just a regular type, update struct key with the value
  defp do_record_to_struct([value|vt], [{key,:undefined}|dt], acc) do
    do_record_to_struct(vt, dt, struct(acc,[{key, value}]))
  end

  # ...

end

[^1]: Code extract from Everex, my Evernote API client for Elixir (https://github.com/jwarlander/everex/blob/master/lib/everex/util.ex#L16-L48)