Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract excerpt from RDoc::Markup::Document (raw pages) correctly #1200

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class name change is... unconventional (I looked at the neighboring files). Is it intentional?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentional because class names should be noun (FooTest) instead of (TestFoo). It also makes them work with ruby-lsp's CodeLens feature.
In IRB I did a big batch of rename so it's been like this in most if not all tests. But given RDoc still has relatively low development activities, I just change them as I touch things.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@st0012 Thanks for providing that context. Seems like a good thing!


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