Skip to content

Commit 34db821

Browse files
committed
Extract excerpt from raw pages correctly
Fixes https://bugs.ruby-lang.org/issues/20862
1 parent 50dda13 commit 34db821

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

lib/rdoc/generator/darkfish.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,19 @@ 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+
# This case is for page files that are not markdown nor rdoc
790+
# We convert them to markdown for now as it's easier to extract the text
791+
formatter = RDoc::Markup::ToMarkdown.new
792+
formatter.start_accepting
793+
formatter.accept_document(content)
794+
formatter.end_accepting
795+
else
796+
content
797+
end
786798

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

test/rdoc/test_rdoc_generator_darkfish.rb

Lines changed: 48 additions & 2 deletions
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
@@ -348,7 +348,7 @@ def test_meta_tags_for_classes
348348
)
349349
end
350350

351-
def test_meta_tags_for_pages
351+
def test_meta_tags_for_rdoc_files
352352
top_level = @store.add_file("CONTRIBUTING.rdoc", parser: RDoc::Parser::Simple)
353353
top_level.comment = <<~RDOC
354354
= Contributing
@@ -367,6 +367,52 @@ def test_meta_tags_for_pages
367367
)
368368
end
369369

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

0 commit comments

Comments
 (0)