forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.rb
53 lines (46 loc) · 1.14 KB
/
format.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
require_relative 'dir_handler'
class Format
attr_reader :dir, :extensions
def initialize dir, extensions
@dir = dir
@extensions = extensions
@dir.freeze
@extensions.freeze
@handlers = extensions.collect{ |e| DirHandler.new dir, e }
end
# 1,2,3,4 => 1, 2, 3, 4
def for_commas
each_line! do |line|
begin
line.gsub! /,(\S)/, ', \1' unless line.match /.*\".*,.*\".*/ or line.match /','/
rescue Exception => e
puts e.message + ", ignored."
end
end
end
#{foo} => { foo }
def for_curly_brackets
for_pairs '{', '}'
end
private
def each_line!
@handlers.each do |h|
h.on_each_line! { |line| yield line }
end
end
def for_pairs open, close
each_line! do |line|
begin
if line.match /#{ open + ".*" + close }/
line.gsub! /#{ open + "(\S)"}/, open + ' \1'
line.gsub! /#{ "(\S)" + close }/, '\1 '+ close
end
rescue Exception => e
puts e.message + ", ignored."
end
end
end
end
format = Format.new ARGV[0], [ "cpp", "h", "hpp", "md" ]
format.for_commas
format.for_curly_brackets