Class: CommentGetter

Inherits:
Object
  • Object
show all
Defined in:
lib/comment_getter.rb

Class Method Summary collapse

Class Method Details

.comments_of_c(source_file) ⇒ Object

++ get comments of C/C++ ++



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/comment_getter.rb', line 90

def self.comments_of_c(source_file)
  comments = []
  multi_comment = false
  File.readlines(source_file).each do |line|
    if multi_comment == false && line =~ /\/\*/
      # ++
      # multi comment start
      # ++
      comments << line
      multi_comment = true
    elsif multi_comment == false && line !~ /\/\*/
      if line =~ /.*(\/\/.*)$/
        comments << line
      end
    elsif multi_comment == true && line =~ /\*\//
      # ++
      # multi comment end
      # ++
      comments << line
      multi_comment = false
    elsif multi_comment == true && line !~ /\*\//
      comments << line
    end
  end
  comments
end

.comments_of_cpp(source_file) ⇒ Object



117
118
119
# File 'lib/comment_getter.rb', line 117

def self.comments_of_cpp(source_file)
  comments_of_c(source_file)
end

.comments_of_perl(source_file) ⇒ Object

++ get commendts of perl source code ++



51
52
53
# File 'lib/comment_getter.rb', line 51

def self.comments_of_perl(source_file)
  comments_of_ruby(source_file)
end

.comments_of_python(source_file) ⇒ Object

++ get comments of python ++



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/comment_getter.rb', line 58

def self.comments_of_python(source_file)
  comments = []
  multi_comment = false
  File.readlines(source_file).each do |line|
    if line =~ /^#\!/
      next
    elsif multi_comment == false && line =~ /"""/
      # ++
      # multi comment start
      # ++
      comments << line
      multi_comment = true
    elsif multi_comment == false && line !~ /"""/
      if line =~ /\s*(#.*)$/
        comments << line
      end
    elsif multi_comment == true && line =~ /"""/
      # ++
      # multi comment end
      # ++
      comments << line
      multi_comment = false
    elsif multi_comment == true && line !~ /"""/
      comments << line
    end
  end
  comments
end

.comments_of_ruby(source_file) ⇒ Object

++ get comments of ruby source code ++



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/comment_getter.rb', line 29

def self.comments_of_ruby(source_file)
  comments = []
  File.readlines(source_file).each do |line|
    if line =~ /^#\!/
      next
    elsif line =~ /(\#.*)$/
      comments << line
    end    
  end  
  comments
end

.comments_of_shell(source_file) ⇒ Object

++ get commendts of shell source code ++



44
45
46
# File 'lib/comment_getter.rb', line 44

def self.comments_of_shell(source_file)
  comments_of_ruby(source_file)
end

.on(file:, type:) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/comment_getter.rb', line 2

def self.on(file:, type:)
  type ||= 'plain'
  case type
  when 'plain','md','markdown'
    [File.read(file)]
  when 'ruby','rb'
    comments_of_ruby(file)
  when 'python','py'
    comments_of_python(file)
  when 'perl','pl'
    comments_of_perl(file)
  when 'shell','sh'
    comments_of_shell(file)
  when 'javascript','js'
    comments_of_c(file)
  when 'c','h'
    comments_of_c(file)
  when 'c++','cpp','cxx'
    comments_of_cpp(file)
  else
    [File.read(file)]
  end
end