-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for textilize and coderay to Posts
* Add RedCloth and coderay gems * Write and satisfy tests for textilize and coderay helpers
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"><script></span> | ||
var foo = "bar"; | ||
<span class="ta"></script></span> | ||
</pre></div> | ||
</div> | ||
</div><h2>More code</h2> | ||
<p>This is <span class="CodeRay"><span class="ta"><div></span></span> should be coloured.</p>' | ||
|
||
response = textilize_and_highlight(text) | ||
|
||
response.should == formatted_text | ||
end | ||
end |