Browse code

Codeblock regex improved to better detect extensions fixes #96, added support for tableizing non highlighted code blocks from liquid codeblock tag and backtick code blocks

Brandon Mathis authored on 16/08/2011 at 06:40:47
Showing 3 changed files
... ...
@@ -64,7 +64,7 @@ module Jekyll
64 64
         @file = $1
65 65
         @caption = "<figcaption><span>#{$1}</span></figcaption>\n"
66 66
       end
67
-      if @file =~ /\S[\S\s]*\.(\w+)/
67
+      if @file =~ /\S[\S\s]*\w+\.(\w+)/
68 68
         @filetype = $1
69 69
       end
70 70
       super
... ...
@@ -82,7 +82,7 @@ module Jekyll
82 82
         @filetype = 'yaml' if @filetype == 'yml'
83 83
         source += " #{highlight(code, @filetype)}</figure></div>"
84 84
       else
85
-        source += "<pre><code>" + code.lstrip.rstrip.gsub(/</,'&lt;') + "</code></pre></figure></div>"
85
+        source += "#{tableize_code(code.lstrip.rstrip.gsub(/</,'&lt;'))}</figure></div>"
86 86
       end
87 87
       source = source + context['pygments_suffix'] if context['pygments_suffix']
88 88
     end
... ...
@@ -26,14 +26,17 @@ module OctopressFilters
26 26
   #     code snippet
27 27
   # ```
28 28
   def backtick_codeblock(input)
29
+    code = nil
29 30
     # Markdown support
30 31
     input = input.gsub /<p>`{3}\s*(\w+)?<\/p>\s*<pre><code>\s*(.+?)\s*<\/code><\/pre>\s*<p>`{3}<\/p>/m do
31 32
       lang = $1
32 33
       if lang != ''
33 34
         str  = $2.gsub('&lt;','<').gsub('&gt;','>').gsub('&amp;','&')
34
-        highlight(str, lang)
35
+        code = highlight(str, lang)
36
+        "<figure role=code>#{code}</figure>"
35 37
       else
36
-        "<pre><code>#{$2}</code></pre>"
38
+        code = tableize_code($2)
39
+        "<figure role=code>#{code}</figure>"
37 40
       end
38 41
     end
39 42
 
... ...
@@ -48,9 +51,11 @@ module OctopressFilters
48 48
       lang = $1
49 49
       str  = $2.gsub(/^\s{4}/, '')
50 50
       if lang != ''
51
-        highlight(str, lang)
51
+        code = highlight(str, lang)
52
+        "<figure role=code>#{code}</figure>"
52 53
       else
53
-        "<pre><code>#{$2.gsub('<','&lt;').gsub('>','&gt;')}</code></pre>"
54
+        code = tableize_code($2.gsub('<','&lt;').gsub('>','&gt;'))
55
+        "<figure role=code>#{code}</figure>"
54 56
       end
55 57
     end
56 58
   end
... ...
@@ -8,13 +8,7 @@ FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
8 8
 module HighlightCode
9 9
   def highlight(str, lang)
10 10
     str = pygments(str, lang).match(/<pre>(.+)<\/pre>/m)[1].to_s.gsub(/ *$/, '') #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>"
11
+    tableize_code(str, lang)
18 12
   end
19 13
 
20 14
   def pygments(code, lang)
... ...
@@ -31,4 +25,13 @@ module HighlightCode
31 31
     end
32 32
     highlighted_code
33 33
   end
34
+  def tableize_code (str, lang = '')
35
+    table = '<div class="highlight"><table cellpadding="0" cellspacing="0"><tr><td class="gutter"><pre class="line-numbers">'
36
+    code = ''
37
+    str.lines.each_with_index do |line,index|
38
+      table += "<span class='line'>#{index+1}</span>\n"
39
+      code  += "<div class='line'>#{line}</div>"
40
+    end
41
+    table += "</pre></td><td class='code' width='100%'><pre><code class='#{lang}'>#{code}</code></pre></td></tr></table></div>"
42
+  end
34 43
 end