Skip to content

Infer title and heading from comment #223

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

Merged
merged 1 commit into from
Aug 1, 2023
Merged
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
3 changes: 3 additions & 0 deletions lib/rdoc/generator/template/rails/_context.rhtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<div id="content">
<% unless (description = context.description).empty? %>
<% unless context.comment_title %>
<h1><%= context.title %></h1>
<% end %>
<div class="description">
<%= description %>
</div>
Expand Down
10 changes: 10 additions & 0 deletions lib/sdoc/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ class RDoc::Options
attr_accessor :search_index
end

module RDoc::Generator::Markup
def comment_title
@comment_title ||= @comment.to_s.match(/\A[=#] (.*)$/) {|match| match[1] }
end

def title
comment_title || full_name
end
end

class RDoc::Generator::SDoc
RDoc::RDoc.add_generator self

Expand Down
36 changes: 36 additions & 0 deletions spec/rdoc_generator_markup_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require File.join(File.dirname(__FILE__), '/spec_helper')

describe RDoc::Generator::Markup do
before :each do
@module = RDoc::NormalModule.new 'Example::SomeClass'
end

describe "#comment_title" do
it "should parse the h1 title from the comment if present" do
@module.comment = RDoc::Comment.new '= Some Title'
_(@module.comment_title).must_equal 'Some Title'
end

it "should parse the markdown h1 title from the comment if present" do
@module.comment = RDoc::Comment.new '# Markdown Title'
_(@module.comment_title).must_equal 'Markdown Title'
end

it "should ignore lower level titles" do
@module.comment = RDoc::Comment.new '== Some Title'
_(@module.comment_title).must_equal nil
end
end

describe "#title" do
it "should parse the h1 title from the comment if present" do
@module.comment = RDoc::Comment.new '= Some Title'
_(@module.title).must_equal 'Some Title'
end

it "should fallback to the full_name" do
@module.comment = RDoc::Comment.new 'Some comment without title'
_(@module.title).must_equal "Example::SomeClass"
end
end
end