Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include project name and version in og:title #261

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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