forked from UrgNetwork/urg_library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_comment.rb
176 lines (153 loc) · 4.02 KB
/
split_comment.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
86
87
88
89
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env ruby
# -*- coding: cp932 -*-
# Doxygen コメントに従って対象ファイルをコピーするときのコメント出力を調整する
#$KCODE = "SJIS"
if ARGV.empty?
print "usage: \n" +
" " + __FILE__ + " [options] <files> <directory> \n" +
"\n" +
"[options]\n" +
" -e output english comment.\n" +
" -j output Japanese comment.\n" +
"\n"
exit(1)
end
# 最後の引数がディレクトリでなければ、エラーメッセージを返す
output_directory = ARGV.pop
output_mode = ARGV.shift
if not FileTest::directory?(output_directory)
print "No such directory: " + output_directory + "\n"
exit(1)
end
target_files = ARGV
def split_single_line(line, mode, output_mode)
# 現在のモードに従ったコメントのみを出力する
if output_mode == "-e" then
#if line =~ /\\\~japanese .+ ([\*\\])/
if line =~ /\\\~japanese .+?( \\\~english )/
line = $` + $1 + $'
elsif line =~ /\\\~english .+?( \\\~japanese )/
line = $` + $'
end
if line =~ /\\\~japanese .+?([\r\n])/
#line = $` + $1 + $'
line = ""
end
if line =~ /\\\~english /
line = $` + $'
end
end
if output_mode == "-j"
#if line =~ /\\\~english .+ ([\*\\])/
if line =~ /\\\~english .+?( \\\~japanese )/
line = $` + $1 + $'
elsif line =~ /\\\~japanese .+?( \\\~english )/
line = $` + $'
end
if line =~ /\\\~english .+?([\r\n])/
#line = $` + $1 + $'
line = ""
end
if line =~ /\\\~japanese /
line = $` + $'
end
end
return line
end
def remove_matched_word(line)
if line.strip == ""
line = ""
end
line
end
# Doxygen コメントに従って対象ファイルをコピーするときのコメント出力を調整する
def split_comment(file_name, output_mode)
lines = ""
mode = "both"
is_comment = false
p file_name
File.open(file_name) { |fd|
fd.each { |line|
line.force_encoding("cp932")
if is_comment
# コメント中
# case line
# when /\\\~japanese/
# mode = "japanese"
# line = $` + $'
# if line.strip == ""
# line = ""
# end
# when /\\\~english/
# mode = "japanese"
# line = $` + $'
# if line.strip == ""
# line = ""
# end
# when /\\\~/
# mode = "both"
# line = remove_matched_word($` + $')
# end
if line =~ /\\\~japanese/
mode = "japanese"
line = $` + $'
if line.strip == ""
line = ""
end
elsif line =~ /\\\~english/
#mode = "japanese"
mode = "english"
line = $` + $'
if line.strip == ""
line = ""
end
elsif line =~ /\\\~/
mode = "both"
line = remove_matched_word($` + $')
end
if line =~ /\*\//
is_comment = false
lines += line
else
# モードに従ってコメントを出力する
if mode == "japanese" and output_mode == "-j"
lines += line
elsif mode == "english" and output_mode == "-e"
lines += line
elsif mode == "both"
lines += line
end
end
else
# ソースコード中
if line =~ /\/\*/
if line =~ /\*\//
is_comment = false
line = split_single_line(line, mode, output_mode)
else
is_comment = true
mode = "both"
end
else
# // コメントの場合の処理
case line
when /\\\~japanese/
line = split_single_line(line, mode, output_mode)
when /\\\~english/
line = split_single_line(line, mode, output_mode)
end
end
lines += line
end
}
}
lines
end
# 対象のファイルを処理しつつ、指定されたディレクトリにコピーする
target_files.each { |file_name|
converted_lines = split_comment(file_name, output_mode)
converted_file_name = output_directory + "/" + File.basename(file_name)
File.open(converted_file_name, "w") { |fd|
fd.print converted_lines
}
}