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>
|
|
|
|
#
|
2012-06-05 01:07:40 -04:00
|
|
|
# {% include_code javascripts/test.js Example 2 %}
|
2011-07-21 15:45:09 -04:00
|
|
|
#
|
|
|
|
# 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)
|
|
|
|
@file = nil
|
2012-06-03 01:13:39 -04:00
|
|
|
@title = nil
|
|
|
|
@title_old = nil
|
2012-05-28 05:29:18 -04:00
|
|
|
|
|
|
|
@lang = get_lang(markup)
|
|
|
|
markup = replace_lang(markup)
|
|
|
|
|
|
|
|
@linenos = get_linenos(markup)
|
|
|
|
markup = replace_linenos(markup)
|
|
|
|
|
|
|
|
@marks = get_marks(markup)
|
|
|
|
markup = replace_marks(markup)
|
|
|
|
|
|
|
|
@start = get_start(markup)
|
|
|
|
markup = replace_start(markup)
|
|
|
|
|
|
|
|
@end = get_end(markup)
|
|
|
|
markup = replace_end(markup)
|
|
|
|
|
|
|
|
range = get_range(markup, @start, @end)
|
|
|
|
@start = range[:start]
|
2012-06-03 01:13:39 -04:00
|
|
|
@end = range[:end]
|
2012-05-28 05:29:18 -04:00
|
|
|
markup = replace_range(markup)
|
|
|
|
|
2012-06-03 01:13:39 -04:00
|
|
|
if markup.strip =~ /(^\S*\.\S+) *(.+)?/i
|
|
|
|
@file = $1
|
|
|
|
@title = $2 || nil
|
|
|
|
elsif markup.strip =~ /(.*?)(\S*\.\S+)\Z/i # Title before file is deprecated in 2.1
|
|
|
|
@title_old = $1 || nil
|
|
|
|
@file = $2
|
2011-07-20 11:02:49 -04:00
|
|
|
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
|
|
|
|
|
2012-06-03 01:13:39 -04:00
|
|
|
unless @title_old.nil?
|
|
|
|
@title = @title_old
|
|
|
|
puts "### ------------ WARNING ------------ ###"
|
|
|
|
puts "This include_code syntax is deprecated "
|
|
|
|
puts "Correct syntax: path/to/file.ext [title]"
|
|
|
|
puts "Update include for #{file}"
|
|
|
|
puts "### --------------------------------- ###"
|
|
|
|
end
|
|
|
|
|
2011-06-15 18:31:22 -04:00
|
|
|
if File.symlink?(code_path)
|
2012-06-03 01:13:39 -04:00
|
|
|
puts "Code directory '#{code_path}' cannot be a symlink"
|
2011-06-15 18:31:22 -04:00
|
|
|
return "Code directory '#{code_path}' cannot be a symlink"
|
|
|
|
end
|
|
|
|
|
|
|
|
unless file.file?
|
2012-06-03 01:13:39 -04:00
|
|
|
puts "File #{file} could not be found"
|
2011-06-15 18:31:22 -04:00
|
|
|
return "File #{file} could not be found"
|
|
|
|
end
|
|
|
|
|
|
|
|
Dir.chdir(code_path) do
|
|
|
|
code = file.read
|
2012-05-27 03:30:17 -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
|
2012-05-28 05:29:18 -04:00
|
|
|
@lang = file.extname.sub('.','') unless @lang
|
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-28 05:29:18 -04:00
|
|
|
highlight(code, @lang, {caption: title, url: url, anchor: 'download', start: @start, marks: @marks, linenos: @linenos })
|
2011-06-15 18:31:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
Liquid::Template.register_tag('include_code', Jekyll::IncludeCodeTag)
|