Skip to content

Commit 2293c87

Browse files
committed
Infer title and heading from comment
If the comment of the class/module doesn't have a `h1` defined 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 2293c87

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

Diff for: lib/rdoc/generator/template/rails/_context.rhtml

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<div id="content">
2+
<% unless context.comment_title %>
3+
<h1><%= context.title %></h1>
4+
<% end %>
25
<% unless (description = context.description).empty? %>
36
<div class="description">
47
<%= description %>

Diff for: lib/rdoc/generator/template/rails/class.rhtml

+1-1
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('::') } %>

Diff for: lib/sdoc/generator.rb

+10
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

Diff for: spec/rdoc_generator_markup_spec.rb

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 ignore lower level titles" do
15+
@module.comment = RDoc::Comment.new '== Some Title'
16+
_(@module.comment_title).must_equal nil
17+
end
18+
end
19+
20+
describe "#title" do
21+
it "should parse the h1 title from the comment if present" do
22+
@module.comment = RDoc::Comment.new '= Some Title'
23+
_(@module.title).must_equal 'Some Title'
24+
end
25+
26+
it "should fallback to the full_name" do
27+
@module.comment = RDoc::Comment.new 'Some comment without title'
28+
_(@module.title).must_equal "Example::SomeClass"
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)