mvz-dnote 1.7.2 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,35 +1,32 @@
1
- module DNote
1
+ # frozen_string_literal: true
2
2
 
3
+ module DNote
3
4
  # The Note class encapsulates a single note made in a source file.
4
5
  #
5
6
  # Each note instance holds a reference, +notes+, to the set of notes
6
7
  # being generated for a given session. This allows the note to access
7
8
  # general options applicable to all notes.
8
9
  class Note
9
-
10
- # Number of lines to provide in source context.
11
- #CONTEXT_DEPTH = 5
12
-
13
10
  # Set of notes to which this note belongs.
14
- attr :notes
11
+ attr_reader :notes
15
12
 
16
13
  # The file in which the note is made.
17
- attr :file
14
+ attr_reader :file
18
15
 
19
16
  # The type of note.
20
- attr :label
17
+ attr_reader :label
21
18
 
22
19
  # The line number of the note.
23
- attr :line
20
+ attr_reader :line
24
21
 
25
22
  # The verbatim text of the note.
26
- attr :text
23
+ attr_reader :text
27
24
 
28
25
  # Remark marker used in parsing the note.
29
- attr :mark
26
+ attr_reader :mark
30
27
 
31
28
  # Contextual lines of code.
32
- attr :capture
29
+ attr_reader :capture
33
30
 
34
31
  # Initialize new Note instance.
35
32
  def initialize(notes, file, label, line, text, mark)
@@ -55,13 +52,14 @@ module DNote
55
52
 
56
53
  # Remove newlines from note text.
57
54
  def textline
58
- text.gsub("\n", " ")
55
+ text.tr("\n", ' ')
59
56
  end
60
57
 
61
58
  # Sort by file name and line number.
62
59
  def <=>(other)
63
60
  s = file <=> other.file
64
- return s unless s == 0
61
+ return s unless s.zero?
62
+
65
63
  line <=> other.line
66
64
  end
67
65
 
@@ -71,12 +69,12 @@ module DNote
71
69
  # TODO: Add +code+? Problem is that xml needs code in CDATA.
72
70
  #++
73
71
  def to_h
74
- { 'label'=>label, 'text'=>textline, 'file'=>file, 'line'=>line }
72
+ { 'label' => label, 'text' => textline, 'file' => file, 'line' => line }
75
73
  end
76
74
 
77
75
  # Convert to Hash, leaving the note text verbatim.
78
76
  def to_h_raw
79
- { 'label'=>label, 'text'=>text, 'file'=>file, 'line'=>line, 'code'=>code }
77
+ { 'label' => label, 'text' => text, 'file' => file, 'line' => line, 'code' => code }
80
78
  end
81
79
 
82
80
  # Convert to JSON.
@@ -93,13 +91,12 @@ module DNote
93
91
  # returns the file.
94
92
  def url
95
93
  if notes.url
96
- notes.url % [file, line]
94
+ format(notes.url, file, line)
97
95
  else
98
96
  file
99
97
  end
100
98
  end
101
99
 
102
- #
103
100
  def code
104
101
  unindent(capture).join
105
102
  end
@@ -109,42 +106,20 @@ module DNote
109
106
  !capture.empty?
110
107
  end
111
108
 
112
- =begin
113
- # This isn't being used currently b/c the URL solution as deeemd better,
114
- # but the code is here for custom templates.
115
- def capture
116
- @context ||= (
117
- lines = file_cache(file) #.lines.to_a
118
- count = line()
119
- count +=1 while /^\s*#{mark}/ =~ lines[count]
120
- lines[count, context_depth]
121
- )
122
- end
123
-
124
- # Read in +file+, parse into lines and cache.
125
- def file_cache(file)
126
- @@file_cache ||= {}
127
- @@file_cache[file] ||= File.read(file).lines.to_a
128
- end
129
- =end
130
-
131
109
  private
132
110
 
133
111
  # Remove blank space from lines.
134
112
  def unindent(lines)
135
113
  dents = []
136
114
  lines.each do |line|
137
- if md = /^([\ ]*)/.match(line)
138
- size = md[1].size
115
+ if (md = /^([\ ]*)/.match(line))
139
116
  dents << md[1]
140
117
  end
141
118
  end
142
- dent = dents.min{ |a,b| a.size <=> b.size }
119
+ dent = dents.min_by(&:size)
143
120
  lines.map do |line|
144
121
  line.sub(dent, '')
145
122
  end
146
123
  end
147
-
148
124
  end
149
-
150
125
  end
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'pathname'
2
4
  require 'dnote/note'
3
5
 
4
6
  module DNote
5
-
6
7
  # = Developer Notes
7
8
  #
8
9
  # This class goes through you source files and compiles a list
@@ -20,10 +21,10 @@ module DNote
20
21
  include Enumerable
21
22
 
22
23
  # Default paths (all ruby scripts).
23
- DEFAULT_PATHS = ["**/*.rb"]
24
+ DEFAULT_PATHS = ['**/*.rb'].freeze
24
25
 
25
26
  # Default note labels to look for in source code. (NOT CURRENTLY USED!)
26
- DEFAULT_LABELS = ['TODO', 'FIXME', 'OPTIMIZE', 'THINK', 'DEPRECATE']
27
+ DEFAULT_LABELS = %w(TODO FIXME OPTIMIZE THINK DEPRECATE).freeze
27
28
 
28
29
  # Files to search for notes.
29
30
  attr_accessor :files
@@ -44,11 +45,11 @@ module DNote
44
45
  attr_accessor :context
45
46
 
46
47
  # New set of notes for give +files+ and optional special labels.
47
- def initialize(files, options={})
48
+ def initialize(files, options = {})
48
49
  @files = [files].flatten
49
50
  @labels = [options[:labels] || DEFAULT_LABELS].flatten.compact
50
51
  @colon = options[:colon].nil? ? true : options[:colon]
51
- @marker = options[:marker] #|| '#'
52
+ @marker = options[:marker]
52
53
  @url = options[:url]
53
54
  @context = options[:context] || 0
54
55
 
@@ -58,19 +59,17 @@ module DNote
58
59
  end
59
60
 
60
61
  # Array of notes.
61
- def notes
62
- @notes
63
- end
62
+ attr_reader :notes
64
63
 
65
64
  # Notes counts by label.
66
65
  def counts
67
- @counts ||= (
66
+ @counts ||= begin
68
67
  h = {}
69
68
  by_label.each do |label, notes|
70
69
  h[label] = notes.size
71
70
  end
72
71
  h
73
- )
72
+ end
74
73
  end
75
74
 
76
75
  # Iterate through notes.
@@ -91,46 +90,41 @@ module DNote
91
90
  records = []
92
91
  files.each do |fname|
93
92
  next unless File.file?(fname)
94
- #next unless fname =~ /\.rb$/ # TODO: should this be done?
93
+
95
94
  mark = remark(fname)
96
- lineno, note, text, capt = 0, nil, nil, nil
95
+ lineno = 0
96
+ note = nil
97
+ text = nil
98
+ capt = nil
97
99
  File.readlines(fname).each do |line|
98
- #while line = f.gets
99
- lineno += 1
100
- note = match(line, lineno, fname)
101
- if note
102
- #file = fname
103
- text = note.text
104
- capt = note.capture
105
- #note = {'label'=>label,'file'=>file,'line'=>line_no,'note'=>text}
106
- records << note
107
- else
108
- if text
109
- case line
110
- when /^\s*#{mark}+\s*$/, /^\s*#{mark}\-\-/, /^\s*#{mark}\+\+/
111
- text.strip!
112
- text = nil
113
- when /^\s*#{mark}/
114
- if text[-1,1] == "\n"
115
- text << line.gsub(/^\s*#{mark}\s*/,'')
116
- else
117
- text << "\n" << line.gsub(/^\s*#{mark}\s*/,'')
118
- end
119
- else
120
- text.strip!
121
- text = nil
122
- end
100
+ lineno += 1
101
+ note = match(line, lineno, fname)
102
+ if note
103
+ text = note.text
104
+ capt = note.capture
105
+ records << note
106
+ elsif text
107
+ case line
108
+ when /^\s*#{mark}+\s*$/, /^\s*#{mark}\-\-/, /^\s*#{mark}\+\+/
109
+ text.strip!
110
+ text = nil
111
+ when /^\s*#{mark}/
112
+ if text[-1, 1] == "\n"
113
+ text << line.gsub(/^\s*#{mark}\s*/, '')
123
114
  else
124
- if line !~ /^\s*#{mark}/
125
- capt << line if capt && capt.size < context
126
- end
115
+ text << "\n" << line.gsub(/^\s*#{mark}\s*/, '')
127
116
  end
117
+ else
118
+ text.strip!
119
+ text = nil
128
120
  end
129
- #end
121
+ elsif line !~ /^\s*#{mark}/
122
+ capt << line if capt && capt.size < context
123
+ end
130
124
  end
131
125
  end
132
126
 
133
- @notes = records.sort
127
+ @notes = records.sort
134
128
  end
135
129
 
136
130
  # Is this line a note?
@@ -146,9 +140,8 @@ module DNote
146
140
  def match_special(line, lineno, file)
147
141
  rec = nil
148
142
  labels.each do |label|
149
- if md = match_special_regex(label, file).match(line)
143
+ if (md = match_special_regex(label, file).match(line))
150
144
  text = md[1]
151
- #rec = {'label'=>label,'file'=>file,'line'=>lineno,'note'=>text}
152
145
  rec = Note.new(self, file, label, lineno, text, remark(file))
153
146
  end
154
147
  end
@@ -156,7 +149,7 @@ module DNote
156
149
  end
157
150
 
158
151
  #--
159
- # TODO: ruby-1.9.1-p378 reports: `match': invalid byte sequence in UTF-8
152
+ # TODO: ruby-1.9.1-p378 reports: `match': invalid byte sequence in UTF-8
160
153
  #++
161
154
  def match_special_regex(label, file)
162
155
  mark = remark(file)
@@ -170,12 +163,12 @@ module DNote
170
163
  # Match notes that are labeled with a colon.
171
164
  def match_general(line, lineno, file)
172
165
  rec = nil
173
- if md = match_general_regex(file).match(line)
174
- label, text = md[1], md[2]
175
- #rec = {'label'=>label,'file'=>file,'line'=>lineno,'note'=>text}
166
+ if (md = match_general_regex(file).match(line))
167
+ label = md[1]
168
+ text = md[2]
176
169
  rec = Note.new(self, file, label, lineno, text, remark(file))
177
170
  end
178
- return rec
171
+ rec
179
172
  end
180
173
 
181
174
  # Keep in mind that general non-colon matches have a higher potential
@@ -184,70 +177,70 @@ module DNote
184
177
  mark = remark(file)
185
178
  if colon
186
179
  /#{mark}\s*([A-Z]+)[:]\s+(.*?)$/
187
- else
180
+ else
188
181
  /#{mark}\s*([A-Z]+)\s+(.*?)$/
189
182
  end
190
183
  end
191
184
 
192
185
  # Organize notes into a hash with labels for keys.
193
186
  def by_label
194
- @by_label ||= (
187
+ @by_label ||= begin
195
188
  list = {}
196
189
  notes.each do |note|
197
190
  list[note.label] ||= []
198
191
  list[note.label] << note
199
- list[note.label].sort #!{ |a,b| a.line <=> b.line }
192
+ list[note.label].sort
200
193
  end
201
194
  list
202
- )
195
+ end
203
196
  end
204
197
 
205
198
  # Organize notes into a hash with filename for keys.
206
199
  def by_file
207
- @by_file ||= (
200
+ @by_file ||= begin
208
201
  list = {}
209
202
  notes.each do |note|
210
203
  list[note.file] ||= []
211
204
  list[note.file] << note
212
- list[note.file].sort! #!{ |a,b| a.line <=> b.line }
205
+ list[note.file].sort!
213
206
  end
214
207
  list
215
- )
208
+ end
216
209
  end
217
210
 
218
211
  # Organize notes into a hash with labels for keys, followed
219
212
  # by a hash with filename for keys.
220
213
  def by_label_file
221
- @by_label ||= (
214
+ @by_label_file ||= begin
222
215
  list = {}
223
216
  notes.each do |note|
224
217
  list[note.label] ||= {}
225
218
  list[note.label][note.file] ||= []
226
219
  list[note.label][note.file] << note
227
- list[note.label][note.file].sort! #{ |a,b| a.line <=> b.line }
220
+ list[note.label][note.file].sort!
228
221
  end
229
222
  list
230
- )
223
+ end
231
224
  end
232
225
 
233
226
  # Organize notes into a hash with filenames for keys, followed
234
227
  # by a hash with labels for keys.
235
228
  def by_file_label
236
- @by_file ||= (
229
+ @by_file_label ||= begin
237
230
  list = {}
238
231
  notes.each do |note|
239
232
  list[note.file] ||= {}
240
233
  list[note.file][note.label] ||= []
241
234
  list[note.file][note.label] << note
242
- list[note.file][note.label].sort! #{ |a,b| a.line <=> b.line }
235
+ list[note.file][note.label].sort!
243
236
  end
244
237
  list
245
- )
238
+ end
246
239
  end
247
240
 
248
241
  # Convert to an array of hashes.
249
242
  def to_a
250
- notes.map{ |n| n.to_h }
243
+ notes.map(&:to_h)
251
244
  end
252
245
 
253
246
  # Same as #by_label.
@@ -255,12 +248,11 @@ module DNote
255
248
  by_label
256
249
  end
257
250
 
258
- #
259
251
  def remark(file)
260
- @remark[File.extname(file)] ||= (
252
+ @remark[File.extname(file)] ||= begin
261
253
  mark = guess_marker(file)
262
254
  Regexp.escape(mark)
263
- )
255
+ end
264
256
  end
265
257
 
266
258
  # Guess marker based on file extension. Fallsback to '#'
@@ -269,6 +261,7 @@ module DNote
269
261
  # TODO: Continue to add comment types.
270
262
  def guess_marker(file)
271
263
  return @marker if @marker # forced marker
264
+
272
265
  case File.extname(file)
273
266
  when '.js', '.c', 'cpp', '.css'
274
267
  '//'
@@ -279,42 +272,8 @@ module DNote
279
272
  when '.asm'
280
273
  ';'
281
274
  else
282
- '#'
275
+ '#'
283
276
  end
284
277
  end
285
-
286
- # Convert to array of hashes then to YAML.
287
- #def to_yaml
288
- # require 'yaml'
289
- # to_a.to_yaml
290
- #end
291
-
292
- # Convert to array of hashes then to JSON.
293
- #def to_json
294
- # begin
295
- # require 'json'
296
- # rescue LoadError
297
- # require 'json_pure'
298
- # end
299
- # to_a.to_json
300
- #end
301
-
302
- # Convert to array of hashes then to a SOAP XML envelope.
303
- #def to_soap
304
- # require 'soap/marshal'
305
- # SOAP::Marshal.marshal(to_a)
306
- #end
307
-
308
- # XOXO microformat.
309
- #--
310
- # TODO: Would to_xoxo be better organized by label and or file?
311
- #++
312
- #def to_xoxo
313
- # require 'xoxo'
314
- # to_a.to_xoxo
315
- #end
316
-
317
278
  end
318
-
319
279
  end
320
-