From 515f187a8bb195cce6ef1229f6f9291dadc6201e Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Sat, 29 Jul 2023 15:51:45 -0500 Subject: [PATCH] Include site title in page titles This adds the site title to each page's title. When using the default site title, this will incorporate `ENV["HORO_PROJECT_NAME"]` and `ENV["HORO_BADGE_VERSION"]` into the page title. For example, ```html ActiveRecord::Base ``` will become ```html ActiveRecord::Base - Ruby on Rails v7.0.0 API documentation ``` The intent is to give search engines more context and to differentiate between versions of the documentation. --- lib/rdoc/generator/template/rails/class.rhtml | 2 +- lib/rdoc/generator/template/rails/file.rhtml | 2 +- lib/rdoc/generator/template/rails/index.rhtml | 2 +- lib/sdoc/helpers.rb | 4 ++++ spec/helpers_spec.rb | 15 +++++++++++++++ 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/rdoc/generator/template/rails/class.rhtml b/lib/rdoc/generator/template/rails/class.rhtml index 99ae7ad6..5dc17803 100644 --- a/lib/rdoc/generator/template/rails/class.rhtml +++ b/lib/rdoc/generator/template/rails/class.rhtml @@ -2,7 +2,7 @@ - <%= h klass.full_name %> + <%= page_title klass.full_name %> <%= include_template '_head.rhtml', {:context => klass, :tree_keys => klass.full_name.split('::') } %> diff --git a/lib/rdoc/generator/template/rails/file.rhtml b/lib/rdoc/generator/template/rails/file.rhtml index d07803c9..41d2f710 100644 --- a/lib/rdoc/generator/template/rails/file.rhtml +++ b/lib/rdoc/generator/template/rails/file.rhtml @@ -2,7 +2,7 @@ - <%= h file.name %> + <%= page_title file.name %> <%= include_template '_head.rhtml', {:context => file, :tree_keys => [] } %> diff --git a/lib/rdoc/generator/template/rails/index.rhtml b/lib/rdoc/generator/template/rails/index.rhtml index 7f1f457c..27ae175f 100644 --- a/lib/rdoc/generator/template/rails/index.rhtml +++ b/lib/rdoc/generator/template/rails/index.rhtml @@ -2,7 +2,7 @@ - <%= @options.title %> + <%= page_title %> <%= include_template '_head.rhtml', {:context => :index, tree_keys: []} %> diff --git a/lib/sdoc/helpers.rb b/lib/sdoc/helpers.rb index d47aab1a..301914e4 100644 --- a/lib/sdoc/helpers.rb +++ b/lib/sdoc/helpers.rb @@ -72,6 +72,10 @@ def badge_version @html_safe_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 group_by_first_letter(rdoc_objects) rdoc_objects.sort_by(&:name).group_by do |object| object.name[/^[a-z]/i]&.upcase || "#" diff --git a/spec/helpers_spec.rb b/spec/helpers_spec.rb index e66e03e3..1eb0bfe4 100644 --- a/spec/helpers_spec.rb +++ b/spec/helpers_spec.rb @@ -245,6 +245,21 @@ module Foo; module Bar; module Qux; end; end; end end end + describe "#page_title" do + it "includes options.title" do + @helpers.options.title = "My Docs" + + _(@helpers.page_title).must_equal "My Docs" + _(@helpers.page_title("Foo")).must_equal "Foo - My Docs" + end + + it "escapes the title" do + @helpers.options.title = "Docs & Stuff" + + _(@helpers.page_title("Foo")).must_equal "Foo<Bar> - Docs & Stuff" + 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")