Skip to content

Commit da0dff8

Browse files
committed
Extract directive regexp to constant
1 parent 15fa499 commit da0dff8

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

lib/rdoc/comment.rb

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ def tomdoc?
234234
# There are more, but already handled by RDoc::Parser::C
235235
COLON_LESS_DIRECTIVES = %w[call-seq Document-method].freeze # :nodoc:
236236

237-
private_constant :MULTILINE_DIRECTIVES, :COLON_LESS_DIRECTIVES
237+
DIRECTIVE_OR_ESCAPED_DIRECTIV_REGEXP = /\A(?<colon>\\?:|:?)(?<directive>[\w-]+):(?<param>.*)/
238+
239+
private_constant :MULTILINE_DIRECTIVES, :COLON_LESS_DIRECTIVES, :DIRECTIVE_OR_ESCAPED_DIRECTIV_REGEXP
238240

239241
class << self
240242

@@ -271,7 +273,7 @@ def from_document(document) # :nodoc:
271273
# # private comment
272274
# #++
273275

274-
def parse(text, filename, line_no, type)
276+
def parse(text, filename, line_no, type, &include_callback)
275277
case type
276278
when :ruby
277279
text = text.gsub(/^#+/, '') if text.start_with?('#')
@@ -283,8 +285,6 @@ def parse(text, filename, line_no, type)
283285
private_end_regexp = /^(\s*\*)?\+{2}$/
284286
indent_regexp = /^\s*(\/\*+|\*)?\s*/
285287
text = text.gsub(/\s*\*+\/\s*\z/, '')
286-
# TODO: should not be here. Looks like another type of directive
287-
# text = text.gsub %r%Document-method:\s+[\w:.#=!?|^&<>~+\-/*\%@`\[\]]+%, ''
288288
when :simple
289289
# Unlike other types, this implementation only looks for two dashes at
290290
# the beginning of the line. Three or more dashes are considered to be
@@ -302,10 +302,12 @@ def parse(text, filename, line_no, type)
302302
line = lines.shift
303303
read_lines = 1
304304
if in_private
305+
# If `++` appears in a private section that starts with `--`, private section ends.
305306
in_private = false if line.match?(private_end_regexp)
306307
line_no += read_lines
307308
next
308309
elsif line.match?(private_start_regexp)
310+
# If `--` appears in a line, private section starts.
309311
in_private = true
310312
line_no += read_lines
311313
next
@@ -314,20 +316,29 @@ def parse(text, filename, line_no, type)
314316
prefix = line[indent_regexp]
315317
prefix_indent = ' ' * prefix.size
316318
line = line.byteslice(prefix.bytesize..)
317-
/\A(?<colon>\\?:|:?)(?<directive>[\w-]+):(?<param>.*)/ =~ line
318319

319-
if colon == '\\:'
320-
# unescape if escaped
320+
if (directive_match = DIRECTIVE_OR_ESCAPED_DIRECTIV_REGEXP.match(line))
321+
colon = directive_match[:colon]
322+
directive = directive_match[:directive]
323+
raw_param = directive_match[:param]
324+
param = raw_param.strip
325+
else
326+
colon = directive = raw_param = param = nil
327+
end
328+
329+
if !directive
330+
comment_lines << prefix_indent + line
331+
elsif colon == '\\:'
332+
# If directive is escaped, unescape it
321333
comment_lines << prefix_indent + line.sub('\\:', ':')
322-
elsif !directive || param.start_with?(':') || (colon.empty? && !COLON_LESS_DIRECTIVES.include?(directive))
334+
elsif raw_param.start_with?(':') || (colon.empty? && !COLON_LESS_DIRECTIVES.include?(directive))
323335
# Something like `:toto::` is not a directive
324336
# Only few directives allows to start without a colon
325337
comment_lines << prefix_indent + line
326338
elsif directive == 'include'
327-
filename_to_include = param.strip
328-
yield(filename_to_include, prefix_indent).lines.each { |l| comment_lines << l.chomp }
339+
filename_to_include = param
340+
include_callback.call(filename_to_include, prefix_indent).lines.each { |l| comment_lines << l.chomp }
329341
elsif MULTILINE_DIRECTIVES.include?(directive)
330-
param = param.strip
331342
value_lines = take_multiline_directive_value_lines(directive, filename, line_no, lines, prefix_indent.size, indent_regexp, !param.empty?)
332343
read_lines += value_lines.size
333344
lines.shift(value_lines.size)
@@ -338,8 +349,7 @@ def parse(text, filename, line_no, type)
338349
value = value_lines.join("\n")
339350
directives[directive] = [value.empty? ? nil : value, line_no]
340351
else
341-
value = param.strip
342-
directives[directive] = [value.empty? ? nil : value, line_no]
352+
directives[directive] = [param.empty? ? nil : param, line_no]
343353
end
344354
line_no += read_lines
345355
end

0 commit comments

Comments
 (0)