Plugin to highlight code from source files in jekyll.
01 May 2013Drop 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 %}