Skip to content

Commit

Permalink
Extract excerpt from raw pages correctly (#1200)
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 authored Nov 18, 2024
1 parent 6852567 commit 3c67824
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
14 changes: 13 additions & 1 deletion lib/rdoc/generator/darkfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 48 additions & 2 deletions 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 @@ -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
Expand All @@ -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, '<meta name="keywords" content="ruby,documentation,MyPage">')
assert_include(
content,
'<meta name="description" content="MyPage: # MyPage This is a comment">',
)
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, '<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_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 3c67824

Please sign in to comment.