Class: CodeScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/models/utils/codescanner.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.scan_arrayObject (readonly)

Returns the value of attribute scan_array.



8
9
10
# File 'lib/models/utils/codescanner.rb', line 8

def scan_array
  @scan_array
end

Class Method Details

.array_rangeObject



48
49
50
51
52
# File 'lib/models/utils/codescanner.rb', line 48

def self.array_range
  find_begin_range
  return false unless find_end_range
  @scan_array[@begin_scan+1..@end_scan-1].join
end

.find_begin_rangeObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/models/utils/codescanner.rb', line 22

def self.find_begin_range
  @scan_array.each_with_index do |line, index|
    if line.include?('<snip>') || line.include?('<$>')
      find_title(index)
      @line = index+1
      strip_snip_tag(index)
      return @begin_scan = index
    end
  end
  return false
end

.find_end_rangeObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/models/utils/codescanner.rb', line 34

def self.find_end_range
  @scan_array.each_with_index do |line, index|
    if line.include?('<snip>') || line.include?('<$>') #mismatch before closing tag
      return false
    end
    if line.include?('</snip>') || line.include?('</$>')
      strip_snip_tag(index)
      index
      return @end_scan = index
    end
  end
  return false
end

.find_title(index) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/models/utils/codescanner.rb', line 61

def self.find_title(index)
  matches = @scan_array[index].match(/(<snip>|<\$>)(.+)/)
  if matches
    @title = matches[2].strip.chomp('-->')
  end
  @title
end

.run(scan_array, filename) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/models/utils/codescanner.rb', line 9

def self.run(scan_array, filename)
  @scan_array = scan_array
  while @scan_array.join.include?('<snip>') || @scan_array.join.include?('<$>')
    code = array_range
    if code
      Snippet.new(code: code, title: @title, line: @line, filename: filename)
    else
      return {filename: filename, line: @line} #in case of mismatch
    end
  end
  return nil
end

.strip_snip_tag(index) ⇒ Object



54
55
56
57
58
59
# File 'lib/models/utils/codescanner.rb', line 54

def self.strip_snip_tag(index)
  @scan_array[index].sub!(/<snip>/,'<*snip*>')
  @scan_array[index].sub!(/<\/snip>/,'</*snip*>')
  @scan_array[index].sub!(/<\$>/,'<*$*>')
  @scan_array[index].sub!(/<\/\$>/,'</*$*>')
end