Browse code

added support for pygments.rb removing dependency on pygments, added support for caching highlighted code from pygments.rb and added default line numbering. Javascript auto line numbering now only occurs for embedded gists

Brandon Mathis authored on 27/07/2011 at 03:36:42
Showing 8 changed files
... ...
@@ -48,8 +48,7 @@ function testFeatures() {
48 48
 
49 49
 function addCodeLineNumbers(){
50 50
   if (navigator.appName == 'Microsoft Internet Explorer') { return }
51
-  $('div.highlight pre code').each(function(el){ addDivLines(el); });
52
-  $('div.highlight, div.gist-highlight').each(function(code){
51
+  $('div.gist-highlight').each(function(code){
53 52
     var tableStart = '<table cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter">';
54 53
     var lineNumbers = '<pre class="line-numbers">';
55 54
     var tableMiddle = '</pre></td><td class="code" width="100%">';
... ...
@@ -62,16 +61,6 @@ function addCodeLineNumbers(){
62 62
     $(code).html(table);
63 63
   });
64 64
 }
65
-function addDivLines(el){
66
-  var content = $(el).html();
67
-  var lines = content.replace(/\s*$/g, '').split(/\n/);
68
-  var count = lines.length;
69
-  $(lines).each(function(line, index){
70
-    if(line == '') line = ' ';
71
-    lines[index] = '<div class="line">' + line + '</div>';
72
-  });
73
-  $(el).html(lines.join(''));
74
-}
75 65
 
76 66
 function flashVideoFallback(){
77 67
   var flashplayerlocation = "/assets/jwplayer/player.swf",
... ...
@@ -4,6 +4,7 @@ gem 'rake'
4 4
 gem 'rack'
5 5
 gem 'jekyll'
6 6
 gem 'rdiscount'
7
+gem 'pygments.rb'
7 8
 gem 'RedCloth'
8 9
 gem 'haml', '>= 3.1'
9 10
 gem 'compass', '>= 0.11'
... ...
@@ -4,6 +4,7 @@ GEM
4 4
     RedCloth (4.2.7)
5 5
     albino (1.3.3)
6 6
       posix-spawn (>= 0.3.6)
7
+    blankslate (2.1.2.4)
7 8
     chunky_png (1.2.0)
8 9
     classifier (1.3.3)
9 10
       fast-stemmer (>= 1.0.0)
... ...
@@ -13,6 +14,7 @@ GEM
13 13
       sass (~> 3.1)
14 14
     directory_watcher (1.4.0)
15 15
     fast-stemmer (1.0.0)
16
+    ffi (1.0.9)
16 17
     fssm (0.2.7)
17 18
     haml (3.1.2)
18 19
     jekyll (0.11.0)
... ...
@@ -27,12 +29,17 @@ GEM
27 27
     maruku (0.6.0)
28 28
       syntax (>= 1.0.0)
29 29
     posix-spawn (0.3.6)
30
-    rack (1.3.1)
30
+    pygments.rb (0.1.2)
31
+      rubypython (>= 0.5.1)
32
+    rack (1.3.2)
31 33
     rake (0.9.2)
32 34
     rb-fsevent (0.4.1)
33 35
     rdiscount (1.6.8)
34 36
     rubypants (0.2.0)
35
-    sass (3.1.4)
37
+    rubypython (0.5.1)
38
+      blankslate (>= 2.1.2.3)
39
+      ffi (~> 1.0.7)
40
+    sass (3.1.5)
36 41
     syntax (1.0.0)
37 42
 
38 43
 PLATFORMS
... ...
@@ -43,6 +50,7 @@ DEPENDENCIES
43 43
   compass (>= 0.11)
44 44
   haml (>= 3.1)
45 45
   jekyll
46
+  pygments.rb
46 47
   rack
47 48
   rake
48 49
   rb-fsevent
... ...
@@ -28,7 +28,7 @@ plugins: plugins
28 28
 code_dir: downloads/code
29 29
 category_dir: blog/categories
30 30
 markdown: rdiscount
31
-pygments: true
31
+pygments: false
32 32
 
33 33
 paginate: 10 # Posts per page on the blog index
34 34
 recent_posts: 5 # Posts in the sidebar Recent Posts section
... ...
@@ -41,9 +41,12 @@
41 41
 # <pre><code>&lt;sarcasm> Ooooh, sarcasm... How original!&lt;/sarcasm></code></pre>
42 42
 # </figure>
43 43
 #
44
+require './plugins/pygments_code'
45
+
44 46
 module Jekyll
45 47
 
46 48
   class CodeBlock < Liquid::Block
49
+    include HighlightCode
47 50
     CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)\s+(.+)/i
48 51
     CaptionUrl = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)/i
49 52
     Caption = /(\S[\S\s]*)/
... ...
@@ -75,7 +78,7 @@ module Jekyll
75 75
       if @filetype
76 76
         @filetype = 'objc' if @filetype == 'm'
77 77
         @filetype = 'perl' if @filetype == 'pl'
78
-        source += "{% highlight #{@filetype} %}\n" + code + "\n{% endhighlight %}</figure></div>"
78
+        source += " #{highlight(code, @filetype)}</figure></div>"
79 79
       else
80 80
         source += "<pre><code>" + code.lstrip.rstrip.gsub(/</,'&lt;') + "</code></pre></figure></div>"
81 81
       end
... ...
@@ -20,11 +20,13 @@
20 20
 # will output a figcaption with the title: Example 2 (test.js)
21 21
 #
22 22
 
23
+require './plugins/pygments_code'
23 24
 require 'pathname'
24 25
 
25 26
 module Jekyll
26 27
 
27 28
   class IncludeCodeTag < Liquid::Tag
29
+    include HighlightCode
28 30
     def initialize(tag_name, markup, tokens)
29 31
       @title = nil
30 32
       @file = nil
... ...
@@ -50,13 +52,13 @@ module Jekyll
50 50
 
51 51
       Dir.chdir(code_path) do
52 52
         code = file.read
53
-        @filetype = file.extname
53
+        @filetype = file.extname.sub('.','')
54 54
         @filetype = 'objc' if @filetype == 'm'
55 55
         @filetype = 'perl' if @filetype == 'pl'
56 56
         title = @title ? "#{@title} (#{file.basename})" : file.basename
57 57
         url = "#{context.registers[:site].config['url']}/#{code_dir}/#{@file}"
58 58
         source = "<div><figure role=code><figcaption><span>#{title}</span> <a href='#{url}'>download</a></figcaption>\n"
59
-        source += "{% highlight #{@filetype} %}\n" + code + "\n{% endhighlight %}</figure></div>"
59
+        source += " #{highlight(code, @filetype)}</figure></div>"
60 60
         partial = Liquid::Template.parse(source)
61 61
         context.stack do
62 62
           partial.render(context)
63 63
deleted file mode 100644
... ...
@@ -1,30 +0,0 @@
1
-#
2
-# Author: Raimonds Simanovskis, http://blog.rayapps.com/
3
-# Source URL: https://github.com/rsim/blog.rayapps.com/blob/master/_plugins/pygments_cache_patch.rb
4
-#
5
-
6
-require 'fileutils'
7
-require 'digest/md5'
8
-
9
-PYGMENTS_CACHE_DIR = File.expand_path('../../_code_cache', __FILE__)
10
-FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
11
-
12
-Jekyll::HighlightBlock.class_eval do
13
-  def render_pygments(context, code)
14
-    if defined?(PYGMENTS_CACHE_DIR)
15
-      path = File.join(PYGMENTS_CACHE_DIR, "#{@lang}-#{Digest::MD5.hexdigest(code)}.html")
16
-      if File.exist?(path)
17
-        highlighted_code = File.read(path)
18
-      else
19
-        highlighted_code = Albino.new(code, @lang).to_s(@options)
20
-        File.open(path, 'w') {|f| f.print(highlighted_code) }
21
-      end
22
-    else
23
-      highlighted_code = Albino.new(code, @lang).to_s(@options)
24
-    end
25
-    output = add_code_tags(highlighted_code, @lang)
26
-    output = context["pygments_prefix"] + output if context["pygments_prefix"]
27
-    output = output + context["pygments_suffix"] if context["pygments_suffix"]
28
-    output
29
-  end
30
-end
31 1
new file mode 100644
... ...
@@ -0,0 +1,34 @@
0
+require 'pygments'
1
+require 'fileutils'
2
+require 'digest/md5'
3
+
4
+PYGMENTS_CACHE_DIR = File.expand_path('../../_code_cache', __FILE__)
5
+FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
6
+
7
+module HighlightCode
8
+  def highlight(str, lang)
9
+    str = pygments(str, lang).match(/<pre>(.+)<\/pre>/m)[1].to_s.gsub(/\s*$/, '') #strip out divs <div class="highlight">
10
+    table = '<div class="highlight"><table cellpadding="0" cellspacing="0"><tr><td class="gutter"><pre class="line-numbers">'
11
+    code = ''
12
+    str.lines.each_with_index do |line,index|
13
+      table += "<span class='line'>#{index+1}</span>\n"
14
+      code  += "<div class='line'>#{line}</div>"
15
+    end
16
+    table += "</pre></td><td class='code' width='100%'><pre><code class='#{lang}'>#{code}</code></pre></td></tr></table></div>"
17
+  end
18
+
19
+  def pygments(code, lang)
20
+    if defined?(PYGMENTS_CACHE_DIR)
21
+      path = File.join(PYGMENTS_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest(code)}.html")
22
+      if File.exist?(path)
23
+        highlighted_code = File.read(path)
24
+      else
25
+        highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html')
26
+        File.open(path, 'w') {|f| f.print(highlighted_code) }
27
+      end
28
+    else
29
+      highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html')
30
+    end
31
+    highlighted_code
32
+  end
33
+end