Skip to content

Commit 7b81aab

Browse files
committed
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
1 parent 727a149 commit 7b81aab

8 files changed

Lines changed: 55 additions & 48 deletions

File tree

.themes/classic/source/javascripts/octopress.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ function testFeatures() {
4848

4949
function addCodeLineNumbers(){
5050
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){
5352
var tableStart = '<table cellpadding="0" cellspacing="0"><tbody><tr><td class="gutter">';
5453
var lineNumbers = '<pre class="line-numbers">';
5554
var tableMiddle = '</pre></td><td class="code" width="100%">';
@@ -62,16 +61,6 @@ function addCodeLineNumbers(){
6261
$(code).html(table);
6362
});
6463
}
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-
}
7564

7665
function flashVideoFallback(){
7766
var flashplayerlocation = "/assets/jwplayer/player.swf",

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ gem 'rake'
44
gem 'rack'
55
gem 'jekyll'
66
gem 'rdiscount'
7+
gem 'pygments.rb'
78
gem 'RedCloth'
89
gem 'haml', '>= 3.1'
910
gem 'compass', '>= 0.11'

Gemfile.lock

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ GEM
44
RedCloth (4.2.7)
55
albino (1.3.3)
66
posix-spawn (>= 0.3.6)
7+
blankslate (2.1.2.4)
78
chunky_png (1.2.0)
89
classifier (1.3.3)
910
fast-stemmer (>= 1.0.0)
@@ -13,6 +14,7 @@ GEM
1314
sass (~> 3.1)
1415
directory_watcher (1.4.0)
1516
fast-stemmer (1.0.0)
17+
ffi (1.0.9)
1618
fssm (0.2.7)
1719
haml (3.1.2)
1820
jekyll (0.11.0)
@@ -27,12 +29,17 @@ GEM
2729
maruku (0.6.0)
2830
syntax (>= 1.0.0)
2931
posix-spawn (0.3.6)
30-
rack (1.3.1)
32+
pygments.rb (0.1.2)
33+
rubypython (>= 0.5.1)
34+
rack (1.3.2)
3135
rake (0.9.2)
3236
rb-fsevent (0.4.1)
3337
rdiscount (1.6.8)
3438
rubypants (0.2.0)
35-
sass (3.1.4)
39+
rubypython (0.5.1)
40+
blankslate (>= 2.1.2.3)
41+
ffi (~> 1.0.7)
42+
sass (3.1.5)
3643
syntax (1.0.0)
3744

3845
PLATFORMS
@@ -43,6 +50,7 @@ DEPENDENCIES
4350
compass (>= 0.11)
4451
haml (>= 3.1)
4552
jekyll
53+
pygments.rb
4654
rack
4755
rake
4856
rb-fsevent

_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ plugins: plugins
2828
code_dir: downloads/code
2929
category_dir: blog/categories
3030
markdown: rdiscount
31-
pygments: true
31+
pygments: false
3232

3333
paginate: 10 # Posts per page on the blog index
3434
recent_posts: 5 # Posts in the sidebar Recent Posts section

plugins/code_block.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@
4141
# <pre><code>&lt;sarcasm> Ooooh, sarcasm... How original!&lt;/sarcasm></code></pre>
4242
# </figure>
4343
#
44+
require './plugins/pygments_code'
45+
4446
module Jekyll
4547

4648
class CodeBlock < Liquid::Block
49+
include HighlightCode
4750
CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)\s+(.+)/i
4851
CaptionUrl = /(\S[\S\s]*)\s+(https?:\/\/)(\S+)/i
4952
Caption = /(\S[\S\s]*)/
@@ -75,7 +78,7 @@ def render(context)
7578
if @filetype
7679
@filetype = 'objc' if @filetype == 'm'
7780
@filetype = 'perl' if @filetype == 'pl'
78-
source += "{% highlight #{@filetype} %}\n" + code + "\n{% endhighlight %}</figure></div>"
81+
source += " #{highlight(code, @filetype)}</figure></div>"
7982
else
8083
source += "<pre><code>" + code.lstrip.rstrip.gsub(/</,'&lt;') + "</code></pre></figure></div>"
8184
end

plugins/include_code.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
# will output a figcaption with the title: Example 2 (test.js)
2121
#
2222

23+
require './plugins/pygments_code'
2324
require 'pathname'
2425

2526
module Jekyll
2627

2728
class IncludeCodeTag < Liquid::Tag
29+
include HighlightCode
2830
def initialize(tag_name, markup, tokens)
2931
@title = nil
3032
@file = nil
@@ -50,13 +52,13 @@ def render(context)
5052

5153
Dir.chdir(code_path) do
5254
code = file.read
53-
@filetype = file.extname
55+
@filetype = file.extname.sub('.','')
5456
@filetype = 'objc' if @filetype == 'm'
5557
@filetype = 'perl' if @filetype == 'pl'
5658
title = @title ? "#{@title} (#{file.basename})" : file.basename
5759
url = "#{context.registers[:site].config['url']}/#{code_dir}/#{@file}"
5860
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>"
61+
source += " #{highlight(code, @filetype)}</figure></div>"
6062
partial = Liquid::Template.parse(source)
6163
context.stack do
6264
partial.render(context)

plugins/pygments_cache_patch.rb

Lines changed: 0 additions & 30 deletions
This file was deleted.

plugins/pygments_code.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'pygments'
2+
require 'fileutils'
3+
require 'digest/md5'
4+
5+
PYGMENTS_CACHE_DIR = File.expand_path('../../_code_cache', __FILE__)
6+
FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
7+
8+
module HighlightCode
9+
def highlight(str, lang)
10+
str = pygments(str, lang).match(/<pre>(.+)<\/pre>/m)[1].to_s.gsub(/\s*$/, '') #strip out divs <div class="highlight">
11+
table = '<div class="highlight"><table cellpadding="0" cellspacing="0"><tr><td class="gutter"><pre class="line-numbers">'
12+
code = ''
13+
str.lines.each_with_index do |line,index|
14+
table += "<span class='line'>#{index+1}</span>\n"
15+
code += "<div class='line'>#{line}</div>"
16+
end
17+
table += "</pre></td><td class='code' width='100%'><pre><code class='#{lang}'>#{code}</code></pre></td></tr></table></div>"
18+
end
19+
20+
def pygments(code, lang)
21+
if defined?(PYGMENTS_CACHE_DIR)
22+
path = File.join(PYGMENTS_CACHE_DIR, "#{lang}-#{Digest::MD5.hexdigest(code)}.html")
23+
if File.exist?(path)
24+
highlighted_code = File.read(path)
25+
else
26+
highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html')
27+
File.open(path, 'w') {|f| f.print(highlighted_code) }
28+
end
29+
else
30+
highlighted_code = Pygments.highlight(code, :lexer => lang, :formatter => 'html')
31+
end
32+
highlighted_code
33+
end
34+
end

0 commit comments

Comments
 (0)