-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgenerate_staging_deployment.rb
More file actions
122 lines (101 loc) · 3.3 KB
/
generate_staging_deployment.rb
File metadata and controls
122 lines (101 loc) · 3.3 KB
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
def add_prefix_to_image_in_bib(bib_filename, prefix)
lines = File.readlines(bib_filename)
image_pattern = /^\s*image\s*=\s*{([^}]+)}\s*,?$/
new_lines = lines.map do |line|
if match = line.match(image_pattern)
new_image_value = "#{prefix}#{match[1]}"
line.sub(match[1], new_image_value)
else
line
end
end
File.open(bib_filename, 'w') do |file|
file.puts(new_lines)
end
end
def add_prefix_to_image_in_md(md_filename, prefix)
lines = File.readlines(md_filename)
image_pattern = /image=['"]([^'"]+)['"]/
new_lines = lines.map do |line|
if match = line.match(image_pattern)
new_image_value = "#{prefix}#{match[1]}"
line.sub(match[1], new_image_value)
else
line
end
end
File.open(md_filename, 'w') do |file|
file.puts(new_lines)
end
end
def remove_prefix_from_image_in_bib(bib_filename, prefix)
lines = File.readlines(bib_filename)
image_pattern = /^\s*image\s*=\s*{#{Regexp.escape(prefix)}([^}]+)}\s*,?$/
new_lines = lines.map do |line|
if match = line.match(image_pattern)
new_image_value = match[1]
line.sub("#{prefix}#{new_image_value}", new_image_value)
else
line
end
end
File.open(bib_filename, 'w') do |file|
file.puts(new_lines)
end
end
def remove_prefix_from_image_in_md(md_filename, prefix)
lines = File.readlines(md_filename)
image_pattern = /image=['"]#{Regexp.escape(prefix)}([^'"]+)['"]/
new_lines = lines.map do |line|
if match = line.match(image_pattern)
new_image_value = match[1]
line.sub("#{prefix}#{new_image_value}", new_image_value)
else
line
end
end
File.open(md_filename, 'w') do |file|
file.puts(new_lines)
end
end
def update_baseurl_in_config(config_filename, action, prefix)
lines = File.readlines(config_filename)
baseurl_pattern = /^(\s*baseurl\s*:\s*)(".+"|'.+'|\S*)\s*$/
new_lines = lines.map do |line|
if match = line.match(baseurl_pattern)
if action == 'staging'
line = "#{match[1]}\"#{prefix}\"\n"
elsif action == 'live'
line = "#{match[1]}\"\"\n"
end
end
line
end
File.open(config_filename, 'w') do |file|
file.puts(new_lines)
end
end
def main(action, bib_filename, md_filename, config_filename, prefix)
if action == 'staging'
add_prefix_to_image_in_bib(bib_filename, prefix)
add_prefix_to_image_in_md(md_filename, prefix)
update_baseurl_in_config(config_filename, action, prefix)
elsif action == 'live'
remove_prefix_from_image_in_bib(bib_filename, prefix)
remove_prefix_from_image_in_md(md_filename, prefix)
update_baseurl_in_config(config_filename, action, prefix)
else
puts "Invalid action. Use 'staging' or 'live'."
end
end
# Usage: ruby modify_images.rb [add|remove] [prefix]
if ARGV.length != 2
puts "Usage: ruby modify_images.rb [deploy|live] [prefix]"
else
action = ARGV[0]
bib_filename = "_bibliography/references.bib"
md_filename = "_pages/people.md"
config_filename = "_config.yml"
prefix = ARGV[1]
main(action, bib_filename, md_filename, config_filename, prefix)
end