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
|
# File 'src/interpolating_generator.rb', line 3
def generate(_elements_to_generate)
start_marker = self.start_marker
end_marker = self.end_marker
content = "#{start_marker}#{new_content}#{end_marker}"
if File.exist?(template_path)
match = insert_at&.match(target_contents)
content = if match
if match.captures.empty?
pre = match[0]
post = ""
else
pre, *post = match.captures
post = post.join
end
"#{match.pre_match}#{pre}#{content}#{post}#{match.post_match}"
else
"#{target_contents}#{content}"
end
else
match = /\A\n+/.match(content)
if match
newline_count = match[0].size
start_marker = start_marker[newline_count..]
content = match.post_match
end
match = /\n+\z/.match(content)
if match
newline_count = match[0].size
end_marker = end_marker[...-newline_count]
content = match.pre_match
end
content = "#{content}\n"
end
{ content:, start_marker:, end_marker: }
end
|