Skip to content

Commit

Permalink
Add support for textilize and coderay to Posts
Browse files Browse the repository at this point in the history
* Add RedCloth and coderay gems
* Write and satisfy tests for  textilize and coderay helpers
  • Loading branch information
mrDoktar committed May 11, 2010
1 parent d27594e commit 1252941
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ gem 'rails', '3.0.0.beta3'

gem 'haml', '>= 3.0.0'

gem 'RedCloth'
gem 'coderay'

gem 'sqlite3-ruby', :require => 'sqlite3'

group :test do
Expand Down
26 changes: 26 additions & 0 deletions app/helpers/posts_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
module PostsHelper

# Note that it marks content as html_safe!
def textilize_and_highlight(text)
textilize(code_highlight(text))
end

def code_highlight(text)
text.gsub(/\<code(?:(?:\s+title="([^"]+?)")|(?:\s+lang="([^"]+?)"))*\s*\>(.+?)\<\/code\>/m) do
if $1.present?
"\n" + content_tag(:notextile) do
content_tag(:div, :class => "code") do
content_tag(:h5, $1) + coderay($3, $2)
end
end + "\n"
else
content_tag(:notextile) do
coderay($3, $2, :span)
end
end
end
end

def coderay(text, lang, type = :div)
CodeRay.scan(text, lang).send(type, :css => :class).html_safe + " "
end

end
38 changes: 38 additions & 0 deletions spec/helpers/posts_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'spec_helper'

describe PostsHelper do

include PostsHelper
include ActionView::Helpers

attr_accessor :output_buffer

it "should textilize and color the post correctly" do
text = 'h1. My header
<code lang="javascript" title="My title">
<script>
var foo = "bar";
</script>
</code>
h2. More code
This is <code lang="html"><div></code> should be coloured.'

formatted_text = '<h1>My header</h1>
<div class="code"><h5>My title</h5><div class="CodeRay">
<div class="code"><pre>
<span class="ta">&lt;script&gt;</span>
var foo = &quot;bar&quot;;
<span class="ta">&lt;/script&gt;</span>
</pre></div>
</div>
</div><h2>More code</h2>
<p>This is <span class="CodeRay"><span class="ta">&lt;div&gt;</span></span> should be coloured.</p>'

response = textilize_and_highlight(text)

response.should == formatted_text
end
end

0 comments on commit 1252941

Please sign in to comment.