Skip to content

Commit 11f0df8

Browse files
authored
Merge pull request #223 from p8/features/default-titles
Infer title and heading from comment
2 parents 9c72aaa + 60e05b5 commit 11f0df8

File tree

3 files changed

+49
-0
lines changed

3 files changed

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

Lines changed: 10 additions & 0 deletions
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

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)