Skip to content

Commit 518bb40

Browse files
committed
Generate meta tags based on page's content
1 parent 7757fd9 commit 518bb40

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

lib/rdoc/generator/darkfish.rb

+15
Original file line numberDiff line numberDiff line change
@@ -780,4 +780,19 @@ def template_for file, page = true, klass = ERB
780780
template
781781
end
782782

783+
# Returns an excerpt of the content for usage in meta description tags
784+
def excerpt(content)
785+
text = content.is_a?(RDoc::Comment) ? content.text : content
786+
787+
# Match from a capital letter to the first period, discarding any links, so
788+
# that we don't end up matching badges in the README
789+
first_paragraph_match = text.match(/[A-Z][^\.:\/]+\./)
790+
return text[0...150].gsub(/\n/, " ").squeeze(" ") unless first_paragraph_match
791+
792+
extracted_text = first_paragraph_match[0]
793+
second_paragraph = first_paragraph_match.post_match.match(/[A-Z][^\.:\/]+\./)
794+
extracted_text << " " << second_paragraph[0] if second_paragraph
795+
796+
extracted_text[0...150].gsub(/\n/, " ").squeeze(" ")
797+
end
783798
end

lib/rdoc/generator/template/darkfish/_head.rhtml

+22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@
33

44
<title><%= h @title %></title>
55

6+
<%- if defined?(klass) -%>
7+
<meta name="keywords" content="ruby,<%= h "#{klass.type},#{klass.full_name}" %>">
8+
9+
<%- if klass.comment.empty? -%>
10+
<meta name="description" content="Documentation for the <%= h "#{klass.full_name} #{klass.type}" %>">
11+
<%- else -%>
12+
<meta name="description" content="<%= h "#{klass.type} #{klass.full_name}: #{excerpt(klass.comment)}" %>">
13+
<%- end -%>
14+
<%- elsif defined?(file) -%>
15+
<meta name="keywords" content="ruby,documentation,<%= h file.page_name %>">
16+
<meta name="description" content="<%= h "#{file.page_name}: #{excerpt(file.comment)}" %>">
17+
<%- elsif @title -%>
18+
<meta name="keywords" content="ruby,documentation,<%= h @title %>">
19+
20+
<%- if @options.main_page and
21+
main_page = @files.find { |f| f.full_name == @options.main_page } then %>
22+
<meta name="description" content="<%= h "#{@title}: #{excerpt(main_page.comment)}" %>">
23+
<%- else -%>
24+
<meta name="description" content="Documentation for <%= h @title %>">
25+
<%- end -%>
26+
<%- end -%>
27+
628
<script type="text/javascript">
729
var rdoc_rel_prefix = "<%= h asset_rel_prefix %>/";
830
var index_rel_prefix = "<%= h rel_prefix %>/";

test/rdoc/test_rdoc_generator_darkfish.rb

+45
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,51 @@ def test_title_escape
322322
assert_main_title(File.binread('index.html'), title)
323323
end
324324

325+
def test_meta_tags_for_index
326+
@options.title = "My awesome Ruby project"
327+
@g.generate
328+
329+
content = File.binread("index.html")
330+
331+
assert_include(content, '<meta name="keywords" content="ruby,documentation,My awesome Ruby project">')
332+
assert_include(content, '<meta name="description" content="Documentation for My awesome Ruby project">')
333+
end
334+
335+
def test_meta_tags_for_classes
336+
top_level = @store.add_file("file.rb")
337+
top_level.add_class(@klass.class, @klass.name)
338+
inner = @klass.add_class(RDoc::NormalClass, "Inner")
339+
inner.add_comment("This is a normal class. It is fully documented.", top_level)
340+
341+
@g.generate
342+
343+
content = File.binread("Klass/Inner.html")
344+
assert_include(content, '<meta name="keywords" content="ruby,class,Klass::Inner">')
345+
assert_include(
346+
content,
347+
'<meta name="description" content="class Klass::Inner: This is a normal class. It is fully documented.">',
348+
)
349+
end
350+
351+
def test_meta_tags_for_pages
352+
top_level = @store.add_file("CONTRIBUTING.rdoc", parser: RDoc::Parser::Simple)
353+
top_level.comment = <<~RDOC
354+
= Contributing
355+
356+
Here are the instructions for contributing. Begin by installing Ruby.
357+
RDOC
358+
359+
@g.generate
360+
361+
content = File.binread("CONTRIBUTING_rdoc.html")
362+
assert_include(content, '<meta name="keywords" content="ruby,documentation,CONTRIBUTING">')
363+
assert_include(
364+
content,
365+
"<meta name=\"description\" content=\"CONTRIBUTING: Contributing Here are the instructions for contributing." \
366+
" Begin by installing Ruby.\">",
367+
)
368+
end
369+
325370
##
326371
# Asserts that +filename+ has a link count greater than 1 if hard links to
327372
# @tmpdir are supported.

0 commit comments

Comments
 (0)