Skip to content

Relax paragraph pattern #1299

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

Merged
merged 5 commits into from
Mar 8, 2025
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
22 changes: 18 additions & 4 deletions lib/rdoc/generator/darkfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,12 @@ def template_for file, page = true, klass = ERB
template
end

# :stopdoc:
ParagraphExcerptRegexpOther = %r[\b\w[^./:]++\.]
# use \p/\P{letter} instead of \w/\W in Unicode
ParagraphExcerptRegexpUnicode = %r[\b\p{letter}[^./:]++\.]
# :startdoc:

# Returns an excerpt of the comment for usage in meta description tags
def excerpt(comment)
text = case comment
Expand All @@ -711,14 +717,22 @@ def excerpt(comment)

# Match from a capital letter to the first period, discarding any links, so
# that we don't end up matching badges in the README
first_paragraph_match = text.match(/[A-Z][^\.:\/]+\./)
return text[0...150].gsub(/\n/, " ").squeeze(" ") unless first_paragraph_match
pattern = ParagraphExcerptRegexpUnicode
begin
first_paragraph_match = text.match(pattern)
rescue Encoding::CompatibilityError
# The doc is non-ASCII text and encoded in other than Unicode base encodings.
raise if pattern == ParagraphExcerptRegexpOther
pattern = ParagraphExcerptRegexpOther
retry
end
return text[0...150].tr_s("\n", " ").squeeze(" ") unless first_paragraph_match

extracted_text = first_paragraph_match[0]
second_paragraph = first_paragraph_match.post_match.match(/[A-Z][^\.:\/]+\./)
second_paragraph = text.match(pattern, first_paragraph_match.end(0))
extracted_text << " " << second_paragraph[0] if second_paragraph

extracted_text[0...150].gsub(/\n/, " ").squeeze(" ")
extracted_text[0...150].tr_s("\n", " ").squeeze(" ")
end

def generate_ancestor_list(ancestors, klass)
Expand Down
20 changes: 20 additions & 0 deletions test/rdoc/test_rdoc_generator_darkfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,26 @@ def test_meta_tags_for_rdoc_files
)
end

def test_meta_tags_for_markdwon_files_paragraph
top_level = @store.add_file("README.md", parser: RDoc::Parser::Simple)
top_level.comment = <<~MARKDOWN
# Distributed Ruby: dRuby

dRuby is a distributed object system for Ruby. It allows an object.
MARKDOWN

@g.generate

content = File.binread("README_md.html")
assert_include(
content,
"<meta name=\"description\" content=\"" \
"README: dRuby " \
"dRuby is a distributed object system for Ruby. " \
"It allows an object."
)
end

def test_meta_tags_for_markdown_files
top_level = @store.add_file("MyPage.md", parser: RDoc::Parser::Markdown)
top_level.comment = <<~MARKDOWN
Expand Down
Loading