Skip to content

Commit 60e05b5

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 9c72aaa commit 60e05b5

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

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

+3
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/sdoc/generator.rb

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ class RDoc::Options
1919
attr_accessor :search_index
2020
end
2121

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

spec/rdoc_generator_markup_spec.rb

+36
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)