@@ -286,33 +286,19 @@ def require_relative(path)
286
286
lines . each_with_index { |line , index |
287
287
line . strip!
288
288
unless line . length == 0
289
- if /^s?#/ . match ( line )
290
- # Line starts with a hashtag
289
+ if starts_with_hashtag ( line )
291
290
if index == 0
292
- # Consider it a header if it's the first line
293
- line = line . upcase . colored . yellow . sub /#\s ?/ , "#{ i } . "
291
+ line = line_header ( line , i )
294
292
header_found = true
295
293
else
296
- # Otherwise, it's a comment
297
- line = "\t #{ line . colored . green } "
294
+ line = line_comment ( line )
298
295
end
299
296
else
300
- # Line doesn't start with a hashtag
301
- if line . index cmd or line [ 0 ] == '$'
302
- # Line contains the search keyword, or starts with a $
303
- # Consider it a shell command
304
- if line [ 0 ] != '$'
305
- # If the line doesn't start with a $, add it
306
- line = "\t $ #{ line } "
307
- else
308
- # Otherwise, just indent the line
309
- line = "\t #{ line } "
310
- end
311
- # Highlight the search query
312
- line . gsub! cmd , cmd . important
297
+ if contains_query ( line , cmd ) or starts_with_dollar ( line )
298
+ line = line_cmd ( line , cmd , !starts_with_dollar ( line ) )
313
299
else
314
300
# Last resort - assume it's a comment
315
- line = " \t # #{ line } " . colored . green
301
+ line = line_comment ( line )
316
302
end
317
303
end
318
304
else
@@ -324,7 +310,7 @@ def require_relative(path)
324
310
}
325
311
326
312
if !header_found
327
- body . unshift " #{ i } . UNTITLED" . colored . yellow
313
+ body . unshift line_header ( "# Untitled" , i )
328
314
end
329
315
330
316
puts "\n " + body . join ( "\n " ) + "\n "
@@ -334,3 +320,31 @@ def require_relative(path)
334
320
end
335
321
end
336
322
end
323
+
324
+
325
+ def contains_query ( str , query )
326
+ return str . index query
327
+ end
328
+
329
+ def starts_with_dollar ( str )
330
+ return /^\s ?$/ . match ( str )
331
+ end
332
+
333
+ def starts_with_hashtag ( str )
334
+ return /^\s ?#/ . match ( str )
335
+ end
336
+
337
+ def line_comment ( str )
338
+ return "\t # #{ str . sub ( '#' , '' ) } " . colored . green
339
+ end
340
+
341
+ def line_cmd ( str , highlight , prefix = false )
342
+ if prefix
343
+ str = "$ #{ str } "
344
+ end
345
+ return "\t #{ str } " . gsub highlight , highlight . important
346
+ end
347
+
348
+ def line_header ( str , i )
349
+ return str . upcase . colored . yellow . sub /#\s ?/ , "#{ i } . "
350
+ end
0 commit comments