2011-07-18 21:04:56 +00:00
|
|
|
# Title: Include Code Tag for Jekyll
|
|
|
|
# Author: Brandon Mathis http://brandonmathis.com
|
|
|
|
# Description: Import files on your filesystem into any blog post as embedded code snippets with syntax highlighting and a download link.
|
|
|
|
# Configuration: You can set default import path in _config.yml (defaults to code_dir: downloads/code)
|
|
|
|
#
|
|
|
|
# Syntax {% include_code path/to/file %}
|
|
|
|
#
|
2011-07-21 19:45:09 +00:00
|
|
|
# Example 1:
|
2011-07-18 21:04:56 +00:00
|
|
|
# {% include_code javascripts/test.js %}
|
|
|
|
#
|
|
|
|
# This will import test.js from source/downloads/code/javascripts/test.js
|
|
|
|
# and output the contents in a syntax highlighted code block inside a figure,
|
|
|
|
# with a figcaption listing the file name and download link
|
|
|
|
#
|
2011-07-21 19:45:09 +00:00
|
|
|
# Example 2:
|
|
|
|
# You can also include an optional title for the <figcaption>
|
|
|
|
#
|
|
|
|
# {% include_code Example 2 javascripts/test.js %}
|
|
|
|
#
|
|
|
|
# will output a figcaption with the title: Example 2 (test.js)
|
|
|
|
#
|
2011-07-18 21:04:56 +00:00
|
|
|
|
2011-07-27 03:36:42 +00:00
|
|
|
require './plugins/pygments_code'
|
2011-06-15 22:31:22 +00:00
|
|
|
require 'pathname'
|
|
|
|
|
|
|
|
module Jekyll
|
|
|
|
|
|
|
|
class IncludeCodeTag < Liquid::Tag
|
2011-07-27 03:36:42 +00:00
|
|
|
include HighlightCode
|
2011-07-20 15:02:49 +00:00
|
|
|
def initialize(tag_name, markup, tokens)
|
|
|
|
@title = nil
|
|
|
|
@file = nil
|
2011-08-21 14:31:14 +00:00
|
|
|
if markup.strip =~ /\s*lang:(\w+)/i
|
2011-08-21 00:24:51 +00:00
|
|
|
@filetype = $1
|
2011-08-21 14:31:14 +00:00
|
|
|
markup = markup.strip.sub(/lang:\w+/i,'')
|
2011-08-21 00:24:51 +00:00
|
|
|
end
|
2011-07-20 15:02:49 +00:00
|
|
|
if markup.strip =~ /(.*)?(\s+|^)(\/*\S+)/i
|
|
|
|
@title = $1 || nil
|
|
|
|
@file = $3
|
|
|
|
end
|
2011-06-15 22:31:22 +00:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def render(context)
|
2011-07-29 14:49:37 +00:00
|
|
|
code_dir = (context.registers[:site].config['code_dir'].sub(/^\//,'') || 'downloads/code')
|
2011-06-15 22:31:22 +00:00
|
|
|
code_path = (Pathname.new(context.registers[:site].source) + code_dir).expand_path
|
|
|
|
file = code_path + @file
|
|
|
|
|
|
|
|
if File.symlink?(code_path)
|
|
|
|
return "Code directory '#{code_path}' cannot be a symlink"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless file.file?
|
|
|
|
return "File #{file} could not be found"
|
|
|
|
end
|
|
|
|
|
|
|
|
Dir.chdir(code_path) do
|
|
|
|
code = file.read
|
2011-08-21 00:24:51 +00:00
|
|
|
@filetype = file.extname.sub('.','') if @filetype.nil?
|
2011-07-20 15:02:49 +00:00
|
|
|
title = @title ? "#{@title} (#{file.basename})" : file.basename
|
2011-08-21 20:15:46 +00:00
|
|
|
url = "/#{code_dir}/#{@file}"
|
2011-07-20 15:02:49 +00:00
|
|
|
source = "<div><figure role=code><figcaption><span>#{title}</span> <a href='#{url}'>download</a></figcaption>\n"
|
2011-07-27 03:36:42 +00:00
|
|
|
source += " #{highlight(code, @filetype)}</figure></div>"
|
2011-06-15 22:31:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
Liquid::Template.register_tag('include_code', Jekyll::IncludeCodeTag)
|