about contact

Stefan Liebenberg

:web-log


Plugin to highlight code from source files in jekyll.

01 May 2013

Drop this into _plugins/inlineh.rb in your jekyll project.

/assets/ruby/jekyll-plugins/inlineh.rb:

module Jekyll
  class RenderTimeTag < Liquid::Tag
    def initialize(tag_name, text, tokens)
      super
      parts = text.split(" ");
      @filename = parts[0];
      @type = parts[1..(-1)].join(" ") || "text";
      path = File.join('html', @filename);
      if File.exists?(path)
        @content = File.read(path)
      else
        @content = ""
      end
      @highlighter = Liquid::Template.tags["highlight"].new(
          "highlight", @type, [@content, "{% endhighlight %}"]);
    end

    def render(context)
      link = "<a href=\"#{@filename}\">#{@filename}:</a>"
      code = @highlighter.render(context)
      "#{link}\n\n#{code}"
    end
  end
end

Liquid::Template.register_tag('inlineh', Jekyll::RenderTimeTag)

This reads the content of the given file and uses the highlight tag to highlight the output in you’re posts. Also includes a link to the file.

The above code example was generated using this snippet:

 {% inlineh /assets/ruby/jekyll-plugins/inlineh.rb ruby %}

Using Google Closure in phantomjs

30 Apr 2013, Author: Stefan Liebenberg

If you are developing with closure library you should have a and phantomjs, then you might want to include some of you’re development code into the phantomjs runtime. It turns out you could hack into the goog.provide/require statements to override the IMPORT function.

The following code will make the entire goog.* namespace available to be required via goog.require().

  var fs = require('fs');

  // the relative path to the goog folder.
  CLOSURE_BASE_PATH = 'src/closure-library/closure/goog/';

  // the function to inject scripts.
  CLOSURE_IMPORT_SCRIPT = function(src) {
    return phantom.injectJs(fs.absolute(src));
  };

  // import the base.js file.
  CLOSURE_IMPORT_SCRIPT(fs.join(CLOSURE_BASE_PATH, 'base.js'));

If you also parts of your own codebase, you will need to load the relevant deps.js files.

  CLOSURE_IMPORT_SCRIPT('src/path/to/deps.js');

The following script provides this functionality:

/assets/closure-setup.js:

 1 /**
 2  * @namespace {filesystem}
 3  */
 4 var fs = require("fs");
 5 
 6 /**
 7  * @namespace {closure}
 8  */
 9 exports = exports || {};
10 
11 /**
12  * @param {string} src
13  *        The source to pull in.
14  */
15 exports.import = function(src) {
16   return phantom.injectJs(fs.absolute(src));
17 };
18 
19 
20 
21 /**
22  * @const
23  * @type {string}
24  */
25 exports.BASE = fs.join(module.dirname,
26                        '../src/closure-library/closure/goog/')
27 
28 
29 
30 /**
31  * @param {string=} opt_basepath
32  *        The basepath.
33  */
34 exports.setup = function(opt_basepath) {
35   if (!top['CLOSURE_IMPORT_SCRIPT']) {
36     var basepath = opt_basepath || exports.BASE;
37     CLOSURE_BASE_PATH = basepath;
38     CLOSURE_IMPORT_SCRIPT = exports.import;
39     CLOSURE_IMPORT_SCRIPT(fs.join(CLOSURE_BASE_PATH, 'base.js'));
40     hasBeenSetUp = true;
41   }
42 }

To use it, download and save it somewhere in you’re project, fix the paths in the script to point to the correct sources and then require it with the relative path and run setup().

  require('./assets/closure-setup.js').setup();

Older Posts: