Skip to content

Commit 7c05c1f

Browse files
committed
Decouple Formatter from RDoc::Options
`Formatter#initialize` took an `RDoc::Options` object, but only 2 subclasses (`ToHtml`, `ToHtmlCrossref`) read from it — using just 6 specific boolean/array values. All other formatters passed `nil`. Replace the `options` parameter with explicit keyword arguments: - `ToHtml`: `pipe:`, `output_decoration:` - `ToHtmlCrossref`: `pipe:`, `output_decoration:`, `hyperlink_all:`, `show_hash:`, `autolink_excluded_words:`, `warn_missing_rdoc_ref:` Call sites (`Generator::Markup#formatter`, `RDoc::RDoc#handle_pipe`) now unpack the needed values from `RDoc::Options` at the boundary. `ToHtmlSnippet` and `Text#snippet` no longer receive or forward options. Delete `LinkLabelToHtml` (zero callers in the codebase). Update `CodeObject#options` comment to reflect remaining callers.
1 parent 77a80c7 commit 7c05c1f

21 files changed

+70
-75
lines changed

lib/rdoc/code_object.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ def ignored?
277277
# The options instance from the store this CodeObject is attached to, or a
278278
# default options instance if the CodeObject is not attached.
279279
#
280-
# This is used by Text#snippet
280+
# Used by: store= (visibility check), ClassModule#path, TopLevel#path,
281+
# ClassModule#embed_mixins
281282

282283
def options
283284
@store&.options || RDoc::Options.new

lib/rdoc/generator/markup.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ def formatter
3737
options = @store.options
3838
this = RDoc::Context === self ? self : @parent
3939

40-
@formatter = RDoc::Markup::ToHtmlCrossref.new options, this.path, this
40+
@formatter = RDoc::Markup::ToHtmlCrossref.new(
41+
this.path, this,
42+
pipe: options.pipe,
43+
output_decoration: options.output_decoration,
44+
hyperlink_all: options.hyperlink_all,
45+
show_hash: options.show_hash,
46+
autolink_excluded_words: options.autolink_excluded_words || [],
47+
warn_missing_rdoc_ref: options.warn_missing_rdoc_ref
48+
)
4149
@formatter.code_object = self
4250
@formatter
4351
end

lib/rdoc/markup/formatter.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ def self.gen_relative_url(path, target)
4848
##
4949
# Creates a new Formatter
5050

51-
def initialize(options)
52-
@options = options
53-
51+
def initialize
5452
@markup = RDoc::Markup.new
5553

5654
@from_path = '.'

lib/rdoc/markup/heading.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def self.to_label
5252
#: () -> RDoc::Markup::ToHtml
5353
def self.to_html
5454
@to_html ||= begin
55-
to_html = Markup::ToHtml.new nil
55+
to_html = Markup::ToHtml.new
5656

5757
def to_html.handle_regexp_CROSSREF(text)
5858
text.sub(/^\\/, '')

lib/rdoc/markup/to_html.rb

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter
4343
##
4444
# Creates a new formatter that will output HTML
4545

46-
def initialize(options)
47-
super
46+
def initialize(pipe: false, output_decoration: true)
47+
super()
4848

49+
@pipe = pipe
50+
@output_decoration = output_decoration
4951
@code_object = nil
5052
@from_path = ''
5153
@in_list_entry = nil
@@ -347,7 +349,7 @@ def accept_verbatim(verbatim)
347349
CGI.escapeHTML text
348350
end
349351

350-
if @options.pipe then
352+
if @pipe
351353
@res << "\n<pre><code>#{CGI.escapeHTML text}\n</code></pre>\n"
352354
else
353355
@res << "\n<pre#{klass}>#{content}</pre>\n"
@@ -418,17 +420,17 @@ def accept_heading(heading)
418420

419421
# Add legacy anchor before the heading for backward compatibility.
420422
# This allows old links with label- prefix to still work.
421-
if @options.output_decoration && !@options.pipe
423+
if @output_decoration && !@pipe
422424
@res << "\n<span id=\"#{legacy_label}\" class=\"legacy-anchor\"></span>"
423425
end
424426

425-
@res << if @options.output_decoration
427+
@res << if @output_decoration
426428
"\n<h#{level} id=\"#{label}\">"
427429
else
428430
"\n<h#{level}>"
429431
end
430432

431-
if @options.pipe
433+
if @pipe
432434
@res << to_html(heading.text)
433435
else
434436
@res << "<a href=\"##{label}\">#{to_html(heading.text)}</a>"
@@ -586,19 +588,3 @@ def to_html(item)
586588
to_html_characters(handle_inline(item))
587589
end
588590
end
589-
590-
##
591-
# Formatter dedicated to rendering tidy link labels without mutating the
592-
# calling formatter's state.
593-
594-
class RDoc::Markup::LinkLabelToHtml < RDoc::Markup::ToHtml
595-
def self.render(label, options, from_path)
596-
new(options, from_path).to_html(label)
597-
end
598-
599-
def initialize(options, from_path = nil)
600-
super(options)
601-
602-
self.from_path = from_path if from_path
603-
end
604-
end

lib/rdoc/markup/to_html_crossref.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@ class RDoc::Markup::ToHtmlCrossref < RDoc::Markup::ToHtml
2929
# references are removed unless +show_hash+ is true. Only method names
3030
# preceded by '#' or '::' are linked, unless +hyperlink_all+ is true.
3131

32-
def initialize(options, from_path, context)
32+
def initialize(from_path, context, pipe: false, output_decoration: true,
33+
hyperlink_all: false, show_hash: false,
34+
autolink_excluded_words: [], warn_missing_rdoc_ref: true)
3335
raise ArgumentError, 'from_path cannot be nil' if from_path.nil?
3436

35-
super options
37+
super(pipe: pipe, output_decoration: output_decoration)
3638

3739
@context = context
3840
@from_path = from_path
39-
@hyperlink_all = @options.hyperlink_all
40-
@show_hash = @options.show_hash
41+
@hyperlink_all = hyperlink_all
42+
@show_hash = show_hash
43+
@autolink_excluded_words = autolink_excluded_words
44+
@warn_missing_rdoc_ref = warn_missing_rdoc_ref
4145

4246
@cross_reference = RDoc::CrossReference.new @context
4347
end
@@ -48,7 +52,7 @@ def init_link_notation_regexp_handlings
4852

4953
# The crossref must be linked before tidylink because Klass.method[:sym]
5054
# will be processed as a tidylink first and will be broken.
51-
crossref_re = @options.hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP
55+
crossref_re = @hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP
5256
@markup.add_regexp_handling crossref_re, :CROSSREF
5357
end
5458

@@ -83,7 +87,7 @@ def cross_reference(name, text = nil, code = true, rdoc_ref: false)
8387

8488
def handle_regexp_CROSSREF(name)
8589
return convert_string(name) if in_tidylink_label?
86-
return name if @options.autolink_excluded_words&.include?(name)
90+
return name if @autolink_excluded_words&.include?(name)
8791

8892
return name if name =~ /@[\w-]+\.[\w-]/ # labels that look like emails
8993

@@ -159,7 +163,7 @@ def link(name, text, code = true, rdoc_ref: false)
159163

160164
case ref
161165
when String then
162-
if rdoc_ref && @options.warn_missing_rdoc_ref
166+
if rdoc_ref && @warn_missing_rdoc_ref
163167
puts "#{@from_path}: `rdoc-ref:#{name}` can't be resolved for `#{text}`"
164168
end
165169
ref
@@ -224,7 +228,7 @@ def apply_tidylink_label_special_handling(label, url)
224228
def tt_cross_reference(code)
225229
return if in_tidylink_label?
226230

227-
crossref_regexp = @options.hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP
231+
crossref_regexp = @hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP
228232
match = crossref_regexp.match(code)
229233
return unless match && match.begin(1).zero?
230234
return unless match.post_match.match?(/\A[[:punct:]\s]*\z/)

lib/rdoc/markup/to_html_snippet.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class RDoc::Markup::ToHtmlSnippet < RDoc::Markup::ToHtml
3434
# next word boundary after the given number of +characters+ or +paragraphs+
3535
# of text have been encountered.
3636

37-
def initialize(options, characters = 100, paragraphs = 3)
38-
super options
37+
def initialize(characters = 100, paragraphs = 3)
38+
super()
3939

4040
@character_limit = characters
4141
@paragraph_limit = paragraphs

lib/rdoc/markup/to_joined_paragraph.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class RDoc::Markup::ToJoinedParagraph < RDoc::Markup::Formatter
1111

1212
def initialize # :nodoc:
13-
super nil
13+
super
1414
end
1515

1616
def start_accepting # :nodoc:

lib/rdoc/markup/to_label.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class RDoc::Markup::ToLabel < RDoc::Markup::Formatter
1515
# Creates a new formatter that will output HTML-safe labels
1616

1717
def initialize
18-
super nil
18+
super
1919

2020
@markup.add_regexp_handling RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF
2121

lib/rdoc/markup/to_rdoc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class RDoc::Markup::ToRdoc < RDoc::Markup::Formatter
5353
# Creates a new formatter that will output (mostly) \RDoc markup
5454

5555
def initialize
56-
super nil
56+
super
5757

5858
@markup.add_regexp_handling(/\\\S/, :SUPPRESSED_CROSSREF)
5959
@width = 78

0 commit comments

Comments
 (0)