From 3c678249e2cac623c75b7ec2099b0e786aa11bec Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Mon, 18 Nov 2024 10:32:25 +0000 Subject: [PATCH] Extract excerpt from raw pages correctly (#1200) Fixes https://bugs.ruby-lang.org/issues/20862 --- lib/rdoc/generator/darkfish.rb | 14 ++++++- test/rdoc/test_rdoc_generator_darkfish.rb | 50 ++++++++++++++++++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/lib/rdoc/generator/darkfish.rb b/lib/rdoc/generator/darkfish.rb index 25ade1e9f1..08f2b85e3b 100644 --- a/lib/rdoc/generator/darkfish.rb +++ b/lib/rdoc/generator/darkfish.rb @@ -782,7 +782,19 @@ def template_for file, page = true, klass = ERB # Returns an excerpt of the content for usage in meta description tags def excerpt(content) - text = content.is_a?(RDoc::Comment) ? content.text : content + text = case content + when RDoc::Comment + content.text + when RDoc::Markup::Document + # This case is for page files that are not markdown nor rdoc + # We convert them to markdown for now as it's easier to extract the text + formatter = RDoc::Markup::ToMarkdown.new + formatter.start_accepting + formatter.accept_document(content) + formatter.end_accepting + else + content + end # Match from a capital letter to the first period, discarding any links, so # that we don't end up matching badges in the README diff --git a/test/rdoc/test_rdoc_generator_darkfish.rb b/test/rdoc/test_rdoc_generator_darkfish.rb index f6c0a1e1cf..0a9be7e4ee 100644 --- a/test/rdoc/test_rdoc_generator_darkfish.rb +++ b/test/rdoc/test_rdoc_generator_darkfish.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require_relative 'helper' -class TestRDocGeneratorDarkfish < RDoc::TestCase +class RDocGeneratorDarkfishTest < RDoc::TestCase def setup super @@ -348,7 +348,7 @@ def test_meta_tags_for_classes ) end - def test_meta_tags_for_pages + def test_meta_tags_for_rdoc_files top_level = @store.add_file("CONTRIBUTING.rdoc", parser: RDoc::Parser::Simple) top_level.comment = <<~RDOC = Contributing @@ -367,6 +367,52 @@ def test_meta_tags_for_pages ) end + def test_meta_tags_for_markdown_files + top_level = @store.add_file("MyPage.md", parser: RDoc::Parser::Markdown) + top_level.comment = <<~MARKDOWN + # MyPage + + This is a comment + MARKDOWN + + @g.generate + + content = File.binread("MyPage_md.html") + assert_include(content, '') + assert_include( + content, + '', + ) + end + + def test_meta_tags_for_raw_pages + top_level = @store.add_file("MyPage", parser: RDoc::Parser::Simple) + top_level.comment = RDoc::Markup::Document.new(RDoc::Markup::Paragraph.new('this is a comment')) + + @g.generate + + content = File.binread("MyPage.html") + assert_include(content, '') + assert_include( + content, + '', + ) + end + + def test_meta_tags_for_empty_document + top_level = @store.add_file("MyPage", parser: RDoc::Parser::Simple) + top_level.comment = RDoc::Markup::Document.new + + @g.generate + + content = File.binread("MyPage.html") + assert_include(content, '') + assert_include( + content, + '', + ) + end + ## # Asserts that +filename+ has a link count greater than 1 if hard links to # @tmpdir are supported.