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

This commit is contained in:
Brandon Mathis 2011-08-16 02:40:47 -04:00
parent 59521e3db8
commit ef4a42f977
3 changed files with 21 additions and 13 deletions

View File

@ -64,7 +64,7 @@ module Jekyll
@file = $1 @file = $1
@caption = "<figcaption><span>#{$1}</span></figcaption>\n" @caption = "<figcaption><span>#{$1}</span></figcaption>\n"
end end
if @file =~ /\S[\S\s]*\.(\w+)/ if @file =~ /\S[\S\s]*\w+\.(\w+)/
@filetype = $1 @filetype = $1
end end
super super
@ -82,7 +82,7 @@ module Jekyll
@filetype = 'yaml' if @filetype == 'yml' @filetype = 'yaml' if @filetype == 'yml'
source += " #{highlight(code, @filetype)}</figure></div>" source += " #{highlight(code, @filetype)}</figure></div>"
else else
source += "<pre><code>" + code.lstrip.rstrip.gsub(/</,'&lt;') + "</code></pre></figure></div>" source += "#{tableize_code(code.lstrip.rstrip.gsub(/</,'&lt;'))}</figure></div>"
end end
source = source + context['pygments_suffix'] if context['pygments_suffix'] source = source + context['pygments_suffix'] if context['pygments_suffix']
end end

View File

@ -26,14 +26,17 @@ module OctopressFilters
# code snippet # code snippet
# ``` # ```
def backtick_codeblock(input) def backtick_codeblock(input)
code = nil
# Markdown support # Markdown support
input = input.gsub /<p>`{3}\s*(\w+)?<\/p>\s*<pre><code>\s*(.+?)\s*<\/code><\/pre>\s*<p>`{3}<\/p>/m do input = input.gsub /<p>`{3}\s*(\w+)?<\/p>\s*<pre><code>\s*(.+?)\s*<\/code><\/pre>\s*<p>`{3}<\/p>/m do
lang = $1 lang = $1
if lang != '' if lang != ''
str = $2.gsub('&lt;','<').gsub('&gt;','>').gsub('&amp;','&') str = $2.gsub('&lt;','<').gsub('&gt;','>').gsub('&amp;','&')
highlight(str, lang) code = highlight(str, lang)
"<figure role=code>#{code}</figure>"
else else
"<pre><code>#{$2}</code></pre>" code = tableize_code($2)
"<figure role=code>#{code}</figure>"
end end
end end
@ -48,9 +51,11 @@ module OctopressFilters
lang = $1 lang = $1
str = $2.gsub(/^\s{4}/, '') str = $2.gsub(/^\s{4}/, '')
if lang != '' if lang != ''
highlight(str, lang) code = highlight(str, lang)
"<figure role=code>#{code}</figure>"
else else
"<pre><code>#{$2.gsub('<','&lt;').gsub('>','&gt;')}</code></pre>" code = tableize_code($2.gsub('<','&lt;').gsub('>','&gt;'))
"<figure role=code>#{code}</figure>"
end end
end end
end end

View File

@ -8,13 +8,7 @@ FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
module HighlightCode module HighlightCode
def highlight(str, lang) def highlight(str, lang)
str = pygments(str, lang).match(/<pre>(.+)<\/pre>/m)[1].to_s.gsub(/ *$/, '') #strip out divs <div class="highlight"> 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">' tableize_code(str, lang)
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
def pygments(code, lang) def pygments(code, lang)
@ -31,4 +25,13 @@ module HighlightCode
end end
highlighted_code highlighted_code
end 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 end