Skip to content
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: 2 additions & 1 deletion lib/rdoc/code_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ def ignored?
# The options instance from the store this CodeObject is attached to, or a
# default options instance if the CodeObject is not attached.
#
# This is used by Text#snippet
# Used by: store= (visibility check), ClassModule#path, TopLevel#path,
# ClassModule#embed_mixins

def options
@store&.options || RDoc::Options.new
Expand Down
10 changes: 9 additions & 1 deletion lib/rdoc/generator/markup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ def formatter
options = @store.options
this = RDoc::Context === self ? self : @parent

@formatter = RDoc::Markup::ToHtmlCrossref.new options, this.path, this
@formatter = RDoc::Markup::ToHtmlCrossref.new(
this.path, this,
pipe: options.pipe,
output_decoration: options.output_decoration,
hyperlink_all: options.hyperlink_all,
show_hash: options.show_hash,
autolink_excluded_words: options.autolink_excluded_words || [],
warn_missing_rdoc_ref: options.warn_missing_rdoc_ref
)
@formatter.code_object = self
@formatter
end
Expand Down
6 changes: 2 additions & 4 deletions lib/rdoc/markup/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ def self.gen_relative_url(path, target)
##
# Creates a new Formatter

def initialize(options, markup = nil)
@options = options

@markup = markup || RDoc::Markup.new
def initialize
@markup = RDoc::Markup.new

@from_path = '.'
end
Expand Down
5 changes: 1 addition & 4 deletions lib/rdoc/markup/heading.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ def self.to_label
#: () -> RDoc::Markup::ToHtml
def self.to_html
@to_html ||= begin
markup = Markup.new
markup.add_regexp_handling CrossReference::CROSSREF_REGEXP, :CROSSREF

to_html = Markup::ToHtml.new nil
to_html = Markup::ToHtml.new

def to_html.handle_regexp_CROSSREF(text)
text.sub(/^\\/, '')
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/markup/to_ansi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class RDoc::Markup::ToAnsi < RDoc::Markup::ToRdoc
##
# Creates a new ToAnsi visitor that is ready to output vibrant ANSI color!

def initialize(markup = nil)
def initialize
super

@headings.clear
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/markup/to_bs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class RDoc::Markup::ToBs < RDoc::Markup::ToRdoc
##
# Returns a new ToBs that is ready for hot backspace action!

def initialize(markup = nil)
def initialize
super

@in_b = false
Expand Down
30 changes: 8 additions & 22 deletions lib/rdoc/markup/to_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ class RDoc::Markup::ToHtml < RDoc::Markup::Formatter
##
# Creates a new formatter that will output HTML

def initialize(options, markup = nil)
super
def initialize(pipe: false, output_decoration: true)
super()

@pipe = pipe
@output_decoration = output_decoration
@code_object = nil
@from_path = ''
@in_list_entry = nil
Expand Down Expand Up @@ -347,7 +349,7 @@ def accept_verbatim(verbatim)
CGI.escapeHTML text
end

if @options.pipe then
if @pipe
@res << "\n<pre><code>#{CGI.escapeHTML text}\n</code></pre>\n"
else
@res << "\n<pre#{klass}>#{content}</pre>\n"
Expand Down Expand Up @@ -418,17 +420,17 @@ def accept_heading(heading)

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

@res << if @options.output_decoration
@res << if @output_decoration
"\n<h#{level} id=\"#{label}\">"
else
"\n<h#{level}>"
end

if @options.pipe
if @pipe
@res << to_html(heading.text)
else
@res << "<a href=\"##{label}\">#{to_html(heading.text)}</a>"
Expand Down Expand Up @@ -586,19 +588,3 @@ def to_html(item)
to_html_characters(handle_inline(item))
end
end

##
# Formatter dedicated to rendering tidy link labels without mutating the
# calling formatter's state.

class RDoc::Markup::LinkLabelToHtml < RDoc::Markup::ToHtml
def self.render(label, options, from_path)
new(options, from_path).to_html(label)
end

def initialize(options, from_path = nil)
super(options)

self.from_path = from_path if from_path
end
end
20 changes: 12 additions & 8 deletions lib/rdoc/markup/to_html_crossref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ class RDoc::Markup::ToHtmlCrossref < RDoc::Markup::ToHtml
# references are removed unless +show_hash+ is true. Only method names
# preceded by '#' or '::' are linked, unless +hyperlink_all+ is true.

def initialize(options, from_path, context, markup = nil)
def initialize(from_path, context, pipe: false, output_decoration: true,
hyperlink_all: false, show_hash: false,
autolink_excluded_words: [], warn_missing_rdoc_ref: true)
raise ArgumentError, 'from_path cannot be nil' if from_path.nil?

super options, markup
super(pipe: pipe, output_decoration: output_decoration)

@context = context
@from_path = from_path
@hyperlink_all = @options.hyperlink_all
@show_hash = @options.show_hash
@hyperlink_all = hyperlink_all
@show_hash = show_hash
@autolink_excluded_words = autolink_excluded_words
@warn_missing_rdoc_ref = warn_missing_rdoc_ref

@cross_reference = RDoc::CrossReference.new @context
end
Expand All @@ -48,7 +52,7 @@ def init_link_notation_regexp_handlings

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

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

def handle_regexp_CROSSREF(name)
return convert_string(name) if in_tidylink_label?
return name if @options.autolink_excluded_words&.include?(name)
return name if @autolink_excluded_words&.include?(name)

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

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

case ref
when String then
if rdoc_ref && @options.warn_missing_rdoc_ref
if rdoc_ref && @warn_missing_rdoc_ref
puts "#{@from_path}: `rdoc-ref:#{name}` can't be resolved for `#{text}`"
end
ref
Expand Down Expand Up @@ -224,7 +228,7 @@ def apply_tidylink_label_special_handling(label, url)
def tt_cross_reference(code)
return if in_tidylink_label?

crossref_regexp = @options.hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP
crossref_regexp = @hyperlink_all ? ALL_CROSSREF_REGEXP : CROSSREF_REGEXP
match = crossref_regexp.match(code)
return unless match && match.begin(1).zero?
return unless match.post_match.match?(/\A[[:punct:]\s]*\z/)
Expand Down
4 changes: 2 additions & 2 deletions lib/rdoc/markup/to_html_snippet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class RDoc::Markup::ToHtmlSnippet < RDoc::Markup::ToHtml
# next word boundary after the given number of +characters+ or +paragraphs+
# of text have been encountered.

def initialize(options, characters = 100, paragraphs = 3, markup = nil)
super options, markup
def initialize(characters = 100, paragraphs = 3)
super()

@character_limit = characters
@paragraph_limit = paragraphs
Expand Down
5 changes: 0 additions & 5 deletions lib/rdoc/markup/to_joined_paragraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
# other markup syntax items will not work.

class RDoc::Markup::ToJoinedParagraph < RDoc::Markup::Formatter

def initialize # :nodoc:
super nil
end

def start_accepting # :nodoc:
end

Expand Down
4 changes: 2 additions & 2 deletions lib/rdoc/markup/to_label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class RDoc::Markup::ToLabel < RDoc::Markup::Formatter
##
# Creates a new formatter that will output HTML-safe labels

def initialize(markup = nil)
super nil, markup
def initialize
super

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

Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/markup/to_markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class RDoc::Markup::ToMarkdown < RDoc::Markup::ToRdoc
##
# Creates a new formatter that will output Markdown format text

def initialize(markup = nil)
def initialize
super

@headings[1] = ['# ', '']
Expand Down
4 changes: 2 additions & 2 deletions lib/rdoc/markup/to_rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class RDoc::Markup::ToRdoc < RDoc::Markup::Formatter
##
# Creates a new formatter that will output (mostly) \RDoc markup

def initialize(markup = nil)
super nil, markup
def initialize
super

@markup.add_regexp_handling(/\\\S/, :SUPPRESSED_CROSSREF)
@width = 78
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/markup/to_table_of_contents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.to_toc
attr_accessor :omit_headings_below

def initialize # :nodoc:
super nil
super

@omit_headings_below = nil
end
Expand Down
7 changes: 0 additions & 7 deletions lib/rdoc/markup/to_tt_only.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ class RDoc::Markup::ToTtOnly < RDoc::Markup::Formatter

attr_reader :res

##
# Creates a new tt-only formatter.

def initialize(markup = nil)
super nil, markup
end

##
# Adds tts from +block_quote+ to the output

Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def gather_files(files)
# Turns RDoc from stdin into HTML

def handle_pipe
@html = RDoc::Markup::ToHtml.new @options
@html = RDoc::Markup::ToHtml.new(pipe: @options.pipe, output_decoration: @options.output_decoration)

parser = RDoc::Text::MARKUP_FORMAT[@options.markup]

Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def parse(text, format = 'rdoc')
def snippet(text, limit = 100)
document = parse text

RDoc::Markup::ToHtmlSnippet.new(options, limit).convert document
RDoc::Markup::ToHtmlSnippet.new(limit).convert document
end

##
Expand Down
5 changes: 3 additions & 2 deletions test/rdoc/markup/formatter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ class RDocMarkupFormatterTest < RDoc::TestCase

class ToTest < RDoc::Markup::Formatter

def initialize(markup)
super nil, markup
def initialize(markup = nil)
super()
@markup = markup if markup
end

def accept_paragraph(paragraph)
Expand Down
4 changes: 2 additions & 2 deletions test/rdoc/markup/markup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_convert

m = RDoc::Markup.new

tt = RDoc::Markup::ToTest.new m
tt = RDoc::Markup::ToTest.new

out = m.convert str, tt

Expand Down Expand Up @@ -55,7 +55,7 @@ def test_convert_document

m = RDoc::Markup.new

tt = RDoc::Markup::ToTest.new m
tt = RDoc::Markup::ToTest.new

out = m.convert doc, tt

Expand Down
27 changes: 13 additions & 14 deletions test/rdoc/markup/to_html_crossref_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ class RDocMarkupToHtmlCrossrefTest < XrefTestCase
def setup
super

@options.hyperlink_all = true
@options.warn_missing_rdoc_ref = true

@to = RDoc::Markup::ToHtmlCrossref.new @options, 'index.html', @c1
@to = RDoc::Markup::ToHtmlCrossref.new 'index.html', @c1,
hyperlink_all: true, warn_missing_rdoc_ref: true
end

def test_convert_CROSSREF
Expand All @@ -35,9 +33,7 @@ def test_convert_CROSSREF
end

def test_convert_CROSSREF_backslash_in_tt
@options.hyperlink_all = false

formatter = RDoc::Markup::ToHtmlCrossref.new(@options, 'C9.html', @c9_b)
formatter = RDoc::Markup::ToHtmlCrossref.new('C9.html', @c9_b)

result = formatter.convert '<tt>C1</tt>'
assert_equal para('<a href="C1.html"><code>C1</code></a>'), result
Expand All @@ -53,7 +49,9 @@ def test_convert_CROSSREF_backslash_in_tt
end

def test_convert_CROSSREF_ignored_excluded_words
@options.autolink_excluded_words = ['C1']
@to = RDoc::Markup::ToHtmlCrossref.new 'index.html', @c1,
hyperlink_all: true, warn_missing_rdoc_ref: true,
autolink_excluded_words: ['C1']

result = @to.convert 'C1'
assert_equal para("C1"), result
Expand Down Expand Up @@ -285,7 +283,8 @@ def test_handle_regexp_HYPERLINK_rdoc
readme = @store.add_file 'README.txt'
readme.parser = RDoc::Parser::Simple

@to = RDoc::Markup::ToHtmlCrossref.new @options, 'C2.html', @c2
@to = RDoc::Markup::ToHtmlCrossref.new 'C2.html', @c2,
hyperlink_all: true, warn_missing_rdoc_ref: true

link = @to.handle_regexp_HYPERLINK hyper 'C2::C3'

Expand All @@ -304,7 +303,8 @@ def test_handle_TIDYLINK_rdoc
readme = @store.add_file 'README.txt'
readme.parser = RDoc::Parser::Simple

@to = RDoc::Markup::ToHtmlCrossref.new @options, 'C2.html', @c2
@to = RDoc::Markup::ToHtmlCrossref.new 'C2.html', @c2,
hyperlink_all: true, warn_missing_rdoc_ref: true

link = @to.to_html tidy 'C2::C3'

Expand Down Expand Up @@ -339,9 +339,7 @@ def test_convert_TIDYLINK_markdown_with_crossrefs
end

def test_to_html_CROSSREF_email
@options.hyperlink_all = false

@to = RDoc::Markup::ToHtmlCrossref.new @options, 'index.html', @c1
@to = RDoc::Markup::ToHtmlCrossref.new 'index.html', @c1

result = @to.to_html 'first.last@example.com'

Expand All @@ -361,7 +359,8 @@ def test_link
end

def test_link_for_method_traverse
@to = RDoc::Markup::ToHtmlCrossref.new @options, 'C2.html', @c9
@to = RDoc::Markup::ToHtmlCrossref.new 'C2.html', @c9,
hyperlink_all: true, warn_missing_rdoc_ref: true
assert_equal '<a href="C9/A.html#method-i-foo"><code>C9::B#foo</code></a>', @to.link('C9::B#foo', 'C9::B#foo')
end

Expand Down
Loading