diff options
author | Vinicius Stock <[email protected]> | 2024-10-15 18:59:29 -0400 |
---|---|---|
committer | git <[email protected]> | 2024-10-15 22:59:33 +0000 |
commit | ed993b5bcc4fcae661dd022d3211dcc770425218 (patch) | |
tree | ea1540ae474a2a1c18f1ca7fdce66ff48873109e | |
parent | f45eb3dcb9c7d849064cb802953f37e1cf9f3996 (diff) |
[ruby/rdoc] Generate meta tags based on page's content
(https://github.com/ruby/rdoc/pull/1091)
https://github.com/ruby/rdoc/commit/716bc16a7d
-rw-r--r-- | lib/rdoc/generator/darkfish.rb | 15 | ||||
-rw-r--r-- | lib/rdoc/generator/template/darkfish/_head.rhtml | 22 | ||||
-rw-r--r-- | test/rdoc/test_rdoc_generator_darkfish.rb | 45 |
3 files changed, 82 insertions, 0 deletions
diff --git a/lib/rdoc/generator/darkfish.rb b/lib/rdoc/generator/darkfish.rb index 96bb4fb66f..5709fabf81 100644 --- a/lib/rdoc/generator/darkfish.rb +++ b/lib/rdoc/generator/darkfish.rb @@ -780,4 +780,19 @@ class RDoc::Generator::Darkfish template end + # Returns an excerpt of the content for usage in meta description tags + def excerpt(content) + text = content.is_a?(RDoc::Comment) ? content.text : content + + # Match from a capital letter to the first period, discarding any links, so + # that we don't end up matching badges in the README + first_paragraph_match = text.match(/[A-Z][^\.:\/]+\./) + return text[0...150].gsub(/\n/, " ").squeeze(" ") unless first_paragraph_match + + extracted_text = first_paragraph_match[0] + second_paragraph = first_paragraph_match.post_match.match(/[A-Z][^\.:\/]+\./) + extracted_text << " " << second_paragraph[0] if second_paragraph + + extracted_text[0...150].gsub(/\n/, " ").squeeze(" ") + end end diff --git a/lib/rdoc/generator/template/darkfish/_head.rhtml b/lib/rdoc/generator/template/darkfish/_head.rhtml index 69649ad3b5..9e6fb4f94c 100644 --- a/lib/rdoc/generator/template/darkfish/_head.rhtml +++ b/lib/rdoc/generator/template/darkfish/_head.rhtml @@ -3,6 +3,28 @@ <title><%= h @title %></title> +<%- if defined?(klass) -%> + <meta name="keywords" content="ruby,<%= h "#{klass.type},#{klass.full_name}" %>"> + + <%- if klass.comment.empty? -%> + <meta name="description" content="Documentation for the <%= h "#{klass.full_name} #{klass.type}" %>"> + <%- else -%> + <meta name="description" content="<%= h "#{klass.type} #{klass.full_name}: #{excerpt(klass.comment)}" %>"> + <%- end -%> +<%- elsif defined?(file) -%> + <meta name="keywords" content="ruby,documentation,<%= h file.page_name %>"> + <meta name="description" content="<%= h "#{file.page_name}: #{excerpt(file.comment)}" %>"> +<%- elsif @title -%> + <meta name="keywords" content="ruby,documentation,<%= h @title %>"> + + <%- if @options.main_page and + main_page = @files.find { |f| f.full_name == @options.main_page } then %> + <meta name="description" content="<%= h "#{@title}: #{excerpt(main_page.comment)}" %>"> + <%- else -%> + <meta name="description" content="Documentation for <%= h @title %>"> + <%- end -%> +<%- end -%> + <script type="text/javascript"> var rdoc_rel_prefix = "<%= h asset_rel_prefix %>/"; var index_rel_prefix = "<%= h rel_prefix %>/"; diff --git a/test/rdoc/test_rdoc_generator_darkfish.rb b/test/rdoc/test_rdoc_generator_darkfish.rb index 899e697de6..f6c0a1e1cf 100644 --- a/test/rdoc/test_rdoc_generator_darkfish.rb +++ b/test/rdoc/test_rdoc_generator_darkfish.rb @@ -322,6 +322,51 @@ class TestRDocGeneratorDarkfish < RDoc::TestCase assert_main_title(File.binread('index.html'), title) end + def test_meta_tags_for_index + @options.title = "My awesome Ruby project" + @g.generate + + content = File.binread("index.html") + + assert_include(content, '<meta name="keywords" content="ruby,documentation,My awesome Ruby project">') + assert_include(content, '<meta name="description" content="Documentation for My awesome Ruby project">') + end + + def test_meta_tags_for_classes + top_level = @store.add_file("file.rb") + top_level.add_class(@klass.class, @klass.name) + inner = @klass.add_class(RDoc::NormalClass, "Inner") + inner.add_comment("This is a normal class. It is fully documented.", top_level) + + @g.generate + + content = File.binread("Klass/Inner.html") + assert_include(content, '<meta name="keywords" content="ruby,class,Klass::Inner">') + assert_include( + content, + '<meta name="description" content="class Klass::Inner: This is a normal class. It is fully documented.">', + ) + end + + def test_meta_tags_for_pages + top_level = @store.add_file("CONTRIBUTING.rdoc", parser: RDoc::Parser::Simple) + top_level.comment = <<~RDOC + = Contributing + + Here are the instructions for contributing. Begin by installing Ruby. + RDOC + + @g.generate + + content = File.binread("CONTRIBUTING_rdoc.html") + assert_include(content, '<meta name="keywords" content="ruby,documentation,CONTRIBUTING">') + assert_include( + content, + "<meta name=\"description\" content=\"CONTRIBUTING: Contributing Here are the instructions for contributing." \ + " Begin by installing Ruby.\">", + ) + end + ## # Asserts that +filename+ has a link count greater than 1 if hard links to # @tmpdir are supported. |