1
1
# frozen_string_literal: true
2
2
require 'thread'
3
3
4
+ gem 'rdoc' , '>= 6.0'
5
+ require 'rdoc'
6
+ require 'rdoc/markup'
7
+ require 'rdoc/markup/to_html'
8
+
4
9
module YARD
5
10
module Templates
6
11
module Helpers
7
12
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
-
34
13
class RDocMarkup
14
+ MARKUP = RDoc ::Markup
15
+
35
16
attr_accessor :from_path
36
17
37
- @@mutex = Mutex . new
38
- @@formatter = nil
39
- @@markup = nil
18
+ # Class instance variables initialized once
19
+ @mutex = Mutex . new
20
+ @formatter = nil
21
+ @markup = nil
40
22
41
- def initialize ( text )
42
- @text = text
23
+ class << self
24
+ attr_reader :mutex , :formatter , :markup
25
+
26
+ def initialize_markup_components
27
+ return if @markup && @formatter
43
28
44
- @@mutex . synchronize do
45
- @@formatter ||= RDocMarkupToHtml . new
46
- @@markup ||= MARKUP . new
29
+ @mutex . synchronize do
30
+ @formatter ||= RDocMarkupToHtml . new
31
+ @markup ||= MARKUP . new
32
+ end
47
33
end
48
34
end
49
35
36
+ def initialize ( text )
37
+ @text = text
38
+ self . class . initialize_markup_components
39
+ end
40
+
50
41
def to_html
51
42
html = nil
52
- @@ mutex. synchronize do
53
- @@ formatter. from_path = from_path
54
- html = @@ markup. convert ( @text , @@ formatter)
43
+ self . class . mutex . synchronize do
44
+ self . class . formatter . from_path = from_path
45
+ html = self . class . markup . convert ( @text , self . class . formatter )
55
46
end
56
- html = fix_dash_dash ( html )
57
- html = fix_typewriter ( html )
58
- html
47
+
48
+ fix_dash_dash ( fix_typewriter ( html ) )
59
49
end
60
50
61
51
private
62
52
63
53
# Fixes RDoc behaviour with ++ only supporting alphanumeric text.
64
- #
65
- # @todo Refactor into own SimpleMarkup subclass
66
54
def fix_typewriter ( text )
67
55
code_tags = 0
68
56
text . gsub ( %r{<(/)?(pre|code|tt)|(\s |^|>)\+ (?! )([^\n \+ ]{1,900})(?! )\+ } ) do |str |
@@ -76,33 +64,66 @@ def fix_typewriter(text)
76
64
next str
77
65
end
78
66
next str unless code_tags == 0
79
- first_text + ' <tt>' + type_text + ' </tt>'
67
+ " #{ first_text } <tt>#{ type_text } </tt>"
80
68
end
81
69
end
82
70
83
- # Don't allow -- to turn into — element. The chances of this being
84
- # some --option is far more likely than the typographical meaning.
85
- #
86
- # @todo Refactor into own SimpleMarkup subclass
71
+ # Don't allow -- to turn into — element (em dash)
87
72
def fix_dash_dash ( text )
88
73
text . gsub ( /—(?=\S )/ , '--' )
89
74
end
90
75
end
91
76
92
- class RDocMarkupToHtml
77
+ # Specialized ToHtml formatter for YARD
78
+ class RDocMarkupToHtml < RDoc ::Markup ::ToHtml
93
79
attr_accessor :from_path
94
80
81
+ def initialize
82
+ options = RDoc ::Options . new
83
+ options . pipe = true
84
+ super ( options )
85
+
86
+ # The hyperlink detection state
87
+ @hyperlink = false
88
+ end
89
+
95
90
# Disable auto-link of URLs
96
- def handle_special_HYPERLINK ( special ) # rubocop:disable Style/MethodName
91
+ def handle_special_HYPERLINK ( special )
97
92
@hyperlink ? special . text : super
98
93
end
99
94
100
95
def accept_paragraph ( *args )
101
96
par = args . last
102
97
text = par . respond_to? ( :txt ) ? par . txt : par . text
103
- @hyperlink = text =~ /\{ (https?:|mailto:|link:|www\. )/ ? true : false
98
+ @hyperlink = text =~ /\{ (https?:|mailto:|link:|www\. )/
104
99
super
105
100
end
101
+
102
+ # Override gen_url to support from_path in relative links
103
+ def gen_url ( url , text )
104
+ scheme , path , id = parse_url ( url )
105
+
106
+ if scheme == 'link' && !path . start_with? ( '/' ) && !from_path . nil? && !from_path . empty?
107
+ # Make the path relative to from_path for link: URLs
108
+ path = File . expand_path ( path , File . dirname ( from_path ) )
109
+ end
110
+
111
+ super ( "#{ scheme } :#{ path } #{ id } " , text )
112
+ end
113
+
114
+ # Override parse_url to handle custom schemes
115
+ def parse_url ( url )
116
+ case url
117
+ when /^mailto:(.*)$/i
118
+ [ 'mailto' , $1, '' ]
119
+ when /^(https?|ftp|irc):(.*)$/i
120
+ [ $1, $2, '' ]
121
+ when /^link:(.*)$/i
122
+ [ 'link' , $1, '' ]
123
+ else
124
+ [ 'http' , url , '' ]
125
+ end
126
+ end
106
127
end
107
128
end
108
129
end
0 commit comments