Skip to content

Commit

Permalink
Extract excerpt from RDoc::Markup::Document (page documents) correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Nov 8, 2024
1 parent 50dda13 commit faa9ee0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
17 changes: 16 additions & 1 deletion lib/rdoc/generator/darkfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,22 @@ 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
first_part = content.parts.first

if first_part.respond_to?(:text)
first_part.text
else
# TODO: We may want to find a way to proceed to the following parts
# instead
return ""
end
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
Expand Down
44 changes: 43 additions & 1 deletion test/rdoc/test_rdoc_generator_darkfish.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require_relative 'helper'

class TestRDocGeneratorDarkfish < RDoc::TestCase
class RDocGeneratorDarkfishTest < RDoc::TestCase

def setup
super
Expand Down Expand Up @@ -367,6 +367,48 @@ def test_meta_tags_for_pages
)
end

def test_meta_tags_for_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, '<meta name="keywords" content="ruby,documentation,MyPage">')
assert_include(
content,
'<meta name="description" content="MyPage: this is a comment">',
)
end

def test_meta_tags_for_pages_with_non_text_parts
top_level = @store.add_file("MyPage", parser: RDoc::Parser::Simple)
top_level.comment = RDoc::Markup::Document.new(RDoc::Markup::BlankLine.new)

@g.generate

content = File.binread("MyPage.html")
assert_include(content, '<meta name="keywords" content="ruby,documentation,MyPage">')
assert_include(
content,
'<meta name="description" content="MyPage: ">',
)
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, '<meta name="keywords" content="ruby,documentation,MyPage">')
assert_include(
content,
'<meta name="description" content="MyPage: ">',
)
end

##
# Asserts that +filename+ has a link count greater than 1 if hard links to
# @tmpdir are supported.
Expand Down

0 comments on commit faa9ee0

Please sign in to comment.