From b26eb782f5536d8c383aebe3d65571fb16a20bcd Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Wed, 23 Dec 2015 16:56:27 -0500 Subject: Add page descriptions and images A limited number of pages have defined their own descriptions, but otherwise we default to the Project's description (if `@project` is set), or the old `brand_title` fallback. The image will either be the uploaded project icon (never a generated one), the user's uploaded icon or Gravatar, or, finally, the GitLab logo. --- app/helpers/page_layout_helper.rb | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'app/helpers') diff --git a/app/helpers/page_layout_helper.rb b/app/helpers/page_layout_helper.rb index 9bf750124b2..6b16528dde6 100644 --- a/app/helpers/page_layout_helper.rb +++ b/app/helpers/page_layout_helper.rb @@ -8,6 +8,56 @@ module PageLayoutHelper @page_title.join(" \u00b7 ") end + # Define or get a description for the current page + # + # description - String (default: nil) + # + # If this helper is called multiple times with an argument, only the last + # description will be returned when called without an argument. Descriptions + # have newlines replaced with spaces and all HTML tags are sanitized. + # + # Examples: + # + # page_description # => "GitLab Community Edition" + # page_description("Foo") + # page_description # => "Foo" + # + # page_description("Bar\nBaz") + # page_description # => "Bar Baz" + # + # Returns an HTML-safe String. + def page_description(description = nil) + @page_description ||= page_description_default + + if description.present? + @page_description = description + else + sanitize(@page_description.squish, tags: []) + end + end + + # Default value for page_description when one hasn't been defined manually by + # a view + def page_description_default + if @project + @project.description + else + brand_title + end + end + + def page_image + default = image_url('gitlab_logo.png') + + if @project + @project.avatar_url || default + elsif @user + avatar_icon(@user) + else + default + end + end + def header_title(title = nil, title_url = nil) if title @header_title = title -- cgit v1.2.1 From 5a3b9c97e34ee69312ef9bcf575894a106c5a271 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Wed, 23 Dec 2015 17:14:18 -0500 Subject: Account for `@project.description` being nil --- app/helpers/page_layout_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/helpers') diff --git a/app/helpers/page_layout_helper.rb b/app/helpers/page_layout_helper.rb index 6b16528dde6..c4eb09bea2b 100644 --- a/app/helpers/page_layout_helper.rb +++ b/app/helpers/page_layout_helper.rb @@ -40,7 +40,7 @@ module PageLayoutHelper # a view def page_description_default if @project - @project.description + @project.description || brand_title else brand_title end -- cgit v1.2.1 From c6d25083626c9a97a0ae442298b59c2ff9351f3a Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 24 Dec 2015 16:26:52 -0500 Subject: Truncate page_description to 30 words --- app/helpers/page_layout_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/helpers') diff --git a/app/helpers/page_layout_helper.rb b/app/helpers/page_layout_helper.rb index c4eb09bea2b..4f1276f93ec 100644 --- a/app/helpers/page_layout_helper.rb +++ b/app/helpers/page_layout_helper.rb @@ -30,9 +30,9 @@ module PageLayoutHelper @page_description ||= page_description_default if description.present? - @page_description = description + @page_description = description.squish else - sanitize(@page_description.squish, tags: []) + sanitize(@page_description, tags: []).truncate_words(30) end end -- cgit v1.2.1 From ab3d855c0e1869fd1986c3bcdf7519f6b1cf1fa8 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 24 Dec 2015 17:03:54 -0500 Subject: Add support for `twitter:label` meta tags --- app/helpers/page_layout_helper.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'app/helpers') diff --git a/app/helpers/page_layout_helper.rb b/app/helpers/page_layout_helper.rb index 4f1276f93ec..791cb9e50bd 100644 --- a/app/helpers/page_layout_helper.rb +++ b/app/helpers/page_layout_helper.rb @@ -58,6 +58,30 @@ module PageLayoutHelper end end + # Define or get attributes to be used as Twitter card metadata + # + # map - Hash of label => data pairs. Keys become labels, values become data + # + # Raises ArgumentError if given more than two attributes + def page_card_attributes(map = {}) + raise ArgumentError, 'cannot provide more than two attributes' if map.length > 2 + + @page_card_attributes ||= {} + @page_card_attributes = map.reject { |_,v| v.blank? } if map.present? + @page_card_attributes + end + + def page_card_meta_tags + tags = '' + + page_card_attributes.each_with_index do |pair, i| + tags << tag(:meta, property: "twitter:label#{i + 1}", content: pair[0]) + tags << tag(:meta, property: "twitter:data#{i + 1}", content: pair[1]) + end + + tags.html_safe + end + def header_title(title = nil, title_url = nil) if title @header_title = title -- cgit v1.2.1