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_old = nil
|
2012-05-28 05:29:18 -04:00
|
|
|
|
2012-12-24 16:59:48 -05:00
|
|
|
opts = parse_markup(markup)
|
|
|
|
@options = {
|
|
|
|
lang: opts[:lang],
|
|
|
|
title: opts[:title],
|
|
|
|
lineos: opts[:lineos],
|
|
|
|
marks: opts[:marks],
|
|
|
|
url: opts[:url],
|
|
|
|
link_text: opts[:link_text] || 'view raw',
|
|
|
|
start: opts[:start] || 1,
|
|
|
|
end: opts[:end]
|
|
|
|
}
|
2012-12-23 01:38:01 -05:00
|
|
|
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-24 16:59:48 -05:00
|
|
|
@options[: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
|
2012-12-24 16:59:48 -05:00
|
|
|
filepath = code_path + @file
|
2011-06-15 18:31:22 -04:00
|
|
|
|
2012-06-03 01:13:39 -04:00
|
|
|
unless @title_old.nil?
|
2012-12-24 16:59:48 -05:00
|
|
|
@options[: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]"
|
2012-12-24 16:59:48 -05:00
|
|
|
puts "Update include for #{filepath}"
|
2012-06-03 01:13:39 -04:00
|
|
|
puts "### --------------------------------- ###"
|
|
|
|
end
|
2013-02-22 00:53:04 -05:00
|
|
|
|
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
|
|
|
|
|
2012-12-24 16:59:48 -05:00
|
|
|
unless filepath.file?
|
|
|
|
puts "File #{filepath} could not be found"
|
|
|
|
return "File #{filepath} could not be found"
|
2011-06-15 18:31:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Dir.chdir(code_path) do
|
2012-12-24 16:59:48 -05:00
|
|
|
@options[:lang] ||= filepath.extname.sub('.','')
|
|
|
|
@options[:title] = @options[:title] ? "#{@options[:title]} (#{filepath.basename})" : filepath.basename
|
|
|
|
@options[:url] ||= "/#{code_dir}/#{@file}"
|
|
|
|
|
|
|
|
code = filepath.read
|
|
|
|
code = get_range(code, @options[:start], @options[:end])
|
|
|
|
highlight(code, @options)
|
2011-06-15 18:31:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Liquid::Template.register_tag('include_code', Jekyll::IncludeCodeTag)
|