Class: RDoc::Context::Section

Inherits:
Object
  • Object
show all
Includes:
Generator::Markup, Text
Defined in:
lib/rdoc/context/section.rb,
lib/rdoc/generator/markup.rb

Overview

frozen_string_literal: false

A section of documentation like:

# :section: The title
# The body

Sections can be referenced multiple times and will be collapsed into a single section.

Constant Summary collapse

MARSHAL_VERSION =

:nodoc:

0
@@sequence =
"SEC00000"

Constants included from Text

Text::MARKUP_FORMAT, Text::TO_HTML_CHARACTERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Generator::Markup

#aref_to, #as_href, #cvs_url, #description, #formatter

Methods included from Text

encode_fallback, #expand_tabs, #flush_left, #markup, #normalize_comment, #snippet, #strip_hashes, #strip_newlines, #strip_stars, #to_html, #wrap

Constructor Details

#initialize(parent, title, comment) ⇒ Section

Creates a new section with title and comment



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rdoc/context/section.rb', line 42

def initialize parent, title, comment
  @parent = parent
  @title = title ? title.strip : title

  @@sequence.succ!
  @sequence = @@sequence.dup

  @comments = []

  add_comment comment
end

Instance Attribute Details

#commentObject (readonly)

Section comment



20
21
22
# File 'lib/rdoc/context/section.rb', line 20

def comment
  @comment
end

#commentsObject (readonly)

Section comments



25
26
27
# File 'lib/rdoc/context/section.rb', line 25

def comments
  @comments
end

#parentObject (readonly)

Context this Section lives in



30
31
32
# File 'lib/rdoc/context/section.rb', line 30

def parent
  @parent
end

#titleObject (readonly)

Section title



35
36
37
# File 'lib/rdoc/context/section.rb', line 35

def title
  @title
end

Instance Method Details

#==(other) ⇒ Object

Sections are equal when they have the same #title



57
58
59
# File 'lib/rdoc/context/section.rb', line 57

def == other
  self.class === other and @title == other.title
end

#add_comment(comment) ⇒ Object

Adds comment to this section



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rdoc/context/section.rb', line 64

def add_comment comment
  comment = extract_comment comment

  return if comment.empty?

  case comment
  when RDoc::Comment then
    @comments << comment
  when RDoc::Markup::Document then
    @comments.concat comment.parts
  when Array then
    @comments.concat comment
  else
    raise TypeError, "unknown comment type: #{comment.inspect}"
  end
end

#arefObject

Anchor reference for linking to this section



84
85
86
87
88
# File 'lib/rdoc/context/section.rb', line 84

def aref
  title = @title || '[untitled]'

  CGI.escape(title).gsub('%', '-').sub(/^-/, '')
end

#extract_comment(comment) ⇒ Object

Extracts the comment for this section from the original comment block. If the first line contains :section:, strip it and use the rest. Otherwise remove lines up to the line containing :section:, and look for those lines again at the end and remove them. This lets us write

# :section: The title
# The body


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rdoc/context/section.rb', line 99

def extract_comment comment
  case comment
  when Array then
    comment.map do |c|
      extract_comment c
    end
  when nil
    RDoc::Comment.new ''
  when RDoc::Comment then
    if comment.text =~ /^#[ \t]*:section:.*\n/ then
      start = $`
      rest = $'

      comment.text = if start.empty? then
                       rest
                     else
                       rest.sub(/#{start.chomp}\Z/, '')
                     end
    end

    comment
  when RDoc::Markup::Document then
    comment
  else
    raise TypeError, "unknown comment #{comment.inspect}"
  end
end

#in_filesObject

The files comments in this section come from



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rdoc/context/section.rb', line 134

def in_files
  return [] if @comments.empty?

  case @comments
  when Array then
    @comments.map do |comment|
      comment.file
    end
  when RDoc::Markup::Document then
    @comment.parts.map do |document|
      document.file
    end
  else
    raise RDoc::Error, "BUG: unknown comment class #{@comments.class}"
  end
end

#inspectObject

:nodoc:



127
128
129
# File 'lib/rdoc/context/section.rb', line 127

def inspect # :nodoc:
  "#<%s:0x%x %p>" % [self.class, object_id, title]
end

#marshal_dumpObject

Serializes this Section. The title and parsed comment are saved, but not the section parent which must be restored manually.



155
156
157
158
159
160
161
# File 'lib/rdoc/context/section.rb', line 155

def marshal_dump
  [
    MARSHAL_VERSION,
    @title,
    parse,
  ]
end

#marshal_load(array) ⇒ Object

De-serializes this Section. The section parent must be restored manually.



166
167
168
169
170
171
# File 'lib/rdoc/context/section.rb', line 166

def marshal_load array
  @parent  = nil

  @title    = array[1]
  @comments = array[2]
end

#parseObject

Parses comment_location into an RDoc::Markup::Document composed of multiple RDoc::Markup::Documents with their file set.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/rdoc/context/section.rb', line 177

def parse
  case @comments
  when String then
    super
  when Array then
    docs = @comments.map do |comment, location|
      doc = super comment
      doc.file = location if location
      doc
    end

    RDoc::Markup::Document.new(*docs)
  when RDoc::Comment then
    doc = super @comments.text, comments.format
    doc.file = @comments.location
    doc
  when RDoc::Markup::Document then
    return @comments
  else
    raise ArgumentError, "unknown comment class #{comments.class}"
  end
end

#plain_htmlObject

The section’s title, or ‘Top Section’ if the title is nil.

This is used by the table of contents template so the name is silly.



205
206
207
# File 'lib/rdoc/context/section.rb', line 205

def plain_html
  @title || 'Top Section'
end

#remove_comment(comment) ⇒ Object

Removes a comment from this section if it is from the same file as comment



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/rdoc/context/section.rb', line 213

def remove_comment comment
  return if @comments.empty?

  case @comments
  when Array then
    @comments.delete_if do |my_comment|
      my_comment.file == comment.file
    end
  when RDoc::Markup::Document then
    @comments.parts.delete_if do |document|
      document.file == comment.file.name
    end
  else
    raise RDoc::Error, "BUG: unknown comment class #{@comments.class}"
  end
end

#sequenceObject

Section sequence number (deprecated)



233
234
235
236
# File 'lib/rdoc/context/section.rb', line 233

def sequence
  warn "RDoc::Context::Section#sequence is deprecated, use #aref"
  @sequence
end