|
22 | 22 | module AnnotateRoutes
|
23 | 23 | PREFIX = '== Route Map'.freeze
|
24 | 24 | PREFIX_MD = '## Route Map'.freeze
|
25 |
| - HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'] |
| 25 | + HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'].freeze |
| 26 | + |
| 27 | + MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze |
26 | 28 |
|
27 | 29 | class << self
|
28 | 30 | def do_annotations(options = {})
|
@@ -86,29 +88,37 @@ def header(options = {})
|
86 | 88 | end
|
87 | 89 | out << '' if magic_comments_map.any?
|
88 | 90 |
|
89 |
| - out += ["# #{options[:wrapper_open]}"] if options[:wrapper_open] |
| 91 | + out << comment(options[:wrapper_open]) if options[:wrapper_open] |
90 | 92 |
|
91 |
| - out += ["# #{options[:format_markdown] ? PREFIX_MD : PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '')] |
92 |
| - out += ['#'] |
| 93 | + out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '') |
| 94 | + out << comment |
93 | 95 | return out if routes_map.size.zero?
|
94 | 96 |
|
95 | 97 | maxs = [HEADER_ROW.map(&:size)] + routes_map[1..-1].map { |line| line.split.map(&:size) }
|
96 | 98 |
|
97 | 99 | if options[:format_markdown]
|
98 | 100 | max = maxs.map(&:max).compact.max
|
99 | 101 |
|
100 |
| - out += ["# #{content(HEADER_ROW, maxs, options)}"] |
101 |
| - out += ["# #{content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options)}"] |
| 102 | + out << comment(content(HEADER_ROW, maxs, options)) |
| 103 | + out << comment(content(['-' * max, '-' * max, '-' * max, '-' * max], maxs, options)) |
102 | 104 | else
|
103 |
| - out += ["# #{content(routes_map[0], maxs, options)}"] |
| 105 | + out << comment(content(routes_map[0], maxs, options)) |
104 | 106 | end
|
105 | 107 |
|
106 |
| - out += routes_map[1..-1].map { |line| "# #{content(options[:format_markdown] ? line.split(' ') : line, maxs, options)}" } |
107 |
| - out += ["# #{options[:wrapper_close]}"] if options[:wrapper_close] |
| 108 | + out += routes_map[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs, options)) } |
| 109 | + out << comment(options[:wrapper_close]) if options[:wrapper_close] |
108 | 110 |
|
109 | 111 | out
|
110 | 112 | end
|
111 | 113 |
|
| 114 | + def comment(row = '') |
| 115 | + if row == '' |
| 116 | + '#' |
| 117 | + else |
| 118 | + "# #{row}" |
| 119 | + end |
| 120 | + end |
| 121 | + |
112 | 122 | # TODO: write the method doc using ruby rdoc formats
|
113 | 123 | # This method returns an array of 'real_content' and 'header_position'.
|
114 | 124 | # 'header_position' will either be :before, :after, or
|
@@ -212,7 +222,7 @@ def extract_magic_comments_from_array(content_array)
|
212 | 222 | new_content = []
|
213 | 223 |
|
214 | 224 | content_array.each do |row|
|
215 |
| - if row =~ magic_comment_matcher |
| 225 | + if row =~ MAGIC_COMMENT_MATCHER |
216 | 226 | magic_comments << row.strip
|
217 | 227 | else
|
218 | 228 | new_content << row
|
@@ -244,9 +254,5 @@ def real_content_and_header_position(real_content, header_position)
|
244 | 254 | # and the default
|
245 | 255 | return real_content, header_position
|
246 | 256 | end
|
247 |
| - |
248 |
| - def magic_comment_matcher |
249 |
| - Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/) |
250 |
| - end |
251 | 257 | end
|
252 | 258 | end
|
0 commit comments