Skip to content

Commit dce7139

Browse files
committed
Infer title and heading from comment
Some classes in Rails have a h1 heading defined in the RDoc comment. For example: https://github.com/rails/rails/blob/4e1fe73028f4b01c8e5179989511b21925f70b20/activesupport/lib/active_support/current_attributes.rb#L8 https://github.com/rails/rails/blob/4e1fe73028f4b01c8e5179989511b21925f70b20/actionpack/lib/action_controller/metal/strong_parameters.rb#L1159 This makes the heading more readable especially when there are a lot of namespaces. For SEO it helps if all classes have a h1 heading, but having to add them by hand is cumbersome when we can generate them instead. By looking at the comment of the class/module we can see if it has a `h1` heading when it's starting with `= ` or `# `. If the heading is missing we can generate a heading with the full name of the class instead. This will prevent us from having to add the h1 for each class in Rails. This also adds the `@options.title` to the `title` tag of every class.
1 parent 9047b7c commit dce7139

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

lib/rdoc/generator/template/rails/_context.rhtml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<div id="content">
22
<% unless (description = context.description).empty? %>
3+
<% unless context.comment_title %>
4+
<h1><%= context.title %></h1>
5+
<% end %>
36
<div class="description">
47
<%= description %>
58
</div>

lib/rdoc/generator/template/rails/class.rhtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<title><%= h klass.full_name %></title>
4+
<title><%= h @options.title %> | <%= h klass.title %></title>
55
<meta charset="<%= @options.charset %>" />
66
<meta name="viewport" content="width=device-width,initial-scale=1">
77
<%= include_template '_head.rhtml', {:context => klass, :rel_prefix => rel_prefix, :tree_keys => klass.full_name.split('::') } %>

lib/sdoc/generator.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ class RDoc::Options
2020
attr_accessor :search_index
2121
end
2222

23+
module RDoc::Generator::Markup
24+
def comment_title
25+
@comment_title ||= @comment.to_s.match(/\A[=#] (.*)$/) {|match| match[1] }
26+
end
27+
28+
def title
29+
comment_title || full_name
30+
end
31+
end
32+
2333
class RDoc::Generator::SDoc
2434
RDoc::RDoc.add_generator self
2535

spec/rdoc_generator_markup_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require File.join(File.dirname(__FILE__), '/spec_helper')
2+
3+
describe RDoc::Generator::Markup do
4+
before :each do
5+
@module = RDoc::NormalModule.new 'Example::SomeClass'
6+
end
7+
8+
describe "#comment_title" do
9+
it "should parse the h1 title from the comment if present" do
10+
@module.comment = RDoc::Comment.new '= Some Title'
11+
_(@module.comment_title).must_equal 'Some Title'
12+
end
13+
14+
it "should parse the markdown h1 title from the comment if present" do
15+
@module.comment = RDoc::Comment.new '# Markdown Title'
16+
_(@module.comment_title).must_equal 'Markdown Title'
17+
end
18+
19+
it "should ignore lower level titles" do
20+
@module.comment = RDoc::Comment.new '== Some Title'
21+
_(@module.comment_title).must_equal nil
22+
end
23+
end
24+
25+
describe "#title" do
26+
it "should parse the h1 title from the comment if present" do
27+
@module.comment = RDoc::Comment.new '= Some Title'
28+
_(@module.title).must_equal 'Some Title'
29+
end
30+
31+
it "should fallback to the full_name" do
32+
@module.comment = RDoc::Comment.new 'Some comment without title'
33+
_(@module.title).must_equal "Example::SomeClass"
34+
end
35+
end
36+
end

0 commit comments

Comments
 (0)