From e4cd278e0c130ffd318bd6c8b792e256c0f924e8 Mon Sep 17 00:00:00 2001 From: Brandon Mathis Date: Sun, 3 Jun 2012 00:13:39 -0500 Subject: [PATCH] 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 --- plugins/code_block.rb | 2 +- plugins/include_code.rb | 25 ++++++++++++++++++++----- plugins/pygments_code.rb | 32 +++++++++++++++++++------------- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/plugins/code_block.rb b/plugins/code_block.rb index 8cbb7ad..7d42234 100644 --- a/plugins/code_block.rb +++ b/plugins/code_block.rb @@ -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) diff --git a/plugins/include_code.rb b/plugins/include_code.rb index 94eb534..1e4a5c1 100644 --- a/plugins/include_code.rb +++ b/plugins/include_code.rb @@ -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 diff --git a/plugins/pygments_code.rb b/plugins/pygments_code.rb index 86b6cd6..fe828ac 100644 --- a/plugins/pygments_code.rb +++ b/plugins/pygments_code.rb @@ -73,7 +73,13 @@ module HighlightCode table += "
"
     if marks.size
       code.lines.each_with_index do |line,index|
-        table += "#{line}"
+        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 += "#{line}"
       end
     else
       table += code.gsub /^((.+)?(\n?))/, '\1'
@@ -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