-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathspacer
executable file
·177 lines (146 loc) · 2.82 KB
/
spacer
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
177
#!/usr/bin/env ruby
########################################
# HELP
########################################
HELP=<<EOS
add or remove space between double width Japanese
and single width English
with consider about markdown syntax
$ space.rb -r README.md # remove
$ space.rb -a README.md # add
$ space.rb -ra README.md # remove -> add
$ space.rb README.md # equiv with -ra
EOS
require "optparse"
opts = ARGV.getopts("rah")
if opts["h"]
puts HELP
exit 0
end
########################################
# Main
########################################
def removeSpace(str)
# "a あ" => "aあ"
str.gsub!(/([a-zA-Z0-9]) ([^[:ascii:]])/) {
"#{$1}#{$2}"
}
# "あ a" => "あa"
str.gsub!(/([^[:ascii:]]) ([a-zA-Z0-9])/) {
"#{$1}#{$2}"
}
return str
end
def addSpace(str)
# "aあ" => "a あ"
str.gsub!(/([a-zA-Z0-9])([^[:ascii:]])/) {
if $2 === "」"
"#{$1}#{$2}"
else
"#{$1} #{$2}"
end
}
# "あa" => "あ a"
str.gsub!(/([^[:ascii:]])([a-zA-Z0-9])/) {
if $1 === "「"
"#{$1}#{$2}"
else
"#{$1} #{$2}"
end
}
return str
end
def process(data)
result = ""
state = ""
data.lines{|line|
if line.size == 1
result << line
next
end
# ```
if line.match("```.*")
if state == "```"
state = ""
else
state = "```"
end
result << line
next
end
if state == "```"
result << line
next
end
# #
h = line.match(/^(\#{1,6}) (.*)/)
if h
result << "#{h[1]} #{yield(h[2])}\n"
next
end
# -
ul = line.match(/^( *-) (.*)/)
if ul
result << "#{ul[1]} #{yield(ul[2])}\n"
next
end
# +
ol = line.match(/^( *\+) (.*)/)
if ol
result << "#{ol[1]} #{yield(ol[2])}\n"
next
end
# 1.
num = line.match(/^( *\d*\.) (.*)/)
if num
result << "#{num[1]} #{yield(num[2])}\n"
next
end
result << yield(line)
}
return result
end
targets = ARGV # file paths without option flags
targets.each do |target|
path = File.absolute_path(target)
data = File.read(path)
result = process(data) {|str|
if opts["r"] == false && opts["a"] == false
str = removeSpace(str)
str = addSpace(str)
end
if opts["r"]
str = removeSpace(str)
end
if opts["a"]
str = addSpace(str)
end
str
}
if data != result
puts "update #{target}"
File.write(path, result)
end
end
__END__
ほげほげ?
aaaa
ほげ(ふが)ぴよ[おい]あ'い'う"え"お!
あ「invalid input」的
あ <invalid input> 的
あ `invalid input` 的
aaa ほげ bbb
```ruby
puts("aaa ほげ bbb")
puts("aaa ほげ bbb")
```
# ccc ひげ
- u あ asf い u
- u あ asf い u
+ u あ asf い u
+ u あ asf い u
##### ddd ふぁ sd ふぁ
1. kk あおい
2. あいう jjj
10. ff a
100. ff a