Permalink
Browse files

get rid of Comment#body_with_links

Some of the substitution logic has been moved to Comment#body=
  • Loading branch information...
1 parent 145d678 commit 6d6f0e02ed3d3e4d445f79d5f0ecb058b10e430a @rtomayko rtomayko committed Jun 7, 2008
Showing with 29 additions and 10 deletions.
  1. +8 −7 lib/wink/models.rb
  2. +1 −1 lib/wink/web.rb
  3. +18 −0 test/comment_test.rb
  4. +1 −1 views/comments.haml
  5. +1 −1 views/entry.haml
View
@@ -262,7 +262,7 @@ class Comment
property :author, :string, :size => 80
property :ip, :string, :size => 50
property :url, :string, :size => 255
- property :body, :text, :nullable => false
+ property :body, :text, :nullable => false, :lazy => false
property :created_at, :datetime, :nullable => false, :index => true
property :referrer, :string, :size => 255
property :user_agent, :string, :size => 255
@@ -291,12 +291,13 @@ def excerpt(length=65)
body.to_s.gsub(/[\s\r\n]+/, ' ')[0..65] + " ..."
end
- def body_with_links
- body.to_s.
- gsub(/(^|[\s\t])(www\.\S+)/, '\1<http://\2>').
- gsub(/(?:^|[^\]])\((https?:\/\/[^)]+)\)/, '<\1>').
- gsub(/(^|[\s\t])(https?:\/\/\S+)/, '\1<\2>').
- gsub(/^(\s*)(#\d+)/) { [$1, "\\", $2].join }
+ def body=(text)
+ # the first sub autolinks URLs when on line by itself; the second sub
+ # disables escapes markdown's headings when followed by a number.
+ @body = text.to_s
+ @body.gsub!(/^https?:\/\/\S+$/, '<\&>')
+ @body.gsub!(/^(\s*)(#\d+)/) { [$1, "\\", $2].join }
+ @body.gsub!(/\r/, '')
end
def url
View
@@ -307,7 +307,7 @@ def wink
get '/comments/:id' do
comment = Comment[params[:id].to_i]
raise Sinatra::NotFound if comment.nil?
- content_filter(comment.body_with_links, :markdown)
+ content_filter(comment.body, :markdown)
end
post '/writings/:slug/comment' do
View
@@ -52,6 +52,24 @@
comment.author_link?.should.not.be.truthful
end
+ it 'autolinks URLs when -- and only when -- only thing on line' do
+ comment = Comment.new
+
+ comment.body = "foo\nhttp://example.com/\nbar"
+ comment.body.should.be == "foo\n<http://example.com/>\nbar"
+
+ comment.body = "foo\nbar: http://example.com/\nbar"
+ comment.body.should.be == "foo\nbar: http://example.com/\nbar"
+
+ comment = Comment.new(:body => "X\nhttp://foo\nX")
+ comment.body.should.be == "X\n<http://foo>\nX"
+ end
+
+ it 'escapes comment references to prevent markdown headings' do
+ comment = Comment.new(:body => "#35 - you're wrong about that!")
+ comment.body.should.be == "\\#35 - you're wrong about that!"
+ end
+
it 'finds only ham with ::ham' do
Comment.should.respond_to :ham
(1..10).each do |i|
View
@@ -20,7 +20,7 @@
&nbsp;on&nbsp;
= entry_ref(c.entry)
%div.body
- ~ sanitize(content_filter(c.body_with_links, :markdown))
+ ~ sanitize(content_filter(c.body, :markdown))
-if admin?
%form{:action=>"/comments/#{c.id}",:style=>'display:none'}
%div
View
@@ -34,7 +34,7 @@
- @comments.each do |c|
%li.comment.container{:id=>"comment-#{c.id}"}
.body
- ~ sanitize(content_filter(c.body_with_links, :markdown))
+ ~ sanitize(content_filter(c.body, :markdown))
-if admin?
%form{:action=>"/comments/#{c.id}",:style=>'display:none'}
%div

0 comments on commit 6d6f0e0

Please sign in to comment.