Merge branch 'ZBarg-feature/improve-post-title' into develop

This commit is contained in:
Jonne Haß 2013-07-31 14:59:51 +02:00
commit 77c94bf4c2
7 changed files with 95 additions and 9 deletions

View file

@ -21,6 +21,7 @@
* Show the user if a contact is sharing with them when viewing their profile page [#2948](https://github.com/diaspora/diaspora/issues/2948)
* Made Unicorn timeout configurable and increased the default to 90 seconds
* Follow DiasporaHQ upon account creation is now configurable to another account [#4278](https://github.com/diaspora/diaspora/pull/4278)
* Use first header as title in the single post view, when possible [#4256](https://github.com/diaspora/diaspora/pull/4256)
# 0.1.1.0

View file

@ -73,7 +73,12 @@ app.pages.PostViewer = app.views.Base.extend({
},
postRenderTemplate : function() {
if(this.model.get("title")){ document.title = this.model.get("title"); }
if(this.model.get("title")){
// formats title to html...
var html_title = app.helpers.textFormatter(this.model.get("title"), this.model);
//... and converts html to plain text
document.title = $('<div>').html(html_title).text();
}
},
commentAnywhere : function(evt) {

View file

@ -10,7 +10,18 @@ module PostsHelper
I18n.t "posts.show.reshare_by", :author => post.author_name
else
if post.text.present?
truncate(post.text(:plain_text => true), :length => opts.fetch(:length, 20))
if opts.has_key?(:length)
truncate(post.text(:plain_text => true), :length => opts.fetch(:length))
elsif /\A(?: # Regexp to match a Markdown header present on first line :
(?<setext_content>.{1,200}\n(?:={1,200}|-{1,200}))(?:\r?\n|$) # Setext-style header
| # or
(?<atx_content>\#{1,6}\s.{1,200})(?:\r?\n|$) # Atx-style header
)/x =~ post.text(:plain_text => true)
return setext_content unless setext_content.nil?
return atx_content unless atx_content.nil?
else
truncate(post.text(:plain_text => true), :length => 20 )
end
elsif post.respond_to?(:photos) && post.photos.present?
I18n.t "posts.show.photos_by", :count => post.photos.size, :author => post.author_name
end
@ -19,7 +30,7 @@ module PostsHelper
def post_iframe_url(post_id, opts={})
opts[:width] ||= 516
opts[:height] ||= 315
opts[:height] ||= 315
host = AppConfig.pod_uri.authority
"<iframe src='#{Rails.application.routes.url_helpers.post_url(post_id, :host => host)}' width='#{opts[:width]}px' height='#{opts[:height]}px' frameBorder='0'></iframe>".html_safe
end

View file

@ -1,4 +1,7 @@
class PostPresenter
include PostsHelper
include ActionView::Helpers::TextHelper
attr_accessor :post, :current_user
def initialize(post, current_user = nil)
@ -54,7 +57,7 @@ class PostPresenter
end
def title
@post.text.present? ? @post.text(:plain_text => true) : I18n.translate('posts.presenter.title', :name => @post.author_name)
@post.text.present? ? post_page_title(@post) : I18n.translate('posts.presenter.title', :name => @post.author_name)
end
def template_name #kill me, lol, I should be client side

View file

@ -5,6 +5,42 @@
require 'spec_helper'
describe PostsHelper do
describe '#post_page_title' do
before do
@sm = FactoryGirl.create(:status_message)
end
context 'with posts with text' do
context 'when :length is passed in parameters' do
it 'returns string of size less or equal to :length' do
@sm = stub(:text => "## My title\n Post content...")
string_size = 12
post_page_title(@sm, :length => string_size ).size.should <= string_size
end
end
context 'when :length is not passed in parameters' do
context 'with a Markdown header of less than 200 characters on first line'do
it 'returns atx style header' do
@sm = stub(:text => "## My title\n Post content...")
post_page_title(@sm).should == "## My title"
end
it 'returns setext style header' do
@sm = stub(:text => "My title \n======\n Post content...")
post_page_title(@sm).should == "My title \n======"
end
end
context 'without a Markdown header of less than 200 characters on first line 'do
it 'truncates posts to the 20 first characters' do
@sm = stub(:text => "Very, very, very long post")
post_page_title(@sm).should == "Very, very, very ..."
end
end
end
end
end
describe '#post_iframe_url' do
before do
@post = FactoryGirl.create(:status_message)

View file

@ -0,0 +1,14 @@
describe("app.Pages.PostViewer", function(){
describe("postRenderTemplate", function(){
beforeEach(function(){
app.setPreload('post', factory.post({frame_name : "note"}).attributes);
this.page = new app.pages.PostViewer({id : 2});
})
it('translates post title from Markdown to plain text and pushes it in document.title', function () {
this.page.model.set({title : "### My [Markdown](url) *title*" });
this.page.postRenderTemplate();
expect(document.title).toEqual("My Markdown title");
})
})
});

View file

@ -76,12 +76,28 @@ describe PostPresenter do
end
describe '#title' do
it 'includes the text if it is present' do
@sm = stub(:text => "lalalalalalala", :author => bob.person)
@presenter.post = @sm
@presenter.title.should == @sm.text
end
context 'with posts with text' do
context 'with a Markdown header of less than 200 characters on first line'do
it 'returns atx style header' do
@sm = stub(:text => "## My title\n Post content...")
@presenter.post = @sm
@presenter.title.should == "## My title"
end
it 'returns setext style header' do
@sm = stub(:text => "My title \n======\n Post content...")
@presenter.post = @sm
@presenter.title.should == "My title \n======"
end
end
context 'without a Markdown header of less than 200 characters on first line 'do
it 'truncates post to the 20 first characters' do
@sm = stub(:text => "Very, very, very long post")
@presenter.post = @sm
@presenter.title.should == "Very, very, very ..."
end
end
end
context 'with posts without text' do
it ' displays a messaage with the post class' do