Skip to content
Open
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/code_object/context/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ def legacy_aref
#
# # :section: The title
# # The body
#
#--
# TODO Remove when the ripper parser has been removed

def extract_comment(comment)
case comment
Expand Down
17 changes: 15 additions & 2 deletions lib/rdoc/parser/prism_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,28 @@ def parse_comment_text_to_directives(comment_text, start_line) # :nodoc:
comment.line = start_line
markup, = directives['markup']
comment.format = markup&.downcase || @markup
if (section, = directives['section'])
if (section, directive_line = directives['section'])
# If comment has :section:, it is not a documentable comment for a code object
@container.set_current_section(section, comment.dup)
comment.text = extract_section_comment(comment_text, directive_line - start_line)
@container.set_current_section(section, comment)
return
end
@preprocess.run_post_processes(comment, @container)
[comment, directives]
end

# Extracts the comment for this section from the normalized comment block.
# Removes all lines before the line that contains :section:
# If the comment also ends with the same content, remove it as well

def extract_section_comment(comment_text, prefix_line_count) # :nodoc:
prefix = comment_text.lines[0...prefix_line_count].join
comment_text.delete_prefix!(prefix)
# Comment is already normalized and doesn't end with a newline
comment_text.delete_suffix!(prefix.chomp)
comment_text
end

def slice_tokens(start_pos, end_pos) # :nodoc:
start_index = @tokens.bsearch_index { |t| ([t.line_no, t.char_no] <=> start_pos) >= 0 }
end_index = @tokens.bsearch_index { |t| ([t.line_no, t.char_no] <=> end_pos) >= 0 }
Expand Down
35 changes: 35 additions & 0 deletions test/rdoc/parser/prism_ruby_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,41 @@ def test_look_for_directives_in_section
assert_equal 'new section', section.title
end

def test_section_with_divider
util_parser <<~RUBY
# DIVIDER
# :section: section 1
# foo
# DIVIDER

# DIVIDER 1
# DIVIDER 2
# :section: section 2
# foo
# DIVIDER 1
# DIVIDER 2

# DIVIDER TOP ONLY
# :section: section 3
# foo

# DIVIDER TOP ONLY
# :section: section 4
RUBY

section = @top_level.sections_hash['section 1']
assert_equal "\n<p>foo</p>\n", section.description

section = @top_level.sections_hash['section 2']
assert_equal "\n<p>foo</p>\n", section.description

section = @top_level.sections_hash['section 3']
assert_equal "\n<p>foo</p>\n", section.description

section = @top_level.sections_hash['section 4']
assert_equal '', section.description
end

def test_look_for_directives_in_commented
util_parser <<~RUBY
# how to make a section:
Expand Down
Loading