Skip to content

Commit faa9ee0

Browse files
committed
Extract excerpt from RDoc::Markup::Document (page documents) correctly
Fixes https://bugs.ruby-lang.org/issues/20862
1 parent 50dda13 commit faa9ee0

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

lib/rdoc/generator/darkfish.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,22 @@ def template_for file, page = true, klass = ERB
782782

783783
# Returns an excerpt of the content for usage in meta description tags
784784
def excerpt(content)
785-
text = content.is_a?(RDoc::Comment) ? content.text : content
785+
text = case content
786+
when RDoc::Comment
787+
content.text
788+
when RDoc::Markup::Document
789+
first_part = content.parts.first
790+
791+
if first_part.respond_to?(:text)
792+
first_part.text
793+
else
794+
# TODO: We may want to find a way to proceed to the following parts
795+
# instead
796+
return ""
797+
end
798+
else
799+
content
800+
end
786801

787802
# Match from a capital letter to the first period, discarding any links, so
788803
# that we don't end up matching badges in the README

test/rdoc/test_rdoc_generator_darkfish.rb

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22
require_relative 'helper'
33

4-
class TestRDocGeneratorDarkfish < RDoc::TestCase
4+
class RDocGeneratorDarkfishTest < RDoc::TestCase
55

66
def setup
77
super
@@ -367,6 +367,48 @@ def test_meta_tags_for_pages
367367
)
368368
end
369369

370+
def test_meta_tags_for_pages
371+
top_level = @store.add_file("MyPage", parser: RDoc::Parser::Simple)
372+
top_level.comment = RDoc::Markup::Document.new(RDoc::Markup::Paragraph.new('this is a comment'))
373+
374+
@g.generate
375+
376+
content = File.binread("MyPage.html")
377+
assert_include(content, '<meta name="keywords" content="ruby,documentation,MyPage">')
378+
assert_include(
379+
content,
380+
'<meta name="description" content="MyPage: this is a comment">',
381+
)
382+
end
383+
384+
def test_meta_tags_for_pages_with_non_text_parts
385+
top_level = @store.add_file("MyPage", parser: RDoc::Parser::Simple)
386+
top_level.comment = RDoc::Markup::Document.new(RDoc::Markup::BlankLine.new)
387+
388+
@g.generate
389+
390+
content = File.binread("MyPage.html")
391+
assert_include(content, '<meta name="keywords" content="ruby,documentation,MyPage">')
392+
assert_include(
393+
content,
394+
'<meta name="description" content="MyPage: ">',
395+
)
396+
end
397+
398+
def test_meta_tags_for_empty_document
399+
top_level = @store.add_file("MyPage", parser: RDoc::Parser::Simple)
400+
top_level.comment = RDoc::Markup::Document.new
401+
402+
@g.generate
403+
404+
content = File.binread("MyPage.html")
405+
assert_include(content, '<meta name="keywords" content="ruby,documentation,MyPage">')
406+
assert_include(
407+
content,
408+
'<meta name="description" content="MyPage: ">',
409+
)
410+
end
411+
370412
##
371413
# Asserts that +filename+ has a link count greater than 1 if hard links to
372414
# @tmpdir are supported.

0 commit comments

Comments
 (0)