Browse files

Backport changes from the 2.3 branch (TidyFFI)

  • Loading branch information...
1 parent 37bd712 commit 6e8299262c9771a69c8ed51f80c90bd3bf67c378 @calexicoz calexicoz committed Mar 21, 2012
Showing with 45 additions and 22 deletions.
  1. +1 −1 Rakefile
  2. +8 −8 lib/model_formatting.rb
  3. +27 −5 test/formatting_test.rb
  4. +8 −8 test/model_formatting_test.rb
  5. +1 −0 test/test_helper.rb
View
2 Rakefile
@@ -16,7 +16,7 @@ spec = Gem::Specification.new do |s|
s.add_dependency("actionpack", "~>3.0.0")
s.add_dependency("activerecord", "~>3.0.0")
s.add_dependency("activesupport", "~>3.0.0")
- s.add_dependency("tidy", "~>1.1.2")
+ s.add_dependency("tidy_ffi", "~>0.1.2")
s.add_development_dependency("jeremymcanally-context", "~>0.5.5")
s.add_development_dependency("jeremymcanally-matchy", "~>0.1.0")
end
View
16 lib/model_formatting.rb
@@ -198,17 +198,17 @@ def self.process_markdown(text)
end
begin
- require 'tidy'
- Tidy.path = ENV['TIDY_PATH'] unless ENV['TIDY_PATH'].blank?
+ require 'tidy_ffi'
def self.process_tidy(text)
- return text unless Tidy.path
- Tidy.open(:show_body_only => true, :input_encoding => :utf8) do |tidy|
- tidy.options.new_inline_tags = "video"
- tidy.clean(text)
- end
+ tidy = TidyFFI::Tidy.new(text)
+ tidy.options.input_encoding = 'utf8'
+ tidy.options.show_body_only = true
+ tidy.options.new_inline_tags = "video"
+ tidy.options.force_output = true
+ tidy.clean.strip
end
rescue LoadError
- puts "No Tidy gem found. `gem install tidy`. Don't forget to set Tidy.path."
+ puts "No TidyFFI gem found. `gem install tidy_ffi`."
def self.process_tidy(text)
text
end
View
32 test/formatting_test.rb
@@ -1,4 +1,5 @@
-require File.dirname(__FILE__) + '/test_helper'
+# encoding: utf-8
+require File.expand_path('../test_helper', __FILE__)
module ModelFormatting
class Test < Test::Unit::TestCase
@@ -38,6 +39,27 @@ class Simple < Base
record.save
record.formatted_body.should == %(<div><p>booya</p></div>)
end
+
+ it "bolds the string 'Name'" do
+ record = Simple.new
+ record.body = "My name is __Name__"
+ record.save
+ record.formatted_body.should == %(<div><p>My name is <strong>Name</strong></p></div>)
+ end
+
+ it "preserves leading spaces in code blocks" do
+ record = Simple.new
+ record.body = "this\n\n code\n more code\n\nnot code\n\n"
+ record.save
+ record.formatted_body.should == %(<div><p>this</p>\n<pre>\n<code>code\nmore code</code>\n</pre>\n<p>not code</p></div>)
+ end
+
+ it "converts unicode characters to html entities" do
+ record = Simple.new
+ record.body = "Encöded ɐ \\Upload \\upload"
+ record.save
+ record.formatted_body.should ==%(<div><p>Enc&ouml;ded &#592; \\Upload \\upload</p></div>)
+ end
end
class BaseWithAfter < Base
@@ -94,19 +116,19 @@ class Post < Base
end
describe "being saved" do
- before :all do
+ before do
@record = Post.new
@record.body = 'booya'
@record.title = 'wtf'
@record.save
end
it "formats #body" do
- @record.formatted_body.should == %(<div>(<p>ayoob</p>)</div>)
+ @record.formatted_body.should == %(<div>(\n<p>ayoob</p>\n)</div>)
end
it "formats #title" do
- @record.title_html.should == %(<div>(<p>ftw</p>)</div>)
+ @record.title_html.should == %(<div>(\n<p>ftw</p>\n)</div>)
end
it "formats #bio" do
@@ -148,7 +170,7 @@ class ChildPost < Post
end
describe "being saved" do
- before :all do
+ before do
@record = ChildPost.new
@record.body = 'booya'
@record.bio = 'wtf'
View
16 test/model_formatting_test.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/test_helper'
+require File.expand_path('../test_helper', __FILE__)
class ModelFormattingTest < Test::Unit::TestCase
it "parses simple string into array of single FormattedBlock" do
@@ -55,27 +55,27 @@ class ModelFormattingTest < Test::Unit::TestCase
end
it "doesn't remove lines with pipes" do
- ModelFormatting.process(:html, "Yo.\n\nhere is a | pipe\n\nfoobaz").should == %(<div><p>Yo.</p>\n\n<p>here is a | pipe</p>\n\n<p>foobaz</p></div>)
+ ModelFormatting.process(:html, "Yo.\n\nhere is a | pipe\n\nfoobaz").should == %(<div><p>Yo.</p>\n<p>here is a | pipe</p>\n<p>foobaz</p></div>)
end
it "doesn't remove duplicate pre blocks" do
- ModelFormatting.process(:html, "Yes! <pre>tacos</pre> are great.\n\nI love to eat <pre>tacos</pre> topped with <pre>tacos</pre>.\n\nYes.").should == %(<div><p>Yes! <pre>tacos</pre> are great.</p>\n\n<p>I love to eat <pre>tacos</pre> topped with <pre>tacos</pre>.</p>\n\n<p>Yes.</p></div>)
+ ModelFormatting.process(:html, "Yes! <pre>tacos</pre> are great.\n\nI love to eat <pre>tacos</pre> topped with <pre>tacos</pre>.\n\nYes.").should == %(<div><p>Yes!</p>\n<pre>\ntacos\n</pre>\nare great.\n<p>I love to eat</p>\n<pre>\ntacos\n</pre>\ntopped with\n<pre>\ntacos\n</pre>\n.\n<p>Yes.</p></div>)
end
it "links and encodes urls correctly" do
- ModelFormatting.process(:html, "a *b* \n[Whoo](http://entp.com?a=1&b=2)").should == %(<div><p>a <em>b</em><br/>\n<a href="http://entp.com?a=1&amp;b=2">Whoo</a></p></div>)
+ ModelFormatting.process(:html, "a *b* \n[Whoo](http://entp.com?a=1&b=2)").should == %(<div><p>a <em>b</em><br>\n<a href=\"http://entp.com?a=1&amp;b=2\">Whoo</a></p></div>)
end
it "converts @@@ to code blocks" do
- ModelFormatting.process(:html, "<a>foo</a>\n\n@@@\n<a>bar</a>\n@@@\n\n@@@\nbaz\n@@@\n\n@@@ wah wah \n \n").should == %(<div><p><a>foo</a></p>\n\n<pre><code>&lt;a&gt;bar&lt;/a&gt;</code>\n</pre>\n\n\n\n\n<pre><code>baz</code>\n</pre>\n\n\n<p>@@@ wah wah</p></div>)
+ ModelFormatting.process(:html, "<a>foo</a>\n\n@@@\n<a>bar</a>\n@@@\n\n@@@\nbaz\n@@@\n\n@@@ wah wah \n \n").should == %(<div><p><a>foo</a></p>\n<pre>\n<code>&lt;a&gt;bar&lt;/a&gt;</code>\n</pre>\n<pre>\n<code>baz</code>\n</pre>\n<p>@@@ wah wah</p></div>)
end
it "converts @@@ with params to code blocks" do
- ModelFormatting.process(:html, "foo\n@@@ ninja\nbar\n@@@\n@@@\nbaz\n@@@\n@@@ wah wah \n \n").should == %(<div><p>foo</p>\n\n<pre><code class=\"ninja\">bar</code>\n</pre>\n\n\n<pre><code>baz</code>\n</pre>\n\n\n<p>@@@ wah wah</p></div>)
+ ModelFormatting.process(:html, "foo\n@@@ ninja\nbar\n@@@\n@@@\nbaz\n@@@\n@@@ wah wah \n \n").should == "<div><p>foo</p>\n<pre>\n<code class=\"ninja\">bar</code>\n</pre>\n<pre>\n<code>baz</code>\n</pre>\n<p>@@@ wah wah</p></div>"
end
it "fixes irregular number of @@@'s" do
- ModelFormatting.process(:html, "foo\n@@@\nbar\n@@@\n@@@\nbaz\n@@@\n@@@ wah wah \n \n@@@").should == %(<div><p>foo</p>\n\n<pre><code>bar</code>\n</pre>\n\n\n<pre><code>baz</code>\n</pre>\n\n\n<p>@@@ wah wah</p>\n\n<pre><code></code>\n</pre></div>)
+ ModelFormatting.process(:html, "foo\n@@@\nbar\n@@@\n@@@\nbaz\n@@@\n@@@ wah wah \n \n@@@").should == "<div><p>foo</p>\n<pre>\n<code>bar</code>\n</pre>\n<pre>\n<code>baz</code>\n</pre>\n<p>@@@ wah wah</p>\n<pre>\n\n</pre></div>"
end
it "converts @@@ with params to code blocks with text format" do
@@ -88,7 +88,7 @@ class ModelFormattingTest < Test::Unit::TestCase
it "treats linebreaks correctly" do
ModelFormatting.process(:html, "Line breaks should not be treated as\nnew paragraphs. They are not paragraphs.\n\nHowever, when a line is skipped, that is a paragraph.\nGMail, and basically every comment or submission form on the \nweb work this way.").should == \
- "<div><p>Line breaks should not be treated as\nnew paragraphs. They are not paragraphs.</p>\n\n<p>However, when a line is skipped, that is a paragraph.\nGMail, and basically every comment or submission form on the\nweb work this way.</p></div>"
+ "<div><p>Line breaks should not be treated as new paragraphs. They are\nnot paragraphs.</p>\n<p>However, when a line is skipped, that is a paragraph. GMail, and\nbasically every comment or submission form on the web work this\nway.</p></div>"
end
describe "GFM" do
View
1 test/test_helper.rb
@@ -3,6 +3,7 @@
require 'rubygems'
require 'action_controller'
require 'action_view'
+require 'active_support/core_ext'
require 'model_formatting'
require 'model_formatting/init'
require 'model_formatting/config'

0 comments on commit 6e82992

Please sign in to comment.