-
Notifications
You must be signed in to change notification settings - Fork 73
/
renumber.rb
178 lines (150 loc) · 3.85 KB
/
renumber.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
177
178
require 'fileutils'
InvalidFilename = Class.new(StandardError)
class Paths < Struct.new(:dir)
def all
@all ||= find_all(dir)
end
private
def find_all(dir)
find(dir).inject([]) do |result, (path, ix)|
result << [path, ix]
result + find_all(path)
end
end
def find(dir)
# paths = Dir["#{dir}/*.md"]
paths = Dir["#{dir}/**/*"]
paths = paths.select { |path| path =~ /[\d]{2}-/ }
# paths = paths.map { |path| path.sub('.md', '') }.uniq.sort
paths.map.with_index { |path, ix| [path, ix] }
end
end
class Source < Struct.new(:path, :number)
def validate!
raise InvalidFilename, "Invalid filename: #{basename}.md" unless basename =~ /^[\d]{2}-[\w_]+$/
end
def save
File.write(filename, content)
end
def move
# ['md', nil].each do |ext|
# source, target = "#{path}.md", "#{self.target}.md"
source, target = path, self.target
# target = [self.target, ext].compact.join('.')
puts "source: #{source}", "target: #{target}"
unless source == target
# FileUtils.mv(source, target)
end
# end
# self.path = self.target
end
def exercise?
dir == 'exercises'
end
def content
@content ||= File.read(filename)
end
def target
path.sub(/(.*)[\d]{2}-/, "\\1#{formatted_number}-")
end
def dir
dirname.split('-').last
end
def name
path.split('-').last
end
def filename
"#{path}.md"
end
def dirname
File.dirname(path)
end
def basename
File.basename(path)
end
def formatted_number
number.to_s.rjust(2, '0')
end
end
class Fixes < Struct.new(:sources)
def apply(source)
[Hrefs, Headlines, Filenames].each do |fix|
fix.new(sources).apply(source)
end
end
end
class Hrefs < Struct.new(:sources)
def apply(source)
path, dir, name = source.path.sub('source/', ''), source.dir, source.name
sources.each do |source|
matches = source.content.match(%r(["\(]/([\d]{2}-#{dir}/?[\d]{2}-#{name}).html["\)]))
match = matches && matches.to_a.last
next unless match && match != path
source.content.gsub!(match, path)
source.save
end
end
end
class Headlines < Struct.new(:sources)
def apply(source)
return unless source.exercise?
source.content.gsub!(/([#]+ .+) [\d]+.([\d]+)/) do |match|
"#{$1} #{source.number}.#{$2}"
end
source.save
end
end
class Filenames < Struct.new(:sources)
def apply(source)
return unless source.exercise?
name, target = source.name, File.basename(source.target)
sources.each do |source|
source.content.gsub!(/[\d]{1,2}-#{name}-([\d]{1,2}).rb/) do |match|
filename = "#{target}-#{$1}.rb"
move_solution(match, filename) unless match == filename
filename
end
source.save
end
end
def move_solution(source, target)
dir = "source/solutions"
FileUtils.mv("#{dir}/#{source}", "#{dir}/#{target}") if File.exists?("#{dir}/#{source}")
end
end
class Sources < Struct.new(:dir)
def renumber
sources.reverse.each.with_index do |source, ix|
# source.move
# source.validate!
# fixes.apply(source)
path = source.path
source.move
# sources.select { |s| s.path.include?(path) }.each do |s|
# s.path.sub(path, source.path)
# end
# next_source = sources[ix + 1]
# next unless next_source
# if next_source.path.include?(path)
# puts path
# puts source.path
# next_source.path.sub(path, source.path)
# puts next_source.path
# end
end
rescue InvalidFilename => e
puts e.message
end
private
def fixes
@fixes ||= Fixes.new(sources)
end
def sources
@sources ||= Paths.new(dir).all.map do |path, ix|
Source.new(path, ix + 1)
end
end
end
fail 'kaputt'
sources = Sources.new('source')
sources.renumber