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,
|
2012-12-23 01:44:40 -05:00
|
|
|
# with a figcaption listing the file name and download link
|
2011-07-18 17:04:56 -04:00
|
|
|
#
|
2011-07-21 15:45:09 -04:00
|
|
|
# Example 2:
|
2012-12-23 01:44:40 -05:00
|
|
|
# You can also include an optional title for the <figcaption>
|
2011-07-21 15:45:09 -04:00
|
|
|
#
|
2012-06-05 01:07:40 -04:00
|
|
|
# {% include_code javascripts/test.js Example 2 %}
|
2011-07-21 15:45:09 -04:00
|
|
|
#
|
2012-12-23 01:44:40 -05:00
|
|
|
# will output a figcaption with the title: Example 2 (test.js)
|
2011-07-21 15:45:09 -04:00
|
|
|
#
|
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
|
|
|
|
2012-12-23 01:38:01 -05:00
|
|
|
options = parse_markup(markup)
|
|
|
|
@lang = options[:lang]
|
|
|
|
@title = options[:title]
|
|
|
|
@lineos = options[:lineos]
|
|
|
|
@marks = options[:marks]
|
|
|
|
@url = options[:url]
|
|
|
|
@link_text = options[:link_text]
|
|
|
|
@start = options[:start]
|
|
|
|
@end = options[:end]
|
|
|
|
markup = clean_markup(markup)
|
2012-05-28 05:29:18 -04:00
|
|
|
|
2012-06-03 01:13:39 -04:00
|
|
|
if markup.strip =~ /(^\S*\.\S+) *(.+)?/i
|
|
|
|
@file = $1
|
2012-12-23 01:38:01 -05:00
|
|
|
@title ||= $2
|
2012-06-03 01:13:39 -04:00
|
|
|
elsif markup.strip =~ /(.*?)(\S*\.\S+)\Z/i # Title before file is deprecated in 2.1
|
2012-12-23 01:38:01 -05:00
|
|
|
@title_old = $1
|
2012-06-03 01:13:39 -04:00
|
|
|
@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?
|
2012-12-23 01:38:01 -05:00
|
|
|
@title ||= @title_old
|
2012-06-03 01:13:39 -04:00
|
|
|
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
|
2012-12-23 01:38:01 -05:00
|
|
|
@end ||= length
|
|
|
|
@start ||= 1
|
2012-05-27 03:30:17 -04:00
|
|
|
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-12-23 01:38:01 -05:00
|
|
|
highlight(code, @lang, {title: title, url: url, link_text: @link_text || 'view raw', 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)
|