2011-07-18 17:04:56 -04: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 15:45:09 -04:00
|
|
|
# Example 1:
|
2011-07-18 17:04:56 -04: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 15:45:09 -04: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 17:04:56 -04:00
|
|
|
|
2011-07-26 23:36:42 -04:00
|
|
|
require './plugins/pygments_code'
|
2011-06-15 18:31:22 -04:00
|
|
|
require 'pathname'
|
|
|
|
|
|
|
|
module Jekyll
|
|
|
|
|
|
|
|
class IncludeCodeTag < Liquid::Tag
|
2011-07-26 23:36:42 -04:00
|
|
|
include HighlightCode
|
2011-07-20 11:02:49 -04:00
|
|
|
def initialize(tag_name, markup, tokens)
|
|
|
|
@title = nil
|
|
|
|
@file = nil
|
2012-05-27 03:14:37 -04:00
|
|
|
@start = 1
|
|
|
|
@end = nil
|
2011-08-21 10:31:14 -04:00
|
|
|
if markup.strip =~ /\s*lang:(\w+)/i
|
2011-08-20 20:24:51 -04:00
|
|
|
@filetype = $1
|
2011-08-21 10:31:14 -04:00
|
|
|
markup = markup.strip.sub(/lang:\w+/i,'')
|
2011-08-20 20:24:51 -04:00
|
|
|
end
|
2012-05-27 03:14:37 -04:00
|
|
|
if markup =~ /\s*start:(\d+)/i
|
|
|
|
@start = $1.to_i
|
|
|
|
markup = markup.sub(/\s*start:\d+/i,'')
|
|
|
|
end
|
|
|
|
if markup =~ /\s*end:(\d+)/i
|
|
|
|
@end = $1.to_i
|
|
|
|
markup = markup.sub(/\s*end:\d+/i,'')
|
|
|
|
end
|
|
|
|
if markup =~ /\s*range:(\d+),(\d+)/i
|
|
|
|
@start = $1.to_i
|
|
|
|
@end = $2.to_i
|
|
|
|
markup = markup.sub(/\s*range:\d+,\d+/i,'')
|
|
|
|
end
|
2011-07-20 11:02:49 -04:00
|
|
|
if markup.strip =~ /(.*)?(\s+|^)(\/*\S+)/i
|
|
|
|
@title = $1 || nil
|
|
|
|
@file = $3
|
|
|
|
end
|
2011-06-15 18:31:22 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def render(context)
|
2011-07-29 10:49:37 -04:00
|
|
|
code_dir = (context.registers[:site].config['code_dir'].sub(/^\//,'') || 'downloads/code')
|
2011-06-15 18:31:22 -04: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
|
2012-05-27 03:14:37 -04:00
|
|
|
length = code.lines.count
|
|
|
|
@end ||= length
|
|
|
|
return "#{file} is #{length} lines long, cannot begin at line #{@start}" if @start > length
|
|
|
|
return "#{file} is #{length} lines long, cannot read beyond line #{@end}" if @end > length
|
|
|
|
if @start > 1 or @end < length
|
|
|
|
code = code.split(/\n/).slice(@start -1, @end + 1 - @start).join("\n")
|
|
|
|
end
|
2011-08-20 20:24:51 -04:00
|
|
|
@filetype = file.extname.sub('.','') if @filetype.nil?
|
2011-07-20 11:02:49 -04:00
|
|
|
title = @title ? "#{@title} (#{file.basename})" : file.basename
|
2011-08-21 16:15:46 -04:00
|
|
|
url = "/#{code_dir}/#{@file}"
|
2012-05-27 03:14:37 -04:00
|
|
|
highlight(code, @filetype, {caption: title, url: url, anchor: 'download', start: @start})
|
2011-06-15 18:31:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
Liquid::Template.register_tag('include_code', Jekyll::IncludeCodeTag)
|