diff options
author | Brandon Mathis <brandon@imathis.com> | 2011-08-16 02:40:47 -0400 |
---|---|---|
committer | Brandon Mathis <brandon@imathis.com> | 2011-08-16 02:41:28 -0400 |
commit | ef4a42f9774ca36c8cf9921aadda93572c26fba6 (patch) | |
tree | a1376cabeea8613f1a0ff4200d5f5a67c0e903d9 | |
parent | 59521e3db81b36f8abe8c61802f5acf74d15ebfe (diff) | |
download | my_new_personal_website-ef4a42f9774ca36c8cf9921aadda93572c26fba6.tar.xz my_new_personal_website-ef4a42f9774ca36c8cf9921aadda93572c26fba6.zip |
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
-rw-r--r-- | plugins/code_block.rb | 4 | ||||
-rw-r--r-- | plugins/octopress_filters.rb | 13 | ||||
-rw-r--r-- | plugins/pygments_code.rb | 17 |
3 files changed, 21 insertions, 13 deletions
diff --git a/plugins/code_block.rb b/plugins/code_block.rb index ff242aab..55267a36 100644 --- a/plugins/code_block.rb +++ b/plugins/code_block.rb @@ -64,7 +64,7 @@ module Jekyll @file = $1 @caption = "<figcaption><span>#{$1}</span></figcaption>\n" end - if @file =~ /\S[\S\s]*\.(\w+)/ + if @file =~ /\S[\S\s]*\w+\.(\w+)/ @filetype = $1 end super @@ -82,7 +82,7 @@ module Jekyll @filetype = 'yaml' if @filetype == 'yml' source += " #{highlight(code, @filetype)}</figure></div>" else - source += "<pre><code>" + code.lstrip.rstrip.gsub(/</,'<') + "</code></pre></figure></div>" + source += "#{tableize_code(code.lstrip.rstrip.gsub(/</,'<'))}</figure></div>" end source = source + context['pygments_suffix'] if context['pygments_suffix'] end diff --git a/plugins/octopress_filters.rb b/plugins/octopress_filters.rb index 9c94f0e2..1170f8b6 100644 --- a/plugins/octopress_filters.rb +++ b/plugins/octopress_filters.rb @@ -26,14 +26,17 @@ module OctopressFilters # code snippet # ``` def backtick_codeblock(input) + code = nil # Markdown support input = input.gsub /<p>`{3}\s*(\w+)?<\/p>\s*<pre><code>\s*(.+?)\s*<\/code><\/pre>\s*<p>`{3}<\/p>/m do lang = $1 if lang != '' str = $2.gsub('<','<').gsub('>','>').gsub('&','&') - highlight(str, lang) + code = highlight(str, lang) + "<figure role=code>#{code}</figure>" else - "<pre><code>#{$2}</code></pre>" + code = tableize_code($2) + "<figure role=code>#{code}</figure>" end end @@ -48,9 +51,11 @@ module OctopressFilters lang = $1 str = $2.gsub(/^\s{4}/, '') if lang != '' - highlight(str, lang) + code = highlight(str, lang) + "<figure role=code>#{code}</figure>" else - "<pre><code>#{$2.gsub('<','<').gsub('>','>')}</code></pre>" + code = tableize_code($2.gsub('<','<').gsub('>','>')) + "<figure role=code>#{code}</figure>" end end end diff --git a/plugins/pygments_code.rb b/plugins/pygments_code.rb index cdd6c3a4..1930ec83 100644 --- a/plugins/pygments_code.rb +++ b/plugins/pygments_code.rb @@ -8,13 +8,7 @@ FileUtils.mkdir_p(PYGMENTS_CACHE_DIR) module HighlightCode def highlight(str, lang) str = pygments(str, lang).match(/<pre>(.+)<\/pre>/m)[1].to_s.gsub(/ *$/, '') #strip out divs <div class="highlight"> - table = '<div class="highlight"><table cellpadding="0" cellspacing="0"><tr><td class="gutter"><pre class="line-numbers">' - code = '' - str.lines.each_with_index do |line,index| - table += "<span class='line'>#{index+1}</span>\n" - code += "<div class='line'>#{line}</div>" - end - table += "</pre></td><td class='code' width='100%'><pre><code class='#{lang}'>#{code}</code></pre></td></tr></table></div>" + tableize_code(str, lang) end def pygments(code, lang) @@ -31,4 +25,13 @@ module HighlightCode end highlighted_code end + def tableize_code (str, lang = '') + table = '<div class="highlight"><table cellpadding="0" cellspacing="0"><tr><td class="gutter"><pre class="line-numbers">' + code = '' + str.lines.each_with_index do |line,index| + table += "<span class='line'>#{index+1}</span>\n" + code += "<div class='line'>#{line}</div>" + end + table += "</pre></td><td class='code' width='100%'><pre><code class='#{lang}'>#{code}</code></pre></td></tr></table></div>" + end end |