Improvements to code plugins

- Range options are now properly stripped out of code plugin markup
- Markers now add start and end class names for more style control
- code_block plugin properly passes markings in highlight options
- Deprecated title before file name syntax for include_code
This commit is contained in:
Brandon Mathis 2012-06-03 00:13:39 -05:00
parent b867b0877d
commit cf0d5b80cf
3 changed files with 40 additions and 19 deletions

View File

@ -61,7 +61,7 @@ module Jekyll
@linenos = get_linenos(markup) @linenos = get_linenos(markup)
markup = replace_linenos(markup) markup = replace_linenos(markup)
@mark = get_marks(markup) @marks = get_marks(markup)
markup = replace_marks(markup) markup = replace_marks(markup)
@start = get_start(markup) @start = get_start(markup)

View File

@ -28,8 +28,9 @@ module Jekyll
class IncludeCodeTag < Liquid::Tag class IncludeCodeTag < Liquid::Tag
include HighlightCode include HighlightCode
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
@title = nil
@file = nil @file = nil
@title = nil
@title_old = nil
@lang = get_lang(markup) @lang = get_lang(markup)
markup = replace_lang(markup) markup = replace_lang(markup)
@ -48,12 +49,15 @@ module Jekyll
range = get_range(markup, @start, @end) range = get_range(markup, @start, @end)
@start = range[:start] @start = range[:start]
@end = range[:start] @end = range[:end]
markup = replace_range(markup) markup = replace_range(markup)
if markup.strip =~ /(.*)?(\s+|^)(\/*\S+)/i if markup.strip =~ /(^\S*\.\S+) *(.+)?/i
@title = $1 || nil @file = $1
@file = $3 @title = $2 || nil
elsif markup.strip =~ /(.*?)(\S*\.\S+)\Z/i # Title before file is deprecated in 2.1
@title_old = $1 || nil
@file = $2
end end
super super
end end
@ -63,11 +67,22 @@ module Jekyll
code_path = (Pathname.new(context.registers[:site].source) + code_dir).expand_path code_path = (Pathname.new(context.registers[:site].source) + code_dir).expand_path
file = code_path + @file file = code_path + @file
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
if File.symlink?(code_path) if File.symlink?(code_path)
puts "Code directory '#{code_path}' cannot be a symlink"
return "Code directory '#{code_path}' cannot be a symlink" return "Code directory '#{code_path}' cannot be a symlink"
end end
unless file.file? unless file.file?
puts "File #{file} could not be found"
return "File #{file} could not be found" return "File #{file} could not be found"
end end

View File

@ -73,7 +73,13 @@ module HighlightCode
table += "<td class='code'><pre><code class='#{lang}'>" table += "<td class='code'><pre><code class='#{lang}'>"
if marks.size if marks.size
code.lines.each_with_index do |line,index| code.lines.each_with_index do |line,index|
table += "<span class='line#{' marked' if marks.include? index + start}'>#{line}</span>" classes = 'line'
if marks.include? index + start
classes += ' marked'
classes += ' start' unless marks.include? index - 1 + start
classes += ' end' unless marks.include? index + 1 + start
end
table += "<span class='#{classes}'>#{line}</span>"
end end
else else
table += code.gsub /^((.+)?(\n?))/, '<span class=\'line\'>\1</span>' table += code.gsub /^((.+)?(\n?))/, '<span class=\'line\'>\1</span>'
@ -99,7 +105,7 @@ module HighlightCode
end end
def replace_lang (input) def replace_lang (input)
input.sub /\s*lang:\w+/i, '' input.sub(/ *lang:\w+/i, '')
end end
def get_marks (input) def get_marks (input)
@ -107,7 +113,7 @@ module HighlightCode
# Example input mark:1,5-10,2 # Example input mark:1,5-10,2
# Outputs: [1,2,5,6,7,8,9,10] # Outputs: [1,2,5,6,7,8,9,10]
marks = [] marks = []
if input =~ /\s*mark:(\d\S*)/i if input =~ / *mark:(\d\S*)/i
marks = $1.gsub /(\d+)-(\d+)/ do marks = $1.gsub /(\d+)-(\d+)/ do
($1.to_i..$2.to_i).to_a.join(',') ($1.to_i..$2.to_i).to_a.join(',')
end end
@ -117,53 +123,53 @@ module HighlightCode
end end
def replace_marks (input) def replace_marks (input)
input.sub(/\s*mark:\d\S*/i,'') input.sub(/ *mark:\d\S*/i,'')
end end
def get_linenos (input) def get_linenos (input)
linenos = true linenos = true
if input =~ /\s*linenos:false/i if input =~ / *linenos:false/i
linenos = false linenos = false
end end
linenos linenos
end end
def replace_linenos (input) def replace_linenos (input)
input.sub(/\s*linenos:false/i,'') input.sub(/ *linenos:false/i,'')
end end
def get_start (input) def get_start (input)
start = 1 start = 1
if input =~ /\s*start:(\d+)/i if input =~ / *start:(\d+)/i
start = $1.to_i start = $1.to_i
end end
start start
end end
def replace_start (input) def replace_start (input)
input.sub(/\s*start:\d+/i,'') input.sub(/ *start:\d+/i,'')
end end
def get_end (input) def get_end (input)
endline = nil endline = nil
if input =~ /\s*end:(\d+)/i if input =~ / *end:(\d+)/i
endline = $1.to_i endline = $1.to_i
end end
endline endline
end end
def replace_end (input) def replace_end (input)
input.sub(/\s*end:\d+/i,'') input.sub(/ *end:\d+/i,'')
end end
def get_range (input, start, endline) def get_range (input, start, endline)
if input =~ /\s*range:(\d+),(\d+)/i if input =~ / *range:(\d+)-(\d+)/i
start = $1.to_i start = $1.to_i
eneline = $2.to_i endline = $2.to_i
end end
{start: start, end: endline} {start: start, end: endline}
end end
def replace_range (input) def replace_range (input)
input.sub(/\s*range:\d+,\d+/i,'') input.sub(/ *range:\d+-\d+/i,'')
end end
end end