Minor tweaks to importing first posts

This commit is contained in:
Robin Ward 2014-04-02 15:54:21 -04:00
parent 558a06a117
commit d1e7fa1c47
4 changed files with 38 additions and 10 deletions

View File

@ -76,9 +76,9 @@
<div class='cooked'>{{{cooked}}}</div>
{{#if view.showExpandButton}}
{{#if controller.loadingExpanded}}
<button class="btn" disabled>{{i18n loading}}</button>
<button class="btn expand-post" disabled>{{i18n loading}}</button>
{{else}}
<button {{action expandFirstPost this}} class='btn'>{{i18n post.show_full}}</button>
<button {{action expandFirstPost this}} class='btn expand-post'>{{i18n post.show_full}}</button>
{{/if}}
{{/if}}
{{view Discourse.PostMenuView postBinding="this" postViewBinding="view"}}

View File

@ -413,6 +413,9 @@ span.post-count {
opacity: .8;
}
button.expand-post {
margin-top: 10px;
}
#topic-footer-buttons {
.btn {

View File

@ -147,8 +147,9 @@ class PostsController < ApplicationController
post = find_post_from_params
content = Rails.cache.fetch("embed-topic:#{post.topic_id}", expires_in: 10.minutes) do
url = TopicEmbed.where(topic_id: post.topic_id).pluck(:embed_url).first
doc = TopicEmbed.find_remote(url)
doc.content
title, body = TopicEmbed.find_remote(url)
body << TopicEmbed.imported_from_html(url)
body
end
render json: {cooked: content}
rescue

View File

@ -10,6 +10,10 @@ class TopicEmbed < ActiveRecord::Base
url.downcase.sub(/\/$/, '').sub(/\-+/, '-')
end
def self.imported_from_html(url)
"\n<hr>\n<small>#{I18n.t('embed.imported_from', link: "<a href='#{url}'>#{url}</a>")}</small>\n"
end
# Import an article from a source (RSS/Atom/Other)
def self.import(user, url, title, contents)
return unless url =~ /^https?\:\/\//
@ -17,7 +21,7 @@ class TopicEmbed < ActiveRecord::Base
if SiteSetting.embed_truncate
contents = first_paragraph_from(contents)
end
contents << "\n<hr>\n<small>#{I18n.t('embed.imported_from', link: "<a href='#{url}'>#{url}</a>")}</small>\n"
contents << imported_from_html(url)
url = normalize_url(url)
@ -60,15 +64,35 @@ class TopicEmbed < ActiveRecord::Base
require 'ruby-readability'
url = normalize_url(url)
Readability::Document.new(open(url).read,
tags: %w[div p code pre h1 h2 h3 b em i strong a img ul li ol],
attributes: %w[href src])
original_uri = URI.parse(url)
doc = Readability::Document.new(open(url).read,
tags: %w[div p code pre h1 h2 h3 b em i strong a img ul li ol blockquote],
attributes: %w[href src],
remove_empty_nodes: false)
tags = {'img' => 'src', 'script' => 'src', 'a' => 'href'}
title = doc.title
doc = Nokogiri::HTML(doc.content)
doc.search(tags.keys.join(',')).each do |node|
url_param = tags[node.name]
src = node[url_param]
unless (src.empty?)
uri = URI.parse(src)
unless uri.host
uri.scheme = original_uri.scheme
uri.host = original_uri.host
node[url_param] = uri.to_s
end
end
end
[title, doc.to_html]
end
def self.import_remote(user, url, opts=nil)
opts = opts || {}
doc = find_remote(url)
TopicEmbed.import(user, url, opts[:title] || doc.title, doc.content)
title, body = find_remote(url)
TopicEmbed.import(user, url, opts[:title] || title, body)
end
# Convert any relative URLs to absolute. RSS is annoying for this.