-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRakefile
More file actions
208 lines (166 loc) · 5.81 KB
/
Rakefile
File metadata and controls
208 lines (166 loc) · 5.81 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# frozen_string_literal: true
require 'yaml'
require 'fileutils'
# Default archive year detection
def detect_year
# Try to get year from index.md or use current year
index_content = File.read('index.md', encoding: 'UTF-8') rescue nil
if index_content
# Look for date in the content
date_match = index_content.match(/(\d{4})/)
return date_match[1] if date_match
end
Time.now.year.to_s
end
# Default edition name
def detect_edition
'devfest'
end
desc "Archive the current Jekyll build to assets/<year>/<edition>/"
task :archive, [:year, :edition] do |_t, args|
args.with_defaults(year: detect_year, edition: detect_edition)
year = args[:year]
edition = args[:edition]
archive_path = "assets/#{year}/#{edition}"
build_path = '_site'
puts "Archiving #{year}/#{edition}..."
puts "=" * 50
# Step 1: Build the Jekyll site
puts "\n[1/5] Building Jekyll site..."
system('bundle exec jekyll build --trace') || exit(1)
# Step 2: Clean and create archive directory
puts "\n[2/5] Preparing archive directory: #{archive_path}/"
FileUtils.rm_rf(archive_path)
FileUtils.mkdir_p(archive_path)
# Step 3: Copy build files to archive
puts "\n[3/5] Copying build files..."
Dir.glob("#{build_path}/*").each do |file|
target = "#{archive_path}/#{File.basename(file)}"
if File.directory?(file)
FileUtils.cp_r(file, target)
else
FileUtils.cp(file, target)
end
end
# Step 4: Fix relative URLs in HTML files
puts "\n[4/5] Fixing relative URLs..."
fix_urls(archive_path, "/assets/#{year}/#{edition}/")
# Step 5: Add to archives menu
puts "\n[5/5] Adding to archives menu..."
add_to_archives(year, edition)
puts "\n" + "=" * 50
puts "Archive complete: #{archive_path}/"
puts "View at: http://localhost:4000/#{archive_path}/"
end
desc "Build and archive with current year (default edition: devfest)"
task :archive_current do
Rake::Task[:archive].invoke(detect_year, detect_edition)
end
desc "List existing archives"
task :list_archives do
puts "Existing archives:"
puts "-" * 40
archives_yml = YAML.load_file('_data/commons.yml') rescue nil
archives_md = File.read('archives.md', encoding: 'UTF-8') rescue nil
if archives_md
archives_md.scan(/-\s*title:\s*(.+?)\n\s*url:\s*(.+?)\n/).each do |title, url|
puts " #{title.strip} => #{url.strip}"
end
end
puts "\nArchive directories:"
Dir.glob('assets/*').select { |f| File.directory?(f) && f.match?(/\d{4}/) }.each do |year_dir|
year = File.basename(year_dir)
Dir.glob("#{year_dir}/*").select { |f| File.directory?(f) }.each do |edition_dir|
edition = File.basename(edition_dir)
puts " #{year}/#{edition}/"
end
end
end
# Helper method to fix URLs in HTML files
def fix_urls(archive_path, base_path)
html_files = Dir.glob("#{archive_path}/**/*.html")
css_files = Dir.glob("#{archive_path}/**/*.css")
js_files = Dir.glob("#{archive_path}/**/*.js")
all_files = html_files + css_files + js_files
all_files.each do |file|
content = File.read(file, encoding: 'UTF-8')
original_content = content.dup
# Fix absolute asset paths to be relative to the archive location
# Pattern: href="/assets/..." or src="/assets/..."
# Replace with: href="assets/..." (relative)
# For href attributes
content.gsub!(/href="\/assets\//, 'href="assets/')
content.gsub!(/href='\/assets\//, "href='assets/")
# For src attributes
content.gsub!(/src="\/assets\//, 'src="assets/')
content.gsub!(/src='\/assets\//, "src='assets/")
# Fix root-relative links to site root (keep as absolute for navigation)
# href="/" becomes href="/" (unchanged - points to current site)
# href="/archives" becomes href="/archives" (unchanged)
# Fix url() references in CSS
content.gsub!(/url\("?\/(assets\/[^"\)]+)\)?/, 'url(\1)')
content.gsub!(/url\('?\/(assets\/[^'\)]+)\)?/, "url('\1')")
# Write back if changed
if content != original_content
File.write(file, content)
puts " Fixed URLs in: #{file.sub(archive_path + '/', '')}"
end
end
end
# Helper method to add archive entry to archives.md
def add_to_archives(year, edition)
archives_file = 'archives.md'
unless File.exist?(archives_file)
puts " WARNING: #{archives_file} not found, skipping menu update"
return
end
content = File.read(archives_file, encoding: 'UTF-8')
# Check if already exists
archive_url = "/assets/#{year}/#{edition}/"
if content.include?(archive_url)
puts " Archive entry already exists in #{archives_file}"
return
end
# Format edition name for display
display_name = edition == 'devfest' ? "DevFest #{year}" : "#{edition.capitalize} #{year}"
# Find the archives section and add the new entry at the beginning
new_entry = " - title: #{display_name}\n url: #{archive_url}"
# Insert after "archives:" line
if content =~ /(archives:\n)/
content = content.sub($1, "#{$1}#{new_entry}\n")
File.write(archives_file, content)
puts " Added archive entry: #{display_name}"
else
puts " WARNING: Could not find 'archives:' section in #{archives_file}"
end
end
desc "Show help for archive task"
task :archive_help do
puts <<-HELP
Archive Task - Usage Guide
==========================
Archive current site (auto-detects year):
rake archive
Archive with specific year and edition:
rake archive[2025,devfest]
rake archive[2024,devfestnoz]
rake archive[2023,special]
List existing archives:
rake list_archives
Examples:
---------
# Archive 2025 DevFest
rake archive[2025,devfest]
# Archive with current year
rake archive_current
Archive Structure:
------------------
The archive will be created at: assets/<year>/<edition>/
This preserves the complete built site including:
- index.html (homepage)
- All CSS, JS, and images
- Speaker pages
- Agenda
The archive is automatically added to the Archives page.
HELP
end