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> <div class='cooked'>{{{cooked}}}</div>
{{#if view.showExpandButton}} {{#if view.showExpandButton}}
{{#if controller.loadingExpanded}} {{#if controller.loadingExpanded}}
<button class="btn" disabled>{{i18n loading}}</button> <button class="btn expand-post" disabled>{{i18n loading}}</button>
{{else}} {{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}}
{{/if}} {{/if}}
{{view Discourse.PostMenuView postBinding="this" postViewBinding="view"}} {{view Discourse.PostMenuView postBinding="this" postViewBinding="view"}}

View File

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

View File

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

View File

@ -10,6 +10,10 @@ class TopicEmbed < ActiveRecord::Base
url.downcase.sub(/\/$/, '').sub(/\-+/, '-') url.downcase.sub(/\/$/, '').sub(/\-+/, '-')
end 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) # Import an article from a source (RSS/Atom/Other)
def self.import(user, url, title, contents) def self.import(user, url, title, contents)
return unless url =~ /^https?\:\/\// return unless url =~ /^https?\:\/\//
@ -17,7 +21,7 @@ class TopicEmbed < ActiveRecord::Base
if SiteSetting.embed_truncate if SiteSetting.embed_truncate
contents = first_paragraph_from(contents) contents = first_paragraph_from(contents)
end 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) url = normalize_url(url)
@ -60,15 +64,35 @@ class TopicEmbed < ActiveRecord::Base
require 'ruby-readability' require 'ruby-readability'
url = normalize_url(url) url = normalize_url(url)
Readability::Document.new(open(url).read, original_uri = URI.parse(url)
tags: %w[div p code pre h1 h2 h3 b em i strong a img ul li ol], doc = Readability::Document.new(open(url).read,
attributes: %w[href src]) 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 end
def self.import_remote(user, url, opts=nil) def self.import_remote(user, url, opts=nil)
opts = opts || {} opts = opts || {}
doc = find_remote(url) title, body = find_remote(url)
TopicEmbed.import(user, url, opts[:title] || doc.title, doc.content) TopicEmbed.import(user, url, opts[:title] || title, body)
end end
# Convert any relative URLs to absolute. RSS is annoying for this. # Convert any relative URLs to absolute. RSS is annoying for this.