FEATURE: whitelist and blacklist selectors for embeds

This commit is contained in:
Sam 2014-04-15 14:06:51 +10:00
parent a49b38ac35
commit e5f41b8a21
5 changed files with 21 additions and 7 deletions

View file

@ -215,7 +215,11 @@ gem 'puma', require: false
gem 'rbtrace', require: false, platform: :mri gem 'rbtrace', require: false, platform: :mri
# required for feed importing and embedding # required for feed importing and embedding
gem 'ruby-readability', require: false #
# temporary when this is merged https://github.com/cantino/ruby-readability/pull/70
# get rid of this
gem 'ruby-readability-discourse', require: false
gem 'simple-rss', require: false gem 'simple-rss', require: false
gem 'gctools', require: false, platform: :mri_21 gem 'gctools', require: false, platform: :mri_21
gem 'stackprof', require: false, platform: :mri_21 gem 'stackprof', require: false, platform: :mri_21

View file

@ -298,7 +298,7 @@ GEM
rspec-mocks (~> 2.14.0) rspec-mocks (~> 2.14.0)
ruby-hmac (0.4.0) ruby-hmac (0.4.0)
ruby-openid (2.5.0) ruby-openid (2.5.0)
ruby-readability (0.6.0) ruby-readability-discourse (0.6.1)
guess_html_encoding (>= 0.0.4) guess_html_encoding (>= 0.0.4)
nokogiri (>= 1.4.2) nokogiri (>= 1.4.2)
sanitize (2.1.0) sanitize (2.1.0)
@ -457,7 +457,7 @@ DEPENDENCIES
rinku rinku
rspec-given rspec-given
rspec-rails rspec-rails
ruby-readability ruby-readability-discourse
sanitize sanitize
sass sass
sass-rails (~> 4.0.2) sass-rails (~> 4.0.2)

View file

@ -64,10 +64,16 @@ class TopicEmbed < ActiveRecord::Base
url = normalize_url(url) url = normalize_url(url)
original_uri = URI.parse(url) original_uri = URI.parse(url)
doc = Readability::Document.new(open(url).read, opts = {
tags: %w[div p code pre h1 h2 h3 b em i strong a img ul li ol blockquote], tags: %w[div p code pre h1 h2 h3 b em i strong a img ul li ol blockquote],
attributes: %w[href src], attributes: %w[href src],
remove_empty_nodes: false) remove_empty_nodes: false
}
opts[:whitelist] = SiteSetting.embed_whitelist_selector if SiteSetting.embed_whitelist_selector.present?
opts[:blacklist] = SiteSetting.embed_blacklist_selector if SiteSetting.embed_blacklist_selector.present?
doc = Readability::Document.new(open(url).read, opts)
tags = {'img' => 'src', 'script' => 'src', 'a' => 'href'} tags = {'img' => 'src', 'script' => 'src', 'a' => 'href'}
title = doc.title title = doc.title

View file

@ -870,6 +870,8 @@ en:
embed_truncate: "Truncate the imported posts" embed_truncate: "Truncate the imported posts"
embed_category: "Category of created topics" embed_category: "Category of created topics"
embed_post_limit: "Maximum number of posts to embed" embed_post_limit: "Maximum number of posts to embed"
embed_whitelist_selector: "css selector for elements that are allowed in embeds"
embed_blacklist_selector: "css selector for elements that are removed from embeds"
tos_accept_required: "If enabled, users will need to check a box on the signup form to confirm that they accept the terms of service. Edit 'Signup Form: Terms of Service Message' in the Content tab to change the message." tos_accept_required: "If enabled, users will need to check a box on the signup form to confirm that they accept the terms of service. Edit 'Signup Form: Terms of Service Message' in the Content tab to change the message."
notify_about_flags_after: "If there are flags that haven't been handled after this many hours, send an email to the contact_email. Set to 0 to disable." notify_about_flags_after: "If there are flags that haven't been handled after this many hours, send an email to the contact_email. Set to 0 to disable."

View file

@ -425,6 +425,8 @@ embedding:
embed_category: '' embed_category: ''
embed_post_limit: 100 embed_post_limit: 100
embed_truncate: false embed_truncate: false
embed_whitelist_selector: ''
embed_blacklist_selector: ''
legal: legal:
tos_url: tos_url: