Permalink
Browse files

plugin for captioned codeblocks using coderay highlighting

  • Loading branch information...
codebykat committed May 23, 2013
1 parent 23a74f1 commit 2f6c9615c02869dca5f52921ac5eb7e0b35a6427
Showing with 96 additions and 0 deletions.
  1. +96 −0 plugins/code_ray_block.rb
View
@@ -0,0 +1,96 @@
# Title: Simple CodeRay Blocks
# Author: Kat Hagan http://codebykat.com
# Description: Modified version of Brandon Mathis' code_block.rb that uses CodeRay for syntax highlighting, with figcaptions just like the original.
#
# Syntax:
# {% coderay [title] [url] [link text] %}
# code snippet
# {% endcoderay %}
#
# For syntax highlighting, put a file extension somewhere in the title. examples:
# {% coderay file.sh %}
# code snippet
# {% endcoderay %}
#
# {% coderay Time to be Awesome! (awesome.rb) %}
# code snippet
# {% endcoderay %}
#
# Example:
#
# {% coderay Got pain? painrelief.sh http://site.com/painrelief.sh Download it! %}
# $ rm -rf ~/PAIN
# {% endcoderay %}
#
# Output:
#
# <figure class='code'>
# <figcaption class='code-header'><span>Got pain? painrelief.sh</span> <a href="http://site.com/painrelief.sh">Download it!</a></figcaption>
# <div class="highlight"><pre><code class="sh">
# -- nicely escaped highlighted code --
# </code></pre></div>
# </figure>
#
# Example 2 (no syntax highlighting):
#
# {% coderay %}
# <sarcasm>Ooooh, sarcasm... How original!</sarcasm>
# {% endcoderay %}
#
# <figure class='code'>
# <pre><code>&lt;sarcasm> Ooooh, sarcasm... How original!&lt;/sarcasm></code></pre>
# </figure>
#
require './plugins/pygments_code'
require './plugins/raw'
module Jekyll
class CodeRayBlock < Liquid::Block
include HighlightCode
include TemplateWrapper
CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i
Caption = /(\S[\S\s]*)/
def initialize(tag_name, markup, tokens)
@title = nil
@caption = nil
@filetype = nil
@highlight = true
if markup =~ /\s*lang:(\S+)/i
@filetype = $1
markup = markup.sub(/\s*lang:(\S+)/i,'')
end
if markup =~ CaptionUrlTitle
@file = $1
@caption = "<figcaption class='code-header' style='margin-bottom:-5px;'><span>#{$1}</span><a href='#{$2}'>#{$3 || 'link'}</a></figcaption>"
elsif markup =~ Caption
@file = $1
@caption = "<figcaption class='code-header' style='margin-bottom:-5px;'><span>#{$1}</span></figcaption>\n"
end
if @file =~ /\S[\S\s]*\w+\.(\w+)/ && @filetype.nil?
@filetype = $1
end
super
end
def render(context)
output = super
code = super
source = "<figure class='code'>"
source += @caption if @caption
coderay_css = context.registers[:site].config['kramdown']['coderay']['coderay_css'].to_sym
if @filetype
source += " #{CodeRay.scan(code, @filetype).div(:css=>coderay_css)} </figure>"
else
source += " #{CodeRay.scan(code, :text).div(:css=>coderay_css)} </figure>"
end
source = safe_wrap(source)
source = context['pygments_prefix'] + source if context['pygments_prefix']
source = source + context['pygments_suffix'] if context['pygments_suffix']
source
end
end
end
Liquid::Template.register_tag('coderay', Jekyll::CodeRayBlock)

3 comments on commit 2f6c961

@blackat

This comment has been minimized.

Show comment
Hide comment
@blackat

blackat Sep 30, 2014

Hi,
I have installed your plugin but when I run rake generate I get

Liquid Exception: uninitialized constant Jekyll::CodeRayBlock::CodeRay in _posts/2014-09-09-fullstack

this is my code ray block
{% coderay %}
{
"name": "web-app-101",
"version": "0.0.1",
"dependencies": {
"express": "4.0.x"
},
"devDependencies": {

}

}
{% endcoderay %}

running octopress. It seems I have forgotten something, any clue?
Thanks a lot for your attention.

blackat replied Sep 30, 2014

Hi,
I have installed your plugin but when I run rake generate I get

Liquid Exception: uninitialized constant Jekyll::CodeRayBlock::CodeRay in _posts/2014-09-09-fullstack

this is my code ray block
{% coderay %}
{
"name": "web-app-101",
"version": "0.0.1",
"dependencies": {
"express": "4.0.x"
},
"devDependencies": {

}

}
{% endcoderay %}

running octopress. It seems I have forgotten something, any clue?
Thanks a lot for your attention.

@codebykat

This comment has been minimized.

Show comment
Hide comment
@codebykat

codebykat Oct 6, 2014

Owner

Hi! @blackat! This is a bit out of date and so, I am not surprised if it doesn't work with the most up-to-date versions of Jekyll/Coderay/Octopress. There are some comments / workarounds on the original post, did you look at that already? http://blog.codebykat.com/2013/05/23/gorgeous-octopress-codeblocks-with-coderay/

Owner

codebykat replied Oct 6, 2014

Hi! @blackat! This is a bit out of date and so, I am not surprised if it doesn't work with the most up-to-date versions of Jekyll/Coderay/Octopress. There are some comments / workarounds on the original post, did you look at that already? http://blog.codebykat.com/2013/05/23/gorgeous-octopress-codeblocks-with-coderay/

@blackat

This comment has been minimized.

Show comment
Hide comment
@blackat

blackat Oct 20, 2014

Hi @codebykat I have kept the standard markdown interpreter and I have changed the css to get a better syntax highlight and a better page layout, in this way I can update octopress without breaking any code.

Quite a lot of work I have done step by step but I guess it is more or less the time to find and apply workarounds ;-)

Thanks a lot for your time.

blackat replied Oct 20, 2014

Hi @codebykat I have kept the standard markdown interpreter and I have changed the css to get a better syntax highlight and a better page layout, in this way I can update octopress without breaking any code.

Quite a lot of work I have done step by step but I guess it is more or less the time to find and apply workarounds ;-)

Thanks a lot for your time.

Please sign in to comment.