Skip to content

Commit 0a541fb

Browse files
authored
Merge pull request #1299 from nobu/paragraph-excerpt
Relax paragraph pattern
2 parents c23bf63 + 0ed8f01 commit 0a541fb

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

lib/rdoc/generator/darkfish.rb

+18-4
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,12 @@ def template_for file, page = true, klass = ERB
700700
template
701701
end
702702

703+
# :stopdoc:
704+
ParagraphExcerptRegexpOther = %r[\b\w[^./:]++\.]
705+
# use \p/\P{letter} instead of \w/\W in Unicode
706+
ParagraphExcerptRegexpUnicode = %r[\b\p{letter}[^./:]++\.]
707+
# :startdoc:
708+
703709
# Returns an excerpt of the comment for usage in meta description tags
704710
def excerpt(comment)
705711
text = case comment
@@ -711,14 +717,22 @@ def excerpt(comment)
711717

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

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

721-
extracted_text[0...150].gsub(/\n/, " ").squeeze(" ")
735+
extracted_text[0...150].tr_s("\n", " ").squeeze(" ")
722736
end
723737

724738
def generate_ancestor_list(ancestors, klass)

test/rdoc/test_rdoc_generator_darkfish.rb

+20
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,26 @@ def test_meta_tags_for_rdoc_files
449449
)
450450
end
451451

452+
def test_meta_tags_for_markdwon_files_paragraph
453+
top_level = @store.add_file("README.md", parser: RDoc::Parser::Simple)
454+
top_level.comment = <<~MARKDOWN
455+
# Distributed Ruby: dRuby
456+
457+
dRuby is a distributed object system for Ruby. It allows an object.
458+
MARKDOWN
459+
460+
@g.generate
461+
462+
content = File.binread("README_md.html")
463+
assert_include(
464+
content,
465+
"<meta name=\"description\" content=\"" \
466+
"README: dRuby " \
467+
"dRuby is a distributed object system for Ruby. " \
468+
"It allows an object."
469+
)
470+
end
471+
452472
def test_meta_tags_for_markdown_files
453473
top_level = @store.add_file("MyPage.md", parser: RDoc::Parser::Markdown)
454474
top_level.comment = <<~MARKDOWN

0 commit comments

Comments
 (0)