noosfero | 2 new commits pushed to repository
Bráulio Bhavamitra
gitlab at gitlab.com
Fri Jan 30 10:33:11 BRST 2015
Bráulio Bhavamitra pushed to refs/heads/master at <a href="https://gitlab.com/noosfero/noosfero">Noosfero / noosfero</a>
Commits:
<a href="https://gitlab.com/noosfero/noosfero/commit/db301dca88a3d4e9512c43180ae46a6328730264">db301dca</a> by Braulio Bhavamitra
Add metadata plugin
- - - - -
<a href="https://gitlab.com/noosfero/noosfero/commit/77aa69cdca03d4b634727acca0914ac3103eeb8c">77aa69cd</a> by Bráulio Bhavamitra
Merge branch 'metadata-plugin' into 'master'
Add Metadata plugin
Move code from core and add better support for OpenGraph
@aurium
See merge request !359
- - - - -
Changes:
=====================================
app/views/layouts/application-ng.html.erb
=====================================
--- a/app/views/layouts/application-ng.html.erb
+++ b/app/views/layouts/application-ng.html.erb
@@ -12,24 +12,9 @@
<meta name="twitter:title" content="<%= h page_title %>">
<meta name="twitter:description" content="<%= meta_description_tag(@page) %>">
- <!-- Open Graph -->
- <meta property="og:type" content="<%= @page ? 'article' : 'website' %>">
- <meta property="og:url" content="<%= @page ? url_for(@page.url) : top_url %>">
- <meta property="og:title" content="<%= h page_title %>">
- <meta property="og:site_name" content="<%= profile ? profile.name : @environment.name %>">
- <meta property="og:description" content="<%= meta_description_tag(@page) %>">
-
<!-- site root -->
<meta property="noosfero:root" content="<%= Noosfero.root %>"/>
- <% if @page %>
- <meta property="article:published_time" content="<%= show_date(@page.published_at) %>">
- <% @page.body_images_paths.each do |img| %>
- <meta name="twitter:image" content="<%= img.to_s %>">
- <meta property="og:image" content="<%= img.to_s %>">
- <% end %>
- <% end %>
-
<link rel="shortcut icon" href="<%= image_path(theme_favicon) %>" type="image/x-icon" />
<%= noosfero_javascript %>
<%= noosfero_stylesheets %>
=====================================
plugins/metadata/config.yml.dist
=====================================
--- /dev/null
+++ b/plugins/metadata/config.yml.dist
@@ -0,0 +1,12 @@
+open_graph:
+ domain: cirandas.net
+ environment_logo: 'http://cirandas.net/designs/themes/cirandas-responsive/images/cirandas-logo-110.png'
+ types:
+ article: article
+ product: app_cirandas:sse_product
+ uploaded_file: app_cirandas:document
+ image: app_cirandas:picture
+ profile: app_cirandas:profile
+ person: app_cirandas:user
+ community: app_cirandas:community
+ enterprise: app_cirandas:sse_initiative
=====================================
plugins/metadata/lib/ext/article.rb
=====================================
--- /dev/null
+++ b/plugins/metadata/lib/ext/article.rb
@@ -0,0 +1,32 @@
+require_dependency 'article'
+
+class Article
+
+ Metadata = {
+ 'og:type' => MetadataPlugin.og_types[:article],
+ 'og:url' => proc{ |a, c| c.og_url_for a.url },
+ 'og:title' => proc{ |a, c| a.title },
+ 'og:image' => proc do |a, c|
+ result = a.body_images_paths
+ result = "#{a.profile.environment.top_url}#{a.profile.image.public_filename}" if a.profile.image if result.blank?
+ result = MetadataPlugin.config[:open_graph][:environment_logo] if result.blank?
+ result
+ end,
+ 'og:see_also' => [],
+ 'og:site_name' => proc{ |a, c| a.profile.name },
+ 'og:updated_time' => proc{ |a, c| a.updated_at.iso8601 },
+ 'og:locale:locale' => proc{ |a, c| a.environment.default_language },
+ 'og:locale:alternate' => proc{ |a, c| a.environment.languages - [a.environment.default_language] },
+ 'twitter:image' => proc{ |a, c| a.body_images_paths },
+ 'article:expiration_time' => "", # In the future we might want to populate this
+ 'article:modified_time' => proc{ |a, c| a.updated_at.iso8601 },
+ 'article:published_time' => proc{ |a, c| a.published_at.iso8601 },
+ 'article:section' => "", # In the future we might want to populate this
+ 'article:tag' => proc{ |a, c| a.tags.map &:name },
+ 'og:description' => proc{ |a, c| ActionView::Base.full_sanitizer.sanitize a.body },
+ 'og:rich_attachment' => "",
+ }
+
+
+
+end
=====================================
plugins/metadata/lib/ext/community.rb
=====================================
--- /dev/null
+++ b/plugins/metadata/lib/ext/community.rb
@@ -0,0 +1,10 @@
+require_dependency 'community'
+require_dependency "#{File.dirname __FILE__}/profile"
+
+class Community
+
+ Metadata = Metadata.merge({
+ 'og:type' => MetadataPlugin.og_types[:community],
+ })
+
+end
=====================================
plugins/metadata/lib/ext/enterprise.rb
=====================================
--- /dev/null
+++ b/plugins/metadata/lib/ext/enterprise.rb
@@ -0,0 +1,18 @@
+require_dependency 'enterprise'
+require_dependency "#{File.dirname __FILE__}/profile"
+
+class Enterprise
+
+ Metadata = Metadata.merge({
+ 'og:type' => MetadataPlugin.og_types[:enterprise],
+ 'business:contact_data:email' => proc{ |e, c| e.contact_email },
+ 'business:contact_data:phone_number' => proc{ |e, c| e.contact_phone },
+ 'business:contact_data:street_address' => proc{ |e, c| e.address },
+ 'business:contact_data:locality' => proc{ |e, c| e.city },
+ 'business:contact_data:region' => proc{ |e, c| e.state },
+ 'business:contact_data:postal_code' => proc{ |e, c| e.zip_code },
+ 'business:contact_data:country_name' => proc{ |e| e.country },
+ 'place:location:latitude' => proc{ |e, c| p.lat },
+ 'place:location:longitude' => proc{ |e, c| p.lng },
+ })
+end
=====================================
plugins/metadata/lib/ext/environment.rb
=====================================
--- /dev/null
+++ b/plugins/metadata/lib/ext/environment.rb
@@ -0,0 +1,13 @@
+require_dependency 'environment'
+
+class Environment
+
+ Metadata = {
+ 'og:site_name' => proc{ |e, c| e.name },
+ 'og:description' => proc{ |e, c| e.name },
+ 'og:url' => proc{ |e, c| e.top_url },
+ 'og:locale:locale' => proc{ |e, c| e.default_language },
+ 'og:locale:alternate' => proc{ |e, c| e.languages - [e.default_language] }
+ }
+
+end
=====================================
plugins/metadata/lib/ext/person.rb
=====================================
--- /dev/null
+++ b/plugins/metadata/lib/ext/person.rb
@@ -0,0 +1,10 @@
+require_dependency 'person'
+require_dependency "#{File.dirname __FILE__}/profile"
+
+class Person
+
+ Metadata = Metadata.merge({
+ 'og:type' => MetadataPlugin.og_types[:person],
+ })
+
+end
=====================================
plugins/metadata/lib/ext/product.rb
=====================================
--- /dev/null
+++ b/plugins/metadata/lib/ext/product.rb
@@ -0,0 +1,25 @@
+require_dependency 'product'
+
+class Product
+
+ Metadata = {
+ 'og:type' => MetadataPlugin.og_types[:product],
+ 'og:url' => proc{ |p, c| c.og_url_for p.url },
+ 'og:gr_hascurrencyvalue' => proc{ |p, c| p.price.to_f },
+ 'og:gr_hascurrency' => proc{ |p, c| p.environment.currency_unit },
+ 'og:title' => proc{ |p, c| p.name },
+ 'og:description' => proc{ |p, c| ActionView::Base.full_sanitizer.sanitize p.description },
+ 'og:image' => proc{ |p, c| "#{p.environment.top_url}#{p.image.public_filename}" if p.image },
+ 'og:image:type' => proc{ |p, c| p.image.content_type if p.image },
+ 'og:image:height' => proc{ |p, c| p.image.height if p.image },
+ 'og:image:width' => proc{ |p, c| p.image.width if p.image },
+ 'og:see_also' => [],
+ 'og:site_name' => proc{ |p, c| c.og_url_for p.profile.url },
+ 'og:updated_time' => proc{ |p, c| p.updated_at.iso8601 },
+ 'og:locale:locale' => proc{ |p, c| p.environment.default_language },
+ 'og:locale:alternate' => proc{ |p, c| p.environment.languages - [p.environment.default_language] },
+ }
+
+ protected
+
+end
=====================================
plugins/metadata/lib/ext/profile.rb
=====================================
--- /dev/null
+++ b/plugins/metadata/lib/ext/profile.rb
@@ -0,0 +1,24 @@
+require_dependency 'profile'
+
+class Profile
+
+ Metadata = {
+ 'og:type' => MetadataPlugin.og_types[:profile],
+ 'og:image' => proc{ |p, c| "#{p.environment.top_url}#{p.image.public_filename}" if p.image },
+ 'og:title' => proc{ |p, c| p.short_name nil },
+ 'og:url' => proc do |p, c|
+ #force profile identifier for custom domains and fixed host. see og_url_for
+ c.og_url_for p.url.merge(profile: p.identifier)
+ end,
+ 'og:description' => proc{ |p, c| p.description },
+ 'og:updated_time' => proc{ |p, c| p.updated_at.iso8601 },
+ 'place:location:latitude' => proc{ |p, c| p.lat },
+ 'place:location:longitude' => proc{ |p, c| p.lng },
+ 'og:locale:locale' => proc{ |p, c| p.environment.default_language },
+ 'og:locale:alternate' => proc{ |p, c| p.environment.languages - [p.environment.default_language] },
+ 'og:site_name' => "",
+ 'og:see_also' => "",
+ 'og:rich_attachment' => "",
+ }
+
+end
=====================================
plugins/metadata/lib/ext/uploaded_file.rb
=====================================
--- /dev/null
+++ b/plugins/metadata/lib/ext/uploaded_file.rb
@@ -0,0 +1,17 @@
+require_dependency 'uploaded_file'
+require_dependency "#{File.dirname __FILE__}/article"
+
+class UploadedFile
+
+ Metadata = {
+ 'og:type' => proc do |u, c|
+ type = if u.image? then :image else :uploaded_file end
+ MetadataPlugin.og_types[type]
+ end,
+ 'og:url' => proc{ |u, c| c.og_url_for u.url.merge(view: true) },
+ 'og:title' => proc{ |u, c| u.title },
+ 'og:image' => proc{ |u, c| "#{u.environment.top_url}#{u.public_filename}" if u.image? },
+ 'og:description' => proc{ |u, c| u.abstract || u.title },
+ }
+
+end
=====================================
plugins/metadata/lib/metadata_plugin.rb
=====================================
--- /dev/null
+++ b/plugins/metadata/lib/metadata_plugin.rb
@@ -0,0 +1,59 @@
+
+class MetadataPlugin < Noosfero::Plugin
+
+ def self.plugin_name
+ I18n.t 'metadata_plugin.lib.plugin.name'
+ end
+
+ def self.plugin_description
+ I18n.t 'metadata_plugin.lib.plugin.description'
+ end
+
+ def self.config
+ @config ||= HashWithIndifferentAccess.new(YAML.load File.read("#{File.dirname __FILE__}/../config.yml")) rescue {}
+ end
+
+ def self.og_types
+ @og_types ||= self.config[:open_graph][:types] rescue {}
+ end
+
+ def head_ending
+ plugin = self
+ lambda do
+ options = MetadataPlugin::Spec::Controllers[controller.controller_path.to_sym]
+ options ||= MetadataPlugin::Spec::Controllers[:profile] if controller.is_a? ProfileController
+ options ||= MetadataPlugin::Spec::Controllers[:environment]
+ return unless options
+
+ return unless object = case variable = options[:variable]
+ when Proc then instance_exec(&variable) rescue nil
+ else instance_variable_get variable
+ end
+ return unless metadata = (object.class.const_get(:Metadata) rescue nil)
+
+ metadata.map do |property, contents|
+ contents = contents.call(object, plugin) rescue nil if contents.is_a? Proc
+ next if contents.blank?
+
+ Array(contents).map do |content|
+ content = content.call(object, plugin) rescue nil if content.is_a? Proc
+ next if content.blank?
+ tag 'meta', property: property, content: content
+ end.join
+ end.join
+ end
+ end
+
+ # context HELPERS
+ def og_url_for options
+ options.delete :port
+ options[:host] = self.class.config[:open_graph][:domain] rescue context.send(:environment).default_hostname
+ Noosfero::Application.routes.url_helpers.url_for options
+ end
+
+ protected
+
+end
+
+ActiveSupport.run_load_hooks :metadata_plugin, MetadataPlugin
+
=====================================
plugins/metadata/lib/metadata_plugin/spec.rb
=====================================
--- /dev/null
+++ b/plugins/metadata/lib/metadata_plugin/spec.rb
@@ -0,0 +1,29 @@
+
+class MetadataPlugin::Spec
+
+ Controllers = {
+ manage_products: {
+ variable: :@product,
+ },
+ content_viewer: {
+ variable: proc do
+ if profile and profile.home_page_id == @page.id
+ @profile
+ elsif @page.respond_to? :encapsulated_file
+ @page.encapsulated_file
+ else
+ @page
+ end
+ end,
+ },
+ # fallback
+ profile: {
+ variable: :@profile,
+ },
+ # last fallback
+ environment: {
+ variable: :@environment,
+ },
+ }
+
+end
=====================================
plugins/metadata/locales/en.yml
=====================================
--- /dev/null
+++ b/plugins/metadata/locales/en.yml
@@ -0,0 +1,14 @@
+
+"en-US": &en-US
+
+ metadata_plugin:
+ lib:
+ plugin:
+ name: 'Export metadata'
+ description: 'Export metadata for models on meta tags'
+
+'en_US':
+ <<: *en-US
+'en':
+ <<: *en-US
+
=====================================
plugins/metadata/locales/pt.yml
=====================================
--- /dev/null
+++ b/plugins/metadata/locales/pt.yml
@@ -0,0 +1,14 @@
+
+"pt-BR": &pt-BR
+
+ metadata_plugin:
+ lib:
+ plugin:
+ name: 'Exporta metadados'
+ description: 'Exporta metadados de modelos em tags meta'
+
+'pt_BR':
+ <<: *pt-BR
+'pt':
+ <<: *pt-BR
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150130/76eff543/attachment-0001.html>
More information about the Noosfero-dev
mailing list