Skip to content

Commit

Permalink
Merge pull request #261 from jonathanhefner/og_title-include-project
Browse files Browse the repository at this point in the history
Include project name and version in `og:title`
  • Loading branch information
jonathanhefner authored Jul 31, 2023
2 parents eb27d46 + 8993ab1 commit 3d35d07
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/rdoc/generator/template/rails/class.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= include_template '_head.rhtml', {:context => klass, :tree_keys => klass.full_name.split('::') } %>

<meta property="og:title" value="<%= klass.full_name %>">
<meta property="og:title" value="<%= og_title klass.full_name %>">

<% unless (description = klass.description).empty? %>
<% human_desc = truncate(strip_tags(description.gsub("\n", " ").strip)) %>
Expand Down
2 changes: 2 additions & 0 deletions lib/rdoc/generator/template/rails/file.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<title><%= page_title file.name %></title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= include_template '_head.rhtml', {:context => file, :tree_keys => [] } %>

<meta property="og:title" value="<%= og_title file.name %>">
</head>

<body>
Expand Down
11 changes: 8 additions & 3 deletions lib/sdoc/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,26 @@ def canonical_url(context)
end

def project_name
@html_safe_project_name ||= h(ENV["HORO_PROJECT_NAME"]) if ENV["HORO_PROJECT_NAME"]
h(ENV["HORO_PROJECT_NAME"]) if ENV["HORO_PROJECT_NAME"]
end

def project_version
@html_safe_project_version ||= h(ENV["HORO_PROJECT_VERSION"]) if ENV["HORO_PROJECT_VERSION"]
h(ENV["HORO_PROJECT_VERSION"]) if ENV["HORO_PROJECT_VERSION"]
end

def badge_version
@html_safe_badge_version ||= h(ENV["HORO_BADGE_VERSION"]) if ENV["HORO_BADGE_VERSION"]
h(ENV["HORO_BADGE_VERSION"]) if ENV["HORO_BADGE_VERSION"]
end

def page_title(title = nil)
h [title, @options.title].compact.join(" - ")
end

def og_title(title)
project = [project_name, badge_version].join(" ").strip
"#{h title}#{" (#{project})" unless project.empty?}"
end

def group_by_first_letter(rdoc_objects)
rdoc_objects.sort_by(&:name).group_by do |object|
object.name[/^[a-z]/i]&.upcase || "#"
Expand Down
26 changes: 26 additions & 0 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,32 @@ module Foo; module Bar; module Qux; end; end; end
end
end

describe "#og_title" do
it "includes ENV['HORO_PROJECT_NAME'] and ENV['HORO_BADGE_VERSION']" do
with_env("HORO_PROJECT_NAME" => "My Gem", "HORO_BADGE_VERSION" => "v2.0") do
_(@helpers.og_title("Foo")).must_equal "Foo (My Gem v2.0)"
end

with_env("HORO_PROJECT_NAME" => "My Gem", "HORO_BADGE_VERSION" => nil) do
_(@helpers.og_title("Foo")).must_equal "Foo (My Gem)"
end

with_env("HORO_PROJECT_NAME" => nil, "HORO_BADGE_VERSION" => "v2.0") do
_(@helpers.og_title("Foo")).must_equal "Foo (v2.0)"
end

with_env("HORO_PROJECT_NAME" => nil, "HORO_BADGE_VERSION" => nil) do
_(@helpers.og_title("Foo")).must_equal "Foo"
end
end

it "escapes the title" do
with_env("HORO_PROJECT_NAME" => "Ruby & Rails", "HORO_BADGE_VERSION" => "~> 1.0.0") do
_(@helpers.og_title("Foo<Bar>")).must_equal "Foo&lt;Bar&gt; (Ruby &amp; Rails ~&gt; 1.0.0)"
end
end
end

describe "#group_by_first_letter" do
it "groups RDoc objects by the first letter of their #name" do
context = rdoc_top_level_for(<<~RUBY).find_module_named("Foo")
Expand Down

0 comments on commit 3d35d07

Please sign in to comment.