-
Notifications
You must be signed in to change notification settings - Fork 2
/
generator.rb
53 lines (44 loc) · 1.11 KB
/
generator.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
# frozen_string_literal: true
class Generator
README = 'README.md'
DETAILS_START = /<details name="([\/\.\w]+)">/
DETAILS_END = /<\/details>/
def perform
lines = File.read(README).split("\n")
details_started = false
new_lines = lines.map do |line|
if matchdata = line.match(DETAILS_START)
details_name = matchdata.captures.first
if filepath = filepath(details_name)
puts "Processing #{details_name} ..."
details_started = true
"#{line}\n#{code_block(filepath)}"
else
puts "Skipped #{details_name}"
nil
end
elsif line.match(DETAILS_END)
puts "Processed"
details_started = false
line
elsif !details_started
line
end
end
File.write(README, new_lines.compact.join("\n"))
end
private
def filepath(details_name)
File.file?(details_name) && details_name
end
def code_block(filepath)
definition = File.read(filepath)
extension = filepath.split('.').last
<<~CODE
<p>
```#{extension}
#{definition}```
</p>
CODE
end
end