Skip to content

Commit 5e736f0

Browse files
committed
refactor: RDoc markup handling for improved loading and initialization
1 parent e603300 commit 5e736f0

File tree

2 files changed

+25
-40
lines changed

2 files changed

+25
-40
lines changed

lib/yard/templates/helpers/markup/rdoc_markdown.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
# frozen_string_literal: true
2+
gem 'rdoc', '>= 6.0'
3+
require 'rdoc'
4+
require 'rdoc/markdown'
5+
26
module YARD
37
module Templates
48
module Helpers
59
module Markup
6-
begin require 'rdoc'; rescue LoadError; nil end
7-
begin
8-
require 'rdoc/markdown'
9-
rescue LoadError
10-
raise NameError, "could not load RDoc Markdown support (rdoc is too old)"
11-
end
12-
1310
class RDocMarkdown < RDocMarkup
1411
def initialize(text)
1512
super RDoc::Markdown.new.parse(text)

lib/yard/templates/helpers/markup/rdoc_markup.rb

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,18 @@
11
# frozen_string_literal: true
22
require 'thread'
33

4+
gem 'rdoc', '>= 6.0'
5+
require 'rdoc'
6+
require 'rdoc/markup'
7+
require 'rdoc/markup/to_html'
8+
49
module YARD
510
module Templates
611
module Helpers
712
module Markup
8-
begin require 'rdoc'; rescue LoadError; nil end
9-
begin
10-
require 'rdoc/markup'
11-
require 'rdoc/markup/to_html'
12-
class RDocMarkup; MARKUP = RDoc::Markup end
13-
class RDocMarkupToHtml < RDoc::Markup::ToHtml
14-
if defined?(RDoc::VERSION) && RDoc::VERSION >= '4.0.0' &&
15-
defined?(RDoc::Options)
16-
def initialize
17-
options = RDoc::Options.new
18-
options.pipe = true
19-
super(options)
20-
end
21-
end
22-
end
23-
rescue LoadError
24-
begin
25-
require 'rdoc/markup/simple_markup'
26-
require 'rdoc/markup/simple_markup/to_html'
27-
class RDocMarkup; MARKUP = SM::SimpleMarkup end
28-
class RDocMarkupToHtml < SM::ToHtml; end
29-
rescue LoadError
30-
raise NameError, "could not load RDocMarkup (rdoc is not installed)"
31-
end
32-
end
33-
3413
class RDocMarkup
14+
MARKUP = RDoc::Markup
15+
3516
attr_accessor :from_path
3617

3718
@@mutex = Mutex.new
@@ -63,8 +44,6 @@ def to_html
6344
private
6445

6546
# Fixes RDoc behaviour with ++ only supporting alphanumeric text.
66-
#
67-
# @todo Refactor into own SimpleMarkup subclass
6847
def fix_typewriter(text)
6948
code_tags = 0
7049
text.gsub(%r{<(/)?(pre|code|tt)|(\s|^|>)\+(?! )([^\n\+]{1,900})(?! )\+}) do |str|
@@ -82,18 +61,27 @@ def fix_typewriter(text)
8261
end
8362
end
8463

85-
# Don't allow -- to turn into &#8212; element. The chances of this being
86-
# some --option is far more likely than the typographical meaning.
87-
#
88-
# @todo Refactor into own SimpleMarkup subclass
64+
# Don't allow -- to turn into &#8212; element (em dash)
8965
def fix_dash_dash(text)
9066
text.gsub(/&#8212;(?=\S)/, '--')
9167
end
9268
end
9369

94-
class RDocMarkupToHtml
70+
# Specialized ToHtml formatter for YARD
71+
#
72+
# @todo Refactor into own SimpleMarkup subclass
73+
class RDocMarkupToHtml < RDoc::Markup::ToHtml
9574
attr_accessor :from_path
9675

76+
def initialize
77+
options = RDoc::Options.new
78+
options.pipe = true
79+
super(options)
80+
81+
# The hyperlink detection state
82+
@hyperlink = false
83+
end
84+
9785
# Disable auto-link of URLs
9886
def handle_special_HYPERLINK(special) # rubocop:disable Style/MethodName
9987
@hyperlink ? special.text : super

0 commit comments

Comments
 (0)