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 2e51d76681
commit e4cd278e0c
3 changed files with 40 additions and 19 deletions

View File

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

View File

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

View File

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