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

Add support for meta tags #1091

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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: 14 additions & 0 deletions lib/rdoc/generator/darkfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -783,4 +783,18 @@ def template_for file, page = true, klass = ERB
template
end

# Returns an excerpt of the content for usage in meta description tags
def excerpt(content)
text = content.is_a?(RDoc::Comment) ? content.text : content

# Try to select the text up to the second paragraph, the first paragraph or the first 150 characters
excerpt_end = text.index(".")
excerpt_end = if excerpt_end
text.index(".", excerpt_end + 1) || excerpt_end
else
150
end

text[0...excerpt_end].gsub(/\n/, ' ').squeeze(" ")
end
end
22 changes: 22 additions & 0 deletions lib/rdoc/generator/template/darkfish/_head.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

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

<%- if defined?(klass) -%>
<meta name="keywords" content="ruby,<%= h "#{klass.type},#{klass.full_name}" %>">

<%- if klass.comment.empty? -%>
<meta name="description" content="Documentation for the <%= h "#{klass.full_name} #{klass.type}" %>">
<%- else -%>
<meta name="description" content="<%= h "#{klass.type} #{klass.full_name}: #{excerpt(klass.comment)}" %>">
<%- end -%>
<%- elsif defined?(file) -%>
<meta name="keywords" content="ruby,documentation,<%= h file.page_name %>">
<meta name="description" content="<%= h "#{file.page_name}: #{excerpt(file.comment)}" %>">
<%- elsif @title -%>
<meta name="keywords" content="ruby,documentation,<%= h @title %>">

<%- if @options.main_page and
main_page = @files.find { |f| f.full_name == @options.main_page } then %>
<meta name="description" content="<%= h "#{@title}: #{excerpt(main_page.comment)}" %>">
<%- else -%>
<meta name="description" content="Documentation for <%= h @title %>">
<%- end -%>
<%- end -%>

<script type="text/javascript">
var rdoc_rel_prefix = "<%= h asset_rel_prefix %>/";
var index_rel_prefix = "<%= h rel_prefix %>/";
Expand Down
45 changes: 45 additions & 0 deletions test/rdoc/test_rdoc_generator_darkfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,51 @@ def test_title_escape
assert_main_title(File.binread('index.html'), title)
end

def test_meta_tags_for_index
@options.title = "My awesome Ruby project"
@g.generate

content = File.binread("index.html")

assert_include(content, '<meta name="keywords" content="ruby,documentation,My awesome Ruby project">')
assert_include(content, '<meta name="description" content="Documentation for My awesome Ruby project">')
end

def test_meta_tags_for_classes
top_level = @store.add_file("file.rb")
top_level.add_class(@klass.class, @klass.name)
inner = @klass.add_class(RDoc::NormalClass, "Inner")
inner.add_comment("This is a normal class. It is fully documented.", top_level)

@g.generate

content = File.binread("Klass/Inner.html")
assert_include(content, '<meta name="keywords" content="ruby,class,Klass::Inner">')
assert_include(
content,
'<meta name="description" content="class Klass::Inner: This is a normal class. It is fully documented">',
)
end

def test_meta_tags_for_pages
top_level = @store.add_file("CONTRIBUTING.rdoc", parser: RDoc::Parser::Simple)
top_level.comment = <<~RDOC
= Contributing

Here are the instructions for contributing. Begin by installing Ruby.
RDOC

@g.generate

content = File.binread("CONTRIBUTING_rdoc.html")
assert_include(content, '<meta name="keywords" content="ruby,documentation,CONTRIBUTING">')
assert_include(
content,
"<meta name=\"description\" content=\"CONTRIBUTING: = Contributing Here are the instructions for contributing." \
" Begin by installing Ruby\">",
)
end

##
# Asserts that +filename+ has a link count greater than 1 if hard links to
# @tmpdir are supported.
Expand Down